|
-
Jan 10th, 2015, 03:22 PM
#1
Thread Starter
Addicted Member
[RESOLVED] Nested Structure Problem (Revisited)
Shaggy,
I redid the program per your suggestion of using Classes and I am now back to the "Null Ref Error again. The Structure version works perfectly.
Confused.
Code:
Module Module1
Class MyAcft
Public Sub New(numParts As Integer)
ReDim Parts(0 To numParts - 1)
End Sub
Property Type As String
Property Tail As String
Public Parts() As Part
Class Part
Public Number As Integer
Public Name As String
End Class
End Class
Sub Main()
Dim Acft As New MyAcft(5)
Acft.Tail = "N12345"
Acft.Type = "AH1"
Acft.Parts(0).Number = 555688
Acft.Parts(0).Name = "Rotor"
Console.WriteLine("Done")
Console.ReadLine()
End Sub
End Module
-
Jan 10th, 2015, 04:29 PM
#2
Re: Nested Structure Problem (Revisited)
Yeah, interesting thing, isn't it? The problems with classes and structures are a bit different. Consider this example:
Dim myArray As Integer(5)
That creates an array of integers, and there is an integer in each slot of the array set to its default value, which is 0. You have created a collection of slots that can hold integers, and they hold the default value for the integer. However, when you consider this example:
Dim myArray As SomeClass(5)
you have essentially the same thing: It is a collection of slots that holds something, but what exactly? Well, if you create a single SomeClass variable:
Dim myObject As SomeClass
the variable myObject doesn't contain the class. All the variable contains is a reference to the class, which is effectively the address of the class in memory. When you create an instance of the class with:
myObject = New SomeClass
then you are creating a new object out in memory somewhere, and assigning the address of that object to the variable. The variable never holds very much, which is what is efficient about them. If you pass a class to a variable, all you are passing is that little reference, rather than the whole object (which could be of any size). So, the variable never holds more than the reference to an object, which is located somewhere out in memory.
Now, to get back to the array, what does it hold? Just like when you create an array of integers, an array of classes is a bunch of slots occupied by the default value for a reference variable, which is Nothing. Those slots can all point to different classes, or even to the same class, but until they are set to be a reference to a thing, they are a reference to Nothing. So, creating an array of reference types (classes) still requires you to do a further step and create the objects that those references will refer to. A structure is a value type, so it acts just like other value types, such as the integer. All items are set to their default instances. Of course, if you have a structure that contains an array, then the array will also be set to its default instance, which you have already seen is Nothing. That's because the array is a reference type.
So, if you stick with classes throughout, then you'd have to add something to the constructure. Instead of just redimming the array of Part, you'd also have to create new Part objects to put in each slot in the array (actually, you'd be creating the Part objects, but you'd only be putting the reference to them into the array):
Code:
Public Sub New(numParts As Integer)
ReDim Parts(0 To numParts - 1)
For x = 0 to numParts-1
Parts(x) = New Part
Next
End Sub
So, you might be wondering why one would choose a class over a structure. I don't have a solid answer for that, nor does anybody else. There are guidelines to follow, but no absolute rules that I know of. Structures may be more efficient as long as they only have two or three members, and all the members are value types (no arrays or classes). Your Part class could be a structure, in which case your Parts array would be an array of structures, in which case you wouldn't need that loop to create the Part objects. It may even be better off as a structure, but it's more a matter of philosophy. After all, you do have a string in the Part class, and a string is technically a reference type, though it acts very much like a value type for reasons I have heard, but can't recall.
So, there's a long answer.
My usual boring signature: Nothing
 
-
Jan 10th, 2015, 10:03 PM
#3
Thread Starter
Addicted Member
Re: Nested Structure Problem (Revisited)
Shaggy,
You're the man! Good explanation. I'm learning a lot. Thank you.
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
|