0

Lets say I have a few arrays

dim names(3) as String
names(0) = "Name1"
names(1) = "Name2"
names(2) = "Name3"
names(3) = "Name4"

dim descriptions(2) as String
descriptions(0) = "blah1"
descriptions(1) = "blah2"
descriptions(2) = "blah3"

dim people(1) as Array
people(0) = names
people(1) = descriptions

How would I access those out of people?? Like this?: people(0)(0) or this: (people(0))(0)

which should be "Name1" When I try to get names out of people I get an error...

1 Answer 1

2

It would be:

Dim name1 As String = people(0)(0)

However, this structure is very confusing. You have to remember what type of thing is at each index in each array, and there's no way to reliably relate a name to a description.

It would be better to build a Person class with Name and Description properties, then, build a list of new Person objects, even if some of their properties are Nothing (null). Using jagged arrays isn't very maintainable and even your simple example above is hard to swallow.

Here's how I would approach this (with some comments thrown in for explanation). It's more code than your example, but it will be far more maintainable

Public Class Person

    ' Private backing fields
    Private _name As String
    Private _description As String

    ' Default constructor
    Public Sub New()
    End Sub

    ' Overloaded constructor that takes a name and optionally a description
    Public Sub New(ByVal name As String, _
                   Optional ByVal description As String = Nothing)
        Me.Name = name
        Me.Description = description
    End Sub

    ' A property (getter & setter) for Name
    Public Property Name As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property

    ' A property (getter & setter) for Description
    Public Property Description As String
        Get
            Return _description
        End Get
        Set(ByVal value As String)
            _description = value
        End Set
    End Property

End Class

And create a new list of them:

Dim people As New List(Of Person)

people.Add(new Person("Name1", "Description1"))
people.Add(new Person("Name2")) ' Description is optional
people.Add(new Person("Name3", "Description2"))

Then you can use the various methods provided by the List(Of T) class to find a manipulate items in the list without ever needing to know an index.

Edit: If you want multiple descriptions for a person, your code might look like:

Public Class Person

    ' Private backing fields
    Private _name As String
    Private ReadOnly _descriptions As List(Of String) = New List(Of String)

    ' Default constructor
    Public Sub New()
    End Sub

    ' Overloaded constructor that takes a name and optionally a description
    Public Sub New(ByVal name As String, _
                   Optional ByVal descriptions As IEnumerable(Of String) = Nothing)
        Me.Name = name
        If Not descriptions Is Nothing Then
            Me.Descriptions.AddRange(descriptions)
        End If
    End Sub

    ' A property (getter & setter) for Name
    Public Property Name As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property

    ' A property (getter) for Descriptions
    Public ReadOnly Property Descriptions As List(Of String)
        Get
            Return _descriptions
        End Get
    End Property

End Class

And create a new list of them:

Dim people As New List(Of Person)

' Because the constructor takes any `IEnumerable`, you have options
' as to how you add descriptions

people.Add(new Person("Name1", New String() { "Desc1", "Desc2" }))

' or

Dim person1 As New Person("Name1")
person1.Descriptions.Add("Desc1")
person1.Descriptions.Add("Desc2")

people.Add(person1)

There are several other ways to do it but these two are simple.

Sign up to request clarification or add additional context in comments.

3 Comments

What if I tried using a list as one of the parameters. Let's say the description is a list of strings, not a string. 'Dim descriptions As List(Of String)'. Would all of the values added to that list get passed into the value of the property of the instance of the object if I just did: 'people.Add(new Person("name1", descriptions)'
@user2922390: I added a separate example if you want multiple descriptions for a person. You would need to change the underlying Description property to also be a List(Of String) instead of just a String.
I did that but if I set it to description, then change description, then access the description in the list the list will have changed with the description.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.