473,774 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pointer / reference clarification

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.
Nov 21 '05 #1
2 1127
Workgroups,

It looks to me like you are interpreting this correctly.

Kerry Moorman
"Workgroups " wrote:
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.

Nov 21 '05 #2
Sounds about right. In VB, we deal in "Value types" and "Reference types".
Value types are the basic types that hold their data within their own memory
allocation. Numeric types, booleans, chars, dates are all value types.
Reference types contain a pointer to another memory location that holds the
data. Arrays, strings, class types, etc. are all reference types. Here's
MSKB discussion on this:
http://msdn.microsoft.com/library/de...ueRefTypes.asp

"Workgroups " <no*****@domain less.com> wrote in message
news:Us******** ************@sp eakeasy.net...
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.

Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
1842
by: Torsten Mohr | last post by:
Hi, i'd like to pass a reference or a pointer to an object to a function. The function should then change the object and the changes should be visible in the calling function. In perl this would be something like: sub func {
3
1420
by: ma740988 | last post by:
Consider the 'C' source. void myDoorBellISR(starLinkDevice *slDevice, U32 doorBellVal) { doorBellDetected = doorBellVal; } void slRcv() { starLinkOpenStruct myOpenStruct;
110
9959
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object must be an object instead of
13
2682
by: al.cpwn | last post by:
I get that these two are different int* get() { static int m; return &m; } int& get() {
51
4473
by: Kuku | last post by:
What is the difference between a reference and a pointer?
33
5083
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the actual counting. Here is the latter's definition: // --- Begin ReferenceCountable.h ---------- class ReferenceCountable
2
35622
weaknessforcats
by: weaknessforcats | last post by:
Handle Classes Handle classes, also called Envelope or Cheshire Cat classes, are part of the Bridge design pattern. The objective of the Bridge pattern is to separate the abstraction from the implementation so the two can vary independently. Handle classes usually contain a pointer to the object implementation. The Handle object is used rather than the implemented object. This leaves the implemented object free to change without affecting...
41
3686
by: Summercool | last post by:
Can we confirm the following? also someone said, Java also has "reference" like in C++, which is an "implicit pointer": Pointer and Reference --------------------- I am starting to see what pointer and reference are and how they relate to each other.
5
449
by: George2 | last post by:
Hello everyone, This is my understanding of non-const reference, const reference and their relationships with lvalue/rvalue. Please help to review whether it is correct and feel free to correct me. Thanks. 1. A const reference can be binded to a rvalue, for example, a temporary object. And the "life" of the temporary object is guaranteed to be extended and we can safely operate through the const-reference.
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10267
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10040
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9914
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7463
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5355
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3611
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.