tags:

views:

355

answers:

5

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.

A: 

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).

casperOne
A: 

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

Micky McQuade
A: 

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.

Daniel Brückner
+2  A: 

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.

Welbog
A: 

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)
JaredPar
All of these were great answers, but I think this one is the best fit for what I want to do, namely cycle through a classes variables by index, get the values and then assign them to a database.
CDM