|
-
Nov 14th, 2005, 07:16 AM
#1
Thread Starter
Lively Member
Clarification on Something
Lo all, just a small clarification on something in OOP.
If I had a class called characters and done this in another class
VB Code:
Public Members() as Character
Public Sub SetMembers(a as Character)
Members(0) = a
end sub
Then Members(0) would hold a reference to the character ??
Where if I done this
VB Code:
Public Members() as New Character
Public Sub Set Members(a as Character)
Members(0) = a
end Sub]
Then Members would hold a reference to a charcter but a new instance which just holds same data as 'a'.??
Is this correct?
-
Nov 14th, 2005, 07:25 AM
#2
Re: Clarification on Something
The first one wouldn't work because you never instantiate (read: create) the array and the second wouldn't work because it is invalid syntax. To declare and instantiate an array in one line you would use either of these two sytaxes:
VB Code:
Private arr1(upperBound) As Object
Private arr2() As Object = New Object(upperBound) {}
You can then initialise the elements in the second case if you like also. If you declare an arry without specifying an upper bound then you have created a variable that can refer to an array but you have not created an array object. You have to, at some point, specify the upper bound so that the elements are created and you can then assign objects to them.
-
Nov 14th, 2005, 07:34 AM
#3
Thread Starter
Lively Member
Re: Clarification on Something
Sorry I dodnt include the array bits in full for speed lol. If they werent arrays would it hold the refernce for an existing object which I could then get info from??
-
Nov 14th, 2005, 07:43 AM
#4
Re: Clarification on Something
If you assign an instance of a class to a variable then that variable contains a reference to the object. if you assign the same instance to another variable then both variables refer to the same object. If you do the same thing with an instance of a structure, then each variable will contain a copy of the original object. With structures, e.g Integers, Booleans, Chars, etc. the variable contains the object itself, so every variable is a distinct object. With classes the variable contains a reference to the object, i.e. its address in memory, so a number of variables can all refer to the same object. I hope that answers your question becasue I'm afraid I'm not 100% sure what you're asking. Note that arrays are instances of the Array class, so a number of array variables can refer to the same object. The elements of an array can be classes or structures, so the aforementioned rules apply in each case.
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
|