Yes they are just two references to the same instance of the object like you said.
Printable View
Yes they are just two references to the same instance of the object like you said.
Hi, Salvelinus
"Yes they are just two references to the same instance of the object like you said."
I thought that the references are objects in themselves. You can in fact destroy either reference without affecting the other. It seems that as long as at least one reference exists, the instance remains accessible.
So, that means ALL objects are only references to other objects - the ultimate object being just a collection of code???
Really, if this carries on it should be a new thread.
I don't know the mechanics behind it, but Edneiss's answer jibes with what I've forgotten about C++. To analogize in my example, obj2 is the name of the instance of the object, obj1 is just another name for it, like a nickname. Sort of like referring to Prince Charles as the Duke of Wales. Two names for the same thing. Both names act as pointers to the same place in memory.
If SomeObject is a reference type [eg. a Class], then obj1 and obj2 are basically pointers to the same memory location.
If SomeObject is a value type [eg. a Struct], then the content of the memory location referenced by obj2 is copied to that referenced by obj1.
Using .Equals will tell you if they are the same object in memory.
VB Code:
Dim obj1 As New System.Object Dim obj2 As New System.Object MessageBox.Show(obj1.Equals(obj2)) obj1 = obj2 MessageBox.Show(obj1.Equals(obj2))
That's true nemaroller.
VB (or .NET for that matter) does a lot of things behind the scenes. I think of it like this:Quote:
Originally posted by maurices5000
Guys this has been a very beneficial thread. But still, what is this?
VB Code:
Dim FSO As New Scripting.FileSystemObject Dim DRV As Drive For Each DRV In FSO.Drives Debug.print Drv 'DRV = A:, C:, D:, E: Next DRV 'at this point DRV become nothing
Why does this code work? I understand it, but it is just vague. I don't have any really conclusive answers. Is this a type of assignment or what? Or maybe it is an issue of the For Each loop. I guess i may need to look that up. Does the for each loop require a Type between the keywords Each and In?
I don't know if that's exactly right but you get the idea.VB Code:
Dim fso As New Scripting.FileSystemObject Dim drv As Drive Dim i as Integer For i = 0 to fso.Drives.Count() - 1 drv = New Drive() drv = fso.Drives(i) Debug.Print drv Next i
The For...Each construct is just more compact (if obfuscated) code.
regarding certain data types: why is it that you delare some as NEW and some not?
ex
VB Code:
dim Name as string 'dim Name as NEW string doesn't work dim Name as NEW arraylist 'Dim Name as arraylist works too.
is detailed information on declaring New found in the msdn?
Strings are immutable, Arraylists aren't.
There are 2 types Value Types (Integer, Double, etc..) and Reference Types (Objects).
You must use the New keyword when creating a new instance of a reference type.
Strings are the exception.
Although they are a reference type, they are immutable and not subject to the New keyword rule.
thanks guys for your replies. That code was helpful Wey.
Andy i also had a similar question. so thanks.
But guys what is immutable?
My personal answer to Andy's question was that string, double, integer, etc were types while one uses NEW with objects. does this guess have any value. That was teh assumption i made so i didn't ask the question. :)
Thanks a lot. this is going to get me far in understanding the basics of OOP.
immutable means "Can't be changed"
MSDN - Reference Types and Value Types
Because when you do myString = "HI", .NET is creating a new string holding 2 chars ('H?, 'I') and will assign it to myStringQuote:
Originally posted by Andy
regarding certain data types: why is it that you delare some as NEW and some not?
ex
VB Code:
dim Name as string 'dim Name as NEW string doesn't work dim Name as NEW arraylist 'Dim Name as arraylist works too.
is detailed information on declaring New found in the msdn?