|
-
Dec 1st, 2012, 10:11 AM
#1
Thread Starter
Junior Member
[RESOLVED] Structures with Arrays
I am having a hard time understanding how VB 2010 wants me to go about declare a variable size inside a structure. I am used to VB6 and trying to learn VB 2010, but the books I have bought tell me nothing about structures.
In VB6 I could define a custom variable type as such:
Type PolygonType
Dim numberSides as Integer
Dim Length(20) as Integer
Dim Area as Integer
Dim Circumference as Integer
End Type
Now with structures I cannot declare the variable size. For my project I have a structure for a tournament information in my module1.
Structure TournType
Dim Name As String
Dim NumDays As Integer
Dim TournDate() As Date
Dim NumFields As Integer
Dim FieldName() As String
Dim GroupEnabled() As Boolean
Dim DefaultGameLen() As Integer
Dim StartTime As Integer
Dim NumGames As Integer
Dim NumRefs As Integer
Dim NumTeams As Integer
End Structure
Then I create a public variable
Public TournInfo As TournType
So how am I supposed to tell VB how large the arrays TournDate, FieldName, GroupEnabled and DefaultGameLen should be? Thank you.
-
Dec 1st, 2012, 10:18 AM
#2
Re: Structures with Arrays
You don't. You can't. Use a Class instead (and declare the variables Public).
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Dec 1st, 2012, 04:50 PM
#3
Re: Structures with Arrays
You are almost certainly going to want a class. However, you could add a method to the structure that would initialize the arrays. Ultimately, though, it is more likely that you'd want a class. Structures make very little sense for all but simple constructs with few members. You have a very large structure with a whole series of arrays, which are reference types to begin with, so you will probably be saving yourself a lot of headaches by using a class right from the start. You would then have more flexibility in using a constructor, too.
My usual boring signature: Nothing
 
-
Dec 6th, 2012, 04:48 PM
#4
Thread Starter
Junior Member
Re: Structures with Arrays
Thank you. I have changed all my Structures to Classes. Now I am having a little problem with that change. I just do not understand the usage enough to properly code it correctly.
I want to create arrays of some of these new classes. For example, in my module I tried to create an array of a class I called refClass.
Public Referee(500) as refClass
But in debug mode I am told that I need to use New to create the Referee variable. However, when I correct this in the module with
Public Referee(500) as New refClass
The "New" is underlined with the explanation that this keyword cannot be used to create an array.
-
Dec 6th, 2012, 05:23 PM
#5
Re: Structures with Arrays
This is fine as it stands.
Public Referee(500) as refClass
But when you assign values you need to do it this way ...
Referee(0) = New refClass
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Dec 7th, 2012, 01:50 PM
#6
Re: Structures with Arrays
This is the difference between a value type (a structure, but also things like integers, doubles, and the like) and reference types (all classes, including strings, though strings act like value types just to confuse the issue). With a value type, just declaring it creates it, because the variable is a named chunk of memory that holds the actual object. For example, if you create an Integer variable with:
Dim x as Integer
then you have a chunk of memory holding an integer (0 to begin with) that has been named x. This will be true if you create a structure, too. All the different members of the structure will be found in that chunk of memory, but they will all be there. One thing this means is that if you create a structure, you end up with a chunk of memory large enough to hold ALL of the members of that structure, which could be a reasonably large piece of memory.
When you create a variable of reference type, you still get a named piece of memory, but that named piece of memory is the size of an address, which means it is 32 bits (four bytes) on a 32 bit OS, and 64 bits (8 bytes) on a 64 bit OS. It doesn't matter how big the actual type is, because the object isn't in the variable. In fact, the actual object is located somewhere else in memory, and you don't have to worry about that (until you run out of real and virtual RAM, which is a HUGE amount). All that the variable holds is the address of the memory (sort of, there's a bit more to it than that, but you can ignore that). This means that variables of reference type are small and easy to pass around. However, it also means that declaring the variable just allocates enough memory to hold that tiny little address, instead of creating the whole object. Therefore, you need to call New to create the object and stick its address into the variable.
Now, think about what happens when you create an array of structures: What happens is that all of the structures are created with sufficient memory to hold all of their memories. The array is just a sequence of these chunks of memory, one after the other, but all of the structures are there, and all of the members are initialized to their default types.
Roughly speaking, the same thing happens when you create an array of reference types: All of the memory to hold all of the references are created, but the actual objects themselves are not, because all that the array holds is a series of addresses to the actual objects. The objects themselves don't even have to be side by side in memory, but could be located anywhere. The array still holds the series of addresses one after the other, but the objects themselves can be anywhere, and in any order, in memory. When you have the array of reference types, you just have the space for their addresses, you still have to create all the actual reference type objects and put them (or put their addresses, actually) into the array.
This might make it appear that structures are easier to use than classes, since the structure is right there in the array and you don't have to do the other step of creating them. In practice, it is usually easier to work with classes than with structures, especially when they are in collections (though there is one exception to this, and I can't recall whether it is an array or a List, but I think it's an array). Normally, when you access one member with something like:
myObj = myList(4)
what happens is that you get a copy of whatever is in myList in the fifth slot. If myList holds reference types, you are getting a copy of the address, which is great, since that's usually what you want. If myList holds structures, you get a copy of the structure itself, so any changes made to myObj will not be reflected in myList(4), since they are two different objects at that time.
Another point to note is that if you pass a structure to a method, then you are copying ALL of that memory into the argument, which means that a lot more memory has to be swapped around when you make the method call. If the argument is a reference type, then all you are copying in is one little address, which is small.
A third point to note is that a structure can hold a reference type. In your case, the structure held an array, which is a reference type, itself. That means that you have a few integers, and a reference (the address) to an array. The array itself isn't in the structure, just the refernce to it is. That makes copying both a bit easier (since there is less memory moving around than would be the case if the WHOLE array had to be copied), and a bit more error prone (since some members make copies, while other members are copies of addresses while the underlying objects remain unchanged).
In general, people find classes easier to work with than structures, but you do have to create the objects.
My usual boring signature: Nothing
 
-
Dec 8th, 2012, 03:10 AM
#7
Thread Starter
Junior Member
Re: Structures with Arrays
Dunfiddlin - Thank you. I figured it had to be something simple.
Shaggy - Thank you for the explanation on the differences. Good to know.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|