W
Workgroups
I need some clarification with the whole "no pointers in VB.NET" thing,
because it seems like there are "quazi-pointers" happening but I want to
make sure I'm interpreting all this correctly.
Here's a generic class declaration for the sake of example:
Public clsClass
'properties, methods
End Class
And now some declarative snippits, followed by abstracts of what I think is
happening:
Dim objClass As clsClass
That sets up something like this in memory:
{objClass = Nothing} <- Can hold a memory address, but currently holds
Nothing so far
So as I understand it, this essentially creates something akin to a
"pointer". It cannot be treated like a C++ pointer, but it is something of
a pointer in the sense that it will "point" to memory.
objClass = New clsClass
Now it's set to an instance of an object, and so here's what I assume is now
going on in memory:
{@ address 1, clsClass instance} <- is now physically stored in the managed
memory area
{objClass = @1} <- now stores the memory address for lookup of the clsClass
instance
Dim objClass2 As clsClass = objClass
Because they are references, this is what I assume is happening after this
declaration:
{@ address 1, clsClass instance} <- still physically stored in the managed
memory area
{objClass = @1} <- still storing the memory address for lookup
{objClass2 = @1} <- now storing the same memory address for lookup as
objClass
Is this essentially correct? I still have only one actual instance, and two
smaller pointers (well, references) available for lookups against the same
instance. Let's add one more:
Dim objClass3 as New clsClass
{@ address 1, clsClass instance}
{objClass = @1}
{objClass2 = @1}
{@ address 2, clsClass instance}
{objClass3 = @2}
So now two instances, three references (two of which point to the same
instance). Now I start wandering out into stuff I'm not sure I understand,
for example a HashTable. Here's my guess:
Dim objHash as New HashTable:
{@ memory address 1, clsClass instance}
{objClass = 1}
{objClass2 = 1}
{@ memory address 2, clsClass instance}
{objClass3 = 2}
{@ memory address 3, HashTable instance}
{objHash = 3}
And then...
objHash.Add("A", objClass2)
{@ address 1, clsClass instance}
{objClass = @1}
{objClass2 = @1}
{@ address 2, clsClass instance}
{objClass3 = @2}
{@ address 3, HashTable instance}
{HashTable "A" = @1} <- ...something like that?
{objHash = @3}
Is the Hash table storing data in this fashion? Do I still have just one
instance of clsClass?
And finally does the garbage collector kill the instance for me once all the
references are gone?
objClass = Nothing
objClass2 = Nothing
{@ memory address 1, clsClass instance}
{@ memory address 2, clsClass instance}
{objClass3 = 2}
{@ memory address 3, HashTable instance}
{HashTable "A" = 1}
{objHash = 3}
Is the HashTable item "A" maintaining my instance @1?
objHash.Remove(objHash("A"))
[...GC runs]
{@ address 2, clsClass instance}
{objClass3 = @2}
{@ address 3, HashTable instance}
{objHash = @3}
I'm starting a project using lots and lots of classes in multiple 2D arrays
of hashtables and I want to make sure I have this stuff more or less correct
in my mind before I start allocating memory like gangbusters.
because it seems like there are "quazi-pointers" happening but I want to
make sure I'm interpreting all this correctly.
Here's a generic class declaration for the sake of example:
Public clsClass
'properties, methods
End Class
And now some declarative snippits, followed by abstracts of what I think is
happening:
Dim objClass As clsClass
That sets up something like this in memory:
{objClass = Nothing} <- Can hold a memory address, but currently holds
Nothing so far
So as I understand it, this essentially creates something akin to a
"pointer". It cannot be treated like a C++ pointer, but it is something of
a pointer in the sense that it will "point" to memory.
objClass = New clsClass
Now it's set to an instance of an object, and so here's what I assume is now
going on in memory:
{@ address 1, clsClass instance} <- is now physically stored in the managed
memory area
{objClass = @1} <- now stores the memory address for lookup of the clsClass
instance
Dim objClass2 As clsClass = objClass
Because they are references, this is what I assume is happening after this
declaration:
{@ address 1, clsClass instance} <- still physically stored in the managed
memory area
{objClass = @1} <- still storing the memory address for lookup
{objClass2 = @1} <- now storing the same memory address for lookup as
objClass
Is this essentially correct? I still have only one actual instance, and two
smaller pointers (well, references) available for lookups against the same
instance. Let's add one more:
Dim objClass3 as New clsClass
{@ address 1, clsClass instance}
{objClass = @1}
{objClass2 = @1}
{@ address 2, clsClass instance}
{objClass3 = @2}
So now two instances, three references (two of which point to the same
instance). Now I start wandering out into stuff I'm not sure I understand,
for example a HashTable. Here's my guess:
Dim objHash as New HashTable:
{@ memory address 1, clsClass instance}
{objClass = 1}
{objClass2 = 1}
{@ memory address 2, clsClass instance}
{objClass3 = 2}
{@ memory address 3, HashTable instance}
{objHash = 3}
And then...
objHash.Add("A", objClass2)
{@ address 1, clsClass instance}
{objClass = @1}
{objClass2 = @1}
{@ address 2, clsClass instance}
{objClass3 = @2}
{@ address 3, HashTable instance}
{HashTable "A" = @1} <- ...something like that?
{objHash = @3}
Is the Hash table storing data in this fashion? Do I still have just one
instance of clsClass?
And finally does the garbage collector kill the instance for me once all the
references are gone?
objClass = Nothing
objClass2 = Nothing
{@ memory address 1, clsClass instance}
{@ memory address 2, clsClass instance}
{objClass3 = 2}
{@ memory address 3, HashTable instance}
{HashTable "A" = 1}
{objHash = 3}
Is the HashTable item "A" maintaining my instance @1?
objHash.Remove(objHash("A"))
[...GC runs]
{@ address 2, clsClass instance}
{objClass3 = @2}
{@ address 3, HashTable instance}
{objHash = @3}
I'm starting a project using lots and lots of classes in multiple 2D arrays
of hashtables and I want to make sure I have this stuff more or less correct
in my mind before I start allocating memory like gangbusters.