CDM
CDM

Reputation: 93

How can I access a class variable via an array in VB.NET?

If I have the following class and declaration:

Public Class objLocation
   Public SysLocationId As String
   Public NameFull As String
   Public LatRaw As String
   Public LongRaw As String
   Public Active As Integer
End Class

dim lLocation as new objLocation

I can access each variable thus lLocation.SysLocationId, etc. Is there an alternate way, so I can access each variable by index, so something like lLocation(0), lLocation(1), etc., which gives me the flexibility to compare to classes of the same type via a for next loop, or against other sources, like a datatable.

Upvotes: 0

Views: 3071

Answers (6)

Tush
Tush

Reputation: 163

This method I implemented in a public structure to return an array of string variables stored in a structure:

Public Shared Function returnArrayValues() As ArrayList

    Dim arrayOutput As New ArrayList()
    Dim objInstance As New LibertyPIMVaultDefaultCategories()
    Dim t As Type = objInstance.GetType()
    Dim arrayfinfo() As System.Reflection.FieldInfo = t.GetFields()
    For Each finfo As System.Reflection.FieldInfo In arrayfinfo
        Dim str As String = finfo.GetValue(objInstance)
        arrayOutput.Add(str)
    Next

    Return arrayOutput
End Function

Put it inside the structure or a class. Maybe this sample code helps.

Upvotes: 0

JaredPar
JaredPar

Reputation: 754655

There is no built-in langauge support for this. However you can simulate this by creating a default indexer property on the class

Public Class objLocation
  ...
Default Public ReadOnly Property Indexer(ByVal index As Integer)
    Get
        Select Case index
            Case 0
                Return SysLocationId
            Case 1
                Return NameFull
            Case 2
                Return LatRaw
            Case 3
                Return LongRaw
            Case 4
                Return Active
            Case Else
                Throw New ArgumentException
        End Select
    End Get
  End Property

Then you can use it as follows

Dim x As objLocation = GetObjLocation
Dim latRaw = x(2)

Upvotes: 0

Daniel Brückner
Daniel Brückner

Reputation: 59645

You can do that as follows. It is C# and something is a bit different with using indexers in VB, but you should absolutly be able to get it working in VB.

public class ObjLocation
{
   private String[] Properties = new String[5];

   public const Int32 IndexSysLocationId = 0;
   public const Int32 IndexNameFull = 1;
   public const Int32 IndexLatRaw = 2;
   public const Int32 IndexLongRaw = 3;
   public const Int32 IndexActive = 4;

   // Repeat this for all properties
   public String SysLocationId
   {
      get { return this.Properties[ObjLocation.IndexSysLocationId]; }
      set { this.Properties[ObjLocation.IndexSysLocationId] = value; }
   }         

   public String this[Int32 index]
   {
      get { return this.Properties[index]; }
      set { this.Properties[index] = value; }
   }
}

Now you have the object with the properties as before, but stored in an array and you can also access them through an indexer.

Upvotes: 0

Welbog
Welbog

Reputation: 60398

If your goal is comparison, usually what you'll do is implement the IComparable interface or overload the >, < operators (if an ordering is needed) or just the = operator (if equivalence is needed).

You just write one function in one location and invoke that function whenever you need to do your comparison. The same goes for comparing to objects stored in a database. Where you put these functions depends on your application architecture, but for the object-object comparison you can have it as part of the objLocation class itself.

Upvotes: 2

Micky McQuade
Micky McQuade

Reputation: 1858

Are you looking for a List:

Dim LocationList As List<objLocation>;

For Each loc As objLocation In LocationList
    loc.whatever
Next

or to use the index:

For i = 0 To LocationList.Length - 1
    LocationList(i).whatever
Next

sorry, if the VB syntax isn't right...I've been doing C# lately and no VB

Upvotes: 0

casperOne
casperOne

Reputation: 74530

No, you can not do this outright.

You have to use reflection to get the properties, but you have to be aware that there is no guarantee on the order of the properties returned (which is important if you want to index them numerically).

Because of that, you will have to keep the sort order consistent when working with the properties (and indexes).

Upvotes: 0

Related Questions