Home All Groups Group Topic Archive Search About

Array of Value/Ref type

Author
29 Jun 2005 11:23 AM
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

Author
29 Jun 2005 12:20 PM
Armin Zingler
"wg" <w*@hotmail.com> schrieb
> 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?

Show quoteHide quote
> 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
Author
29 Jun 2005 12:21 PM
Mattias Sjögren
>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

--
Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Author
29 Jun 2005 12:30 PM
Herfried K. Wagner [MVP]
Show quote Hide quote
"wg" <w*@hotmail.com> schrieb:
> 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.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
30 Jun 2005 12:39 AM
wg
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


Show quoteHide quote
"wg" <w*@hotmail.com> wrote in message
news:Kvvwe.4074$ho.1813@bignews6.bellsouth.net...
>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
>
>