Redimming a nested User Defined Type
HossI just blew the heck out of this problem I've been having for quite some time in my code. I couldn't quite ReDim a nested User Defined Type without getting a 'subscript out of range' error! For those of you that don't know; A User Defined Type (UDT) is a data type that you create yourself, hence User Defined. Here's an example of a UDT: [code] Public Type Chumps GirlChumps() as String BoyChumps() as String End Type [/code] That is our first data type that we'll be using. Notice that the GirlChumps() and BoyChumps() arrays have no ranges! The purpose of this post is to show you how to redim those arrays, so just wait a moment and I'll get to it. We need another UDT: [code] Public Type Classrooms Subject as String Classmates as Chumps End Type [/code] Notice I defined the variable Classmates as Chumps, our previously defined UDT. And finally: [code] Dim Schedule() as Classrooms [/code] Okay, now that we've got our variables dimmed, let's look into how we ReDim (Resize) our arrays with no ranges! "How can we do this?" You might be asking. With! That's how! [code] ReDim Schedule(1) 'This is our school schedule. Unfortunately this 'school is under-budgeted, so we can only attend 'two classes a day. With Schedule(0) 'Our first class ReDim .Classmates.GirlChumps(1) 'Only two girls in this class ReDim .Classmates.BoyChumps(1) 'Only two boys in this class End With With Schedule(1) 'Our second class ReDim .Classmates.GirlChumps(1) ReDim .Classmates.BoyChumps(1) End With [/code] As you can see, using WITH we can RedDim our undefined arrays without messing with the original Schedule() range. Alternatively, you can use a loop to iterate through the Schedule and Chumps arrays to more effectively set the ranges. I didn't do that... I typed it all out, for simplicity. Now that we have it all ReDimmed, we can now define the arrays! [code] Schedule(0).Classmates.GirlChumps(0) = "Lacy" Schedule(0).Classmates.GirlChumps(1) = "Melissa" Schedule(0).Classmates.BoyChumps(0) = "Todd" Schedule(0).Classmates.BoyChumps(1) = "Alex" Schedule(0).Subject = "Math" Schedule(1).Classmates.GirlChumps(0) = "Clarise" Schedule(1).Classmates.GirlChumps(1) = "Jenna" Schedule(1).Classmates.BoyChumps(0) = "Chris" Schedule(1).Classmates.BoyChumps(1) = "Matt" Schedule(1).Subject = "Science" [/code] Again, a loop could have been used to define the arrays, but for simplicity I typed it out manually. -------------------------- I hope this post provided more help than hinder! Sorry if it's a bit confusing!
Eric ColemanWhere does your problem occur?
HossHeh... There's no problem. It's a tutorial, not a request for help. [^]