Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reference types?

Status
Not open for further replies.

mrmovie

Technical User
Oct 2, 2002
3,094
GB
Hi All, I am showing my age here...
I am creating a number of instances of a class (from a different module). at the point of creating the new instance of the class i am passing the New() Sub for the class some information to use / assign to the new instance of the class. I am struggling because all of the 16 instances of the class i am creating are all ending up references one set of Dictionaries, somewhere in memory. I want the each new instance of the Container class to have their .Children, .Index and .Technical dictionaries contain the information that was sent...not the very last set of information that the For Loop happened to contain. Here is what i have:

...Calling Module / Sub
... For Each aSomething In colSomething
'''build lTechnical and lIndex and lChildren local dictionaries...that aSomething has
Containers.Add(strRecordID, New Container(strRecordID, lTechnical, lIndex, lChildren))
... Next
'''later on i am getting all of the Container instances having the same Technical, Children and Index...based on the last aSomething
...End Calling Module / Sub

Public Class Container
Public Property ID As String
Public Property Children As Dictionary(Of String, String)
Public Property Index As Dictionary(Of String, String)
Public Property Technical As Dictionary(Of String, String)
Public Sub New(ByVal strID As String, ByVal lTechnical As Dictionary(Of String, String), ByVal lIndex As Dictionary(Of String, String), ByVal lChildren As Dictionary(Of String, String))
Me.ID = strID
'Me.Technical = New Dictionary(Of String, String)
'Me.Index = New Dictionary(Of String, String)
'Me.Children = New Dictionary(Of String, String)
Me.Technical = lTechnical
Me.Index = lIndex
Me.Children = lChildren
End Sub
End Class

I Hear, I Forget
I See, I Remember
I Do, I Understand

Ronald McDonald
 
I have ended doing this....which is clearly retarded!!!

Public Class Container
Public Property ID As String
Public Property Children As Dictionary(Of String, String)
Public Property Index As Dictionary(Of String, String)
Public Property Technical As Dictionary(Of String, String)
Public Sub New(ByVal strID As String, ByVal lTechnical As Dictionary(Of String, String), ByVal lIndex As Dictionary(Of String, String), ByVal lChildren As Dictionary(Of String, String))
Me.ID = strID
Me.Technical = New Dictionary(Of String, String)
Me.Index = New Dictionary(Of String, String)
Me.Children = New Dictionary(Of String, String)
For Each aPair As KeyValuePair(Of String, String) In lTechnical
Me.Technical.Add(aPair.Key, aPair.Value)
Next
For Each aPair As KeyValuePair(Of String, String) In lIndex
Me.Index.Add(aPair.Key, aPair.Value)
Next
For Each aPair As KeyValuePair(Of String, String) In lChildren
Me.Children.Add(aPair.Key, aPair.Value)
Next
'Me.Technical = lTechnical
'Me.Index = lIndex
'Me.Children = lChildren
End Sub
End Class

I Hear, I Forget
I See, I Remember
I Do, I Understand

Ronald McDonald
 

Value Types are stored in the Stack (for the most part; there are exceptions but they are not relevant here).

Reference Types are stored in the Heap.

When you access a Reference Type, you are actually accessing a Value Type variable *in the Stack* that points to the Reference Type's address in the Heap. Thus, when you pass that variable to your object's constructor you are just passing the variable that points to the Reference Type, which is why all of your objects end up pointing to the same Dictionary object.

For each object to have its own Dictionary, you need to do just what you did and set one up in the object's code. That or create separate Dictionary objects for each of your objects you want to instantiate and pass the Stack reference to the appropriate Dictionary to the appropriate object. Far more code and "moving parts" that way.

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top