INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

reference types?

reference types?

(OP)
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

RE: reference types?

(OP)
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

RE: reference types?


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!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close