How to create property for an Array? VB.NET

1

I have a question in Arrays, I do not know if I wrote right if what I'm referring to is really a property, I'll explain in detail what I want to do.

For example:

I want to create an array with multiple names =

dim nomes() as string

After this I wanted to make a property such as

nomes(0).middleName
nomes(0).FullName

Is this possible? How do I create these properties?

    
asked by anonymous 11.06.2017 / 20:05

1 answer

1

Not possible. The most you can do is create a class for this.

Something like

Imports System
Imports System.Collections
Imports System.Collections.Generic

Public Class Program
    Public Shared Sub Main()

        dim names As New List(Of Names)


        names.Add(New Names("Joaquim"))
        names.Add(New Names("Pedro", "Moraes"))

        names(0).LastName = "Barbosa"

        Console.WriteLine(names(0).FullName)
        Console.WriteLine(names(1).FullName)
    End Sub
End Class

Class Names

    Sub New(ByVal first As String)
        Me.m_FirstName = first
    End Sub

    Sub New(ByVal first As String, ByVal last As String)
        Me.m_FirstName = first
        Me.m_LastName = last
    End Sub


    Public Property FirstName() As String
        Get
            Return m_FirstName
        End Get
        Set
            m_FirstName = Value
        End Set
    End Property
    Private m_FirstName As String
    Public Property LastName() As String
        Get
            Return m_LastName
        End Get
        Set
            m_LastName = Value
        End Set
    End Property
    Private m_LastName As String
    Public ReadOnly Property FullName() As String
        Get
            Return Convert.ToString(FirstName & Convert.ToString(" ")) & LastName
        End Get
    End Property
End Class

See working in .NET Fiddle.

As I do not program VB.NET, there may be some better way to organize this sample code. Anyway, the idea is this, create a class and modify the properties of this class.

There is also an example in C #.

using System;

public class Program
{
    public static void Main()
    {
        var nomes = new Names[2]; 
        nomes[0] = new Names {  FirstName = "Pedro" };
        nomes[1] = new Names {  FirstName = "Joaquim", LastName = "Soares" } ;          
        nomes[0].LastName = "Barbosa";

        Console.WriteLine(nomes[0].FullName);
        Console.WriteLine(nomes[1].FullName);
    }
}

class Names
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string FullName { get {return FirstName + " " + LastName; } }
}

See working in .NET Fiddle.

    
11.06.2017 / 20:30