Post

 Resources 

Console

Home | Profile | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 VBGamer
 VBGamer
 Redimming a nested User Defined Type
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Hoss
Neophyte

USA
3 Posts

Posted - Jul 30 2004 :  06:54:26 AM  Show Profile  Send Hoss an AOL message  Reply with Quote
I 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:

  
Public Type Chumps  
    GirlChumps() as String
    BoyChumps() as String
End Type
  


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:
  
Public Type Classrooms  
    Subject as String
    Classmates as Chumps  
End Type
  


Notice I defined the variable Classmates as Chumps, our previously defined UDT.

And finally:
  
Dim Schedule() as Classrooms  
  


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!

  
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
  


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!

  
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"  
  


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 Coleman
Gladiator

USA
811 Posts

Posted - Jul 30 2004 :  11:25:19 AM  Show Profile  Visit Eric Coleman's Homepage  Reply with Quote
Where does your problem occur?

Go to Top of Page

Hoss
Neophyte

USA
3 Posts

Posted - Jul 30 2004 :  12:12:54 PM  Show Profile  Send Hoss an AOL message  Reply with Quote
Heh... There's no problem. It's a tutorial, not a request for help.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
VBGamer © Go To Top Of Page
This page was generated in 0.12 seconds. Snitz Forums 2000

Copyright © 2002 - 2004 Eric Coleman, Peter Kuchnio , et. al.