views:

251

answers:

3

Hi,

I want to create an in-memory object in VB.Net with multiple columns. What I am trying to do is create an index of some data. It will look like:

Row 1: 23 1 Row 2: 5 1 Row 3: 3 38 ...

I know I can use a rectangular array to do this, but I want to be able to use indexOf opearations on this object. Is there any such structure in VB.Net?

WT

A: 

If the number of cells in each row is constant and you don't need to grow or shrink the structure, then a simple two-dimensional array is probably the best choice, because its exposes the best possible locality characteristics. If it is not sorted, you can implement indexOf via a simple linear search.

Philipp
A: 

You can do this with a Dictionary.

reinierpost
A: 

Define a row class, and then create a list of rows, like so:

Class row
 Inherits Collections.ArrayList
End Class

Dim cols As New List(Of row)

Now you can access your objects using a x/y notation:

cols(0)(1)

Note this is just a simple example, your structure is uninitialized and untyped.

You can also Shadow the IndexOf function in your own class, for example finding the indexOf by an item's name:

Class col
 Inherits Generic.List(Of Object)
 Shadows Function IndexOf(ByVal itemName As String) As Integer
  Dim e As Enumerator = Me.GetEnumerator
  While e.MoveNext
   If CType(e.Current, myType).name = itemName Then
    Return e.Current
   End If
  End While
 End Function
End Class

You can then access it like so:

Private cols As New col
cols.IndexOf("lookingfor")
Wez