help with multi-dimensional arraylist

  • Thread starter Thread starter Awrightus
  • Start date Start date
A

Awrightus

I'm working on a vb.net project (2008) I have and I'm trying to figure
out how I can create a multi-dimensional array, arraylist, dictionary,
etc, whatever will allow me to do what I want to do. I have data like
the following:

Blue, Green,
Black, Orange
Yellow, Red

Essentially value/pair stuff. I already have it in a dictionary, but
it doesn't look like a dictionary has an index value for each of the
keys. I want to be able to reference any of these "rows" by their
index value and the position of the item in the row (or column). So as
an example, I might want to know what the 2nd index row is and the
value of the 1 index inside that row (or the column). That would be
"Red". Is there a way of doing this in vb.net? Not looking for code,
just the right type of object/function to us. Thanks.

Steve
 
I'm working on a vb.net project (2008) I have and I'm trying to figure
out how I can create a multi-dimensional array, arraylist, dictionary,
etc, whatever will allow me to do what I want to do. I have data like
the following:

Blue, Green,
Black, Orange
Yellow, Red

Essentially value/pair stuff. I already have it in a dictionary, but
it doesn't look like a dictionary has an index value for each of the
keys. I want to be able to reference any of these "rows" by their
index value and the position of the item in the row (or column). So as
an example, I might want to know what the 2nd index row is and the
value of the 1 index inside that row (or the column). That would be
"Red". Is there a way of doing this in vb.net? Not looking for code,
just the right type of object/function to us. Thanks.

Steve

How much data is typically going to be in this collection?
 
Eh, could be around 50,000 or so.

Is the problem you don't know how many, and so you cannot declare as:

dim data0 as string (,)

If so, then perhaps one of the following:

dim data1 as List(of List(of string))
dim data2 as List(of string ())


You address your item as:
data0(2,1) or data1(2)(1) or data2(2)(1)
 
Is the problem you don't know how many, and so you cannot declare as:

dim data0 as string (,)

If so, then perhaps one of the following:

dim data1 as List(of List(of string))
dim data2 as List(of string ())

You address your item as:
data0(2,1) or data1(2)(1) or data2(2)(1)

Just thought of something significant. Every element might not be of
the same dimension. Something like this.

Black, Red
Blue, Purple
Orange
Yellow, White
Indigo

Could the second row perhaps be null?
 
I'm working on a vb.net project (2008) I have and I'm trying to figure
out how I can create a multi-dimensional array, arraylist, dictionary,
etc, whatever will allow me to do what I want to do. I have data like
the following:

Blue, Green,
Black, Orange
Yellow, Red

Essentially value/pair stuff. I already have it in a dictionary, but
it doesn't look like a dictionary has an index value for each of the
keys. I want to be able to reference any of these "rows" by their
index value and the position of the item in the row (or column). So as
an example, I might want to know what the 2nd index row is and the
value of the 1 index inside that row (or the column). That would be
"Red". Is there a way of doing this in vb.net? Not looking for code,
just the right type of object/function to us. Thanks.

Steve

You might want to play with the performance, but here is a simple custom
collection that lets you access by index or by string. I've also implemented
contains, etc... So, take a look see if it does what you want - if not,
should at least give you a start :) as you could just make the type of the
custom collection a 2 dimmensional string array :)

Option Strict On
Option Explicit On

Imports System.Collections.ObjectModel

Module Module1

Sub Main()
Dim pairs As New ColorPairList()
pairs.Add(New ColorPair("Yellow", "Red"))
pairs.Add(New ColorPair("Blue", "Orange"))

If Not pairs.Contains("yellow") Then
pairs.Add(New ColorPair("Yellow", "Red"))
End If

Dim p1 As ColorPair = pairs("yellow")
Dim p2 As ColorPair = pairs(1)

Console.WriteLine(p1.Color1)
Console.WriteLine(p2.Color1)
End Sub

Class ColorPair
Private _color1 As String
Private _color2 As String

Public Sub New()
'
End Sub

Public Sub New(ByVal color1 As String, ByVal color2 As String)
_color1 = color1
_color2 = color2
End Sub

Public Property Color1() As String
Get
Return _color1
End Get
Set(ByVal value As String)
_color1 = value
End Set
End Property

Public Property Color2() As String
Get
Return _color2
End Get
Set(ByVal value As String)
_color2 = value
End Set
End Property

Public Overrides Function Equals(ByVal obj As Object) As Boolean
Dim other As ColorPair = TryCast(obj, ColorPair)
If other IsNot Nothing Then
Return StringComparer.OrdinalIgnoreCase.Compare(Color1, other.Color1) = 0
Else
Dim str As String = TryCast(obj, String)
If str IsNot Nothing Then
Return StringComparer.OrdinalIgnoreCase.Compare(Color1, str) = 0
End If
End If

Return False
End Function

Public Overrides Function GetHashCode() As Integer
Return Color1.GetHashCode()
End Function

Public Overrides Function ToString() As String
Return String.Format("{0}, {1}", _color1, _color2)
End Function
End Class

Class ColorPairList
Inherits Collection(Of ColorPair)

' this is not case senstive...
Private _dictionary As New Dictionary(Of String, ColorPair)(StringComparer.OrdinalIgnoreCase)

Default Public Overloads ReadOnly Property Item(ByVal color As String) As ColorPair
Get
Dim pair As ColorPair = Nothing
Return If(_dictionary.TryGetValue(color, pair), pair, Nothing)
End Get
End Property

Public Overloads Function Contains(ByRef color As String) As Boolean
Return _dictionary.ContainsKey(color)
End Function

Protected Overrides Sub InsertItem(ByVal index As Integer, ByVal item As ColorPair)

' add, let it throw exception :)
_dictionary.Add(item.Color1, item)
MyBase.InsertItem(index, item)

End Sub

Protected Overrides Sub RemoveItem(ByVal index As Integer)
_dictionary.Remove(Items(index).Color1)
MyBase.RemoveItem(index)
End Sub

Protected Overrides Sub ClearItems()
_dictionary.Clear()
MyBase.ClearItems()
End Sub
End Class
End Module
 
You might want to play with the performance, but here is a simple custom
collection that lets you access by index or by string.  I've also implemented
contains, etc...  So, take a look see if it does what you want - if not,
should at least give you a start :) as you could just make the type of the
custom collection a 2 dimmensional string array :)

Option Strict On
Option Explicit On

Imports System.Collections.ObjectModel

Module Module1

    Sub Main()
        Dim pairs As New ColorPairList()
        pairs.Add(New ColorPair("Yellow", "Red"))
        pairs.Add(New ColorPair("Blue", "Orange"))

        If Not pairs.Contains("yellow") Then
            pairs.Add(New ColorPair("Yellow", "Red"))
        End If

        Dim p1 As ColorPair = pairs("yellow")
        Dim p2 As ColorPair = pairs(1)

        Console.WriteLine(p1.Color1)
        Console.WriteLine(p2.Color1)
    End Sub

    Class ColorPair
        Private _color1 As String
        Private _color2 As String

        Public Sub New()
            '
        End Sub

        Public Sub New(ByVal color1 As String, ByVal color2 As String)
            _color1 = color1
            _color2 = color2
        End Sub

        Public Property Color1() As String
            Get
                Return _color1
            End Get
            Set(ByVal value As String)
                _color1 = value
            End Set
        End Property

        Public Property Color2() As String
            Get
                Return _color2
            End Get
            Set(ByVal value As String)
                _color2 = value
            End Set
        End Property

        Public Overrides Function Equals(ByVal obj As Object) As Boolean
            Dim other As ColorPair = TryCast(obj, ColorPair)
            If other IsNot Nothing Then
                Return StringComparer.OrdinalIgnoreCase.Compare(Color1, other.Color1) = 0
            Else
                Dim str As String = TryCast(obj, String)
                If str IsNot Nothing Then
                    Return StringComparer.OrdinalIgnoreCase.Compare(Color1, str) = 0
                End If
            End If

            Return False
        End Function

        Public Overrides Function GetHashCode() As Integer
            Return Color1.GetHashCode()
        End Function

        Public Overrides Function ToString() As String
            Return String.Format("{0}, {1}", _color1, _color2)
        End Function
    End Class

    Class ColorPairList
        Inherits Collection(Of ColorPair)

        ' this is not case senstive...
        Private _dictionary As New Dictionary(Of String, ColorPair)(StringComparer.OrdinalIgnoreCase)

        Default Public Overloads ReadOnly Property Item(ByVal color As String) As ColorPair
            Get
                Dim pair As ColorPair = Nothing
                Return If(_dictionary.TryGetValue(color, pair), pair, Nothing)
            End Get
        End Property

        Public Overloads Function Contains(ByRef color As String)As Boolean
            Return _dictionary.ContainsKey(color)
        End Function

        Protected Overrides Sub InsertItem(ByVal index As Integer, ByVal item As ColorPair)

            ' add, let it throw exception :)
            _dictionary.Add(item.Color1, item)
            MyBase.InsertItem(index, item)

        End Sub

        Protected Overrides Sub RemoveItem(ByVal index As Integer)
            _dictionary.Remove(Items(index).Color1)
            MyBase.RemoveItem(index)
        End Sub

        Protected Overrides Sub ClearItems()
            _dictionary.Clear()
            MyBase.ClearItems()
        End Sub
    End Class
End Module

Thanks. This should give me something to work with.
 
Just thought of something significant. Every element might not be of
the same dimension. Something like this.

Black, Red
Blue, Purple
Orange
Yellow, White
Indigo

Could the second row perhaps be null?

I would then go with List(of List(of string)) or with a variation of
Tom's code that adds the capability to have a null second pair string.
 
I would then go with List(of List(of string)) or with a variation of
Tom's code that adds the capability to have a null second pair string.

Mine actaully already allows it - the second string in the pair is never used
in any of the comparisons or as a key. Only the first string is used.
 
Mine actaully already allows it - the second string in the pair is never used
in any of the comparisons or as a key. Only the first string is used.

Your right, sorry. I must have misread something there, as I thought it
required a second string.
 
Back
Top