Array of Value/Ref type

  • Thread starter Thread starter wg
  • Start date Start date
W

wg

I am attempting to create an array of value types but seem to be haveing a
problem referencing it.

I have created a class
Public Class Tags
Private _Name As String
Private _Address As String

Public Sub New(ByVal Name As String, ByVal Address As String)
_Name = Name
_Address = Address
End Sub
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal Value As String)
_Name = Value
End Set
End Property
Public Property Address() As String
Get
Return _Address
End Get
Set(ByVal Value As String)
_Address = Value
End Set
End Property
End Class

The question is how do I create and initialize an array to reference these
objects. For instance I will populate the array myarray(x).Name = xxx, etc.
Then update based on user inputs later.

Thanks

wg
 
wg said:
I am attempting to create an array of value types but seem to be
haveing a problem referencing it.

Where are the value types in your example?
I have created a class
Public Class Tags
Private _Name As String
Private _Address As String

Public Sub New(ByVal Name As String, ByVal Address As String)
_Name = Name
_Address = Address
End Sub
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal Value As String)
_Name = Value
End Set
End Property
Public Property Address() As String
Get
Return _Address
End Get
Set(ByVal Value As String)
_Address = Value
End Set
End Property
End Class

The question is how do I create and initialize an array to reference
these objects. For instance I will populate the array
myarray(x).Name = xxx, etc. Then update based on user inputs later.

dim Tags(1) as tags

tags(0) = new tags
tags(1) = new tags

tags(0).name = "name1"
tags(1).name = "name2"

Does this help?

Armin
 
I am attempting to create an array of value types but seem to be haveing a
problem referencing it.

I don't see any value types used in your code.

The question is how do I create and initialize an array to reference these
objects. For instance I will populate the array myarray(x).Name = xxx, etc.

Dim myarray(5) As Tags
myarray(2) = New Tags("John", "John's Address")
myarray(2).Name = "xxx"



Mattias
 
wg said:
I have created a class
Public Class Tags
Private _Name As String
Private _Address As String

Public Sub New(ByVal Name As String, ByVal Address As String)
_Name = Name
_Address = Address
End Sub
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal Value As String)
_Name = Value
End Set
End Property
Public Property Address() As String
Get
Return _Address
End Get
Set(ByVal Value As String)
_Address = Value
End Set
End Property
End Class

The question is how do I create and initialize an array to reference these
objects. For instance I will populate the array myarray(x).Name = xxx,
etc. Then update based on user inputs later.

\\\
Dim a(9) As Tags
For i As Integer = 0 To a.Length - 1
a(i) = New Tags()
Next i
....
a(i).Name = "Bla"
///

Note that class types are reference types. When using a structure instead
of the value type some additional code is necessary.
 
Guess I am not sure the difference between a value type and a reference
type. But between the three replys I was able to get it to work. Thanks for
the help.


wg
 
Back
Top