Create a new instance of a class that is a reference

  • Thread starter Jeremy Benenati
  • Start date
J

Jeremy Benenati

Hi all,

I am trying to make a class that when you create a 2nd instance the second instance will be a reference to the first.

Public Class ClassA
Private Shared tableList As Hashtable
Public myInt as integer = 0

Public Sub New()
MyBase.New()
InitializeValues()
End Sub
Private Function openTable() As Boolean
If (tableList Is Nothing) Then
tableList = New Hashtable()
End If

If (tableList.ContainsKey(TABLE)) Then
SetReference(Me, tableList(TABLE))
OpenCount += 1
Else
OpenCount += 1
tableList.Add(TABLE, Me)
End If
End Function
Public Shared Sub SetReference(ByRef obj As Object, ByRef newObj As Object)
obj = newObj
End Sub
End Class

Public Sub Test()
Dim myClass1 as new ClassA
Dim myClass2 as new ClassA

'set myInt = 200 for both
myClass1.myInt = 200
End Sub




Submitted via EggHeadCafe - Software Developer Portal of Choice
ASP.NET Cookies FAQ
http://www.eggheadcafe.com/tutorial...388-89e5-fce33d725aa7/aspnet-cookies-faq.aspx
 
A

Armin Zingler

Jeremy said:
Hi all,

I am trying to make a class that when you create a 2nd instance the second instance will be a reference to the first.

Public Class ClassA
Private Shared tableList As Hashtable
Public myInt as integer = 0

Public Sub New()
MyBase.New()
InitializeValues()
End Sub
Private Function openTable() As Boolean
If (tableList Is Nothing) Then
tableList = New Hashtable()
End If

If (tableList.ContainsKey(TABLE)) Then
SetReference(Me, tableList(TABLE))
OpenCount += 1
Else
OpenCount += 1
tableList.Add(TABLE, Me)
End If
End Function
Public Shared Sub SetReference(ByRef obj As Object, ByRef newObj As Object)
obj = newObj
End Sub
End Class

Public Sub Test()
Dim myClass1 as new ClassA
Dim myClass2 as new ClassA

'set myInt = 200 for both
myClass1.myInt = 200
End Sub

Even with looking at the code, I'm not sure what's your real intention. If it's what you wrote
above, it doesn't makes sense. Each instance is unique. If you have two instances, there are two
instances - surprise. You can make one instance point to another one, or you can have more
references pointing to the same object.

What if you create 3 instances?

If you only want to have one instance at all, you may consider using shared members only.

As "TABLE" is not declared anywhare, I suppose it is meant to be an argument of openTable.
Then I think, you want to have only one instance per TABLE. So maybe it's something like this:

Class classA
Private Shared tableList As New Hashtable

Public Shared Function openTable(ByVal Table As String) As classA

If tableList.ContainsKey(Table) Then
Return DirectCast(tableList.Item(Table), classA)
Else
Dim result = New classA
tableList.Add(Table, result)
Return result
End If

End Function
End Class
 
T

Tom Shelton

Hi all,

I am trying to make a class that when you create a 2nd instance the second instance will be a reference to the first.

Public Class ClassA
Private Shared tableList As Hashtable
Public myInt as integer = 0

Public Sub New()
MyBase.New()
InitializeValues()
End Sub
Private Function openTable() As Boolean
If (tableList Is Nothing) Then
tableList = New Hashtable()
End If

If (tableList.ContainsKey(TABLE)) Then
SetReference(Me, tableList(TABLE))
OpenCount += 1
Else
OpenCount += 1
tableList.Add(TABLE, Me)
End If
End Function
Public Shared Sub SetReference(ByRef obj As Object, ByRef newObj As Object)
obj = newObj
End Sub
End Class

Public Sub Test()
Dim myClass1 as new ClassA
Dim myClass2 as new ClassA

'set myInt = 200 for both
myClass1.myInt = 200
End Sub

I'm not sure I really understand what your after here... But, it sort of
sounds like a singleton pattern. The common way to do that would look
something like:

Public Class ClassA
Private Shared _instance As New ClassA()
Public myInt As Integer = 0

' constructor is private so you can't create
' outside references
Private Sub New()
InitalizeValues()
End Sub

Public Shared ReadOnly Property Instance()
Get
Return _instance
End Get
End Property
End Class

Public Sub Test
Dim myClass1 As ClassA = ClassA.Instance()
Dim myClass2 As ClassA = ClassA.Instance()

myClass1.myInt = 200
Console.WriteLine (myClass2.myInt)
End Sub
 
J

James Hahn

You do that when you instantiate the class, not when you create the class
design.

Public Sub Test()
Dim myClass1 as ClassA = New ClassA
Dim myClass2 as ClassA = myClass1

'set myInt = 200 for both
myClass1.myInt = 200
End Sub

in message news:[email protected]...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top