Array of Value/Ref type

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • wg

    #1

    Array of Value/Ref type

    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


  • Armin Zingler

    #2
    Re: Array of Value/Ref type

    "wg" <wg@hotmail.com > schrieb[color=blue]
    > I am attempting to create an array of value types but seem to be
    > haveing a problem referencing it.[/color]

    Where are the value types in your example?
    [color=blue]
    > 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.[/color]

    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

    Comment

    • Mattias Sjögren

      #3
      Re: Array of Value/Ref type

      [color=blue]
      >I am attempting to create an array of value types but seem to be haveing a
      >problem referencing it.[/color]

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

      [color=blue]
      >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.[/color]

      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.

      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: Array of Value/Ref type

        "wg" <wg@hotmail.com > schrieb:[color=blue]
        > 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.[/color]

        \\\
        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/>

        Comment

        • wg

          #5
          Re: Array of Value/Ref type

          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


          "wg" <wg@hotmail.com > wrote in message
          news:Kvvwe.4074 $ho.1813@bignew s6.bellsouth.ne t...[color=blue]
          >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
          >
          >[/color]


          Comment

          Working...