class working as array

  • Thread starter Thread starter Peter
  • Start date Start date
P

Peter

I want to create a multidemensional arraylist. Seeing as they don't
exist I was wondering if there is a way to create a class that works
like one.

I basically want to use it like this

Dim MyArray as classNewArray

MyArray.add("Data1", "Data2")
or the looping claus....

It set it to grow automatically on both demensions.

then the corresponding:

MyArray.clear

MyArray.Get(0).upperbound and MyArray.Get(1)

Will this work?

-Peter
 
maybe something like this would work (modify the item class to suit your
needs and fix the line wrapping...there's enough here to give you and idea):


Public Class Item

#Region " variables "

Private myKey As String
Public myText As String

#End Region

#Region " properties "

Public ReadOnly Property Key() As String
Get
Return myKey
End Get
End Property

Public ReadOnly Property Text() As String
Get
Return myText
End Get
End Property

#End Region

#Region " methods "

Public Sub New(Optional ByVal Key As String = Nothing, Optional ByVal
Text As String = Nothing)
If Key Is Nothing Then Key = ""
If Text Is Nothing Then Text = ""
If Key.Trim = "" Then Key = ""
If Text.Trim = "" Then Text = ""
myKey = Key
myText = Text
End Sub

Public Overrides Function ToString() As String
Dim serialized As String = myKey
If Not myKey = "" Then serialized = myKey
If Not serialized = "" AndAlso Not myText = "" Then serialized &= " "
If Not myText = "" Then serialized &= myText
Return serialized
End Function

#End Region

End Class

Public Class ItemArray

#Region " interfaces "

Implements IEnumerable

#End Region

#Region " events "

Public Event ItemAdded(ByVal Item As Item)
Public Event ItemsCopied()
Public Event ItemsCleared()
Public Event ItemRemoved()

#End Region

#Region " variables "

Private myItemArray() As Item

#End Region

#Region " properties "

Public ReadOnly Property Count() As Integer
Get
If myItemArray Is Nothing Then Return 0
Return myItemArray.Length
End Get
End Property

Default Public Overloads ReadOnly Property Item(ByVal Id As String) As
Item
Get
If myItemArray Is Nothing Then Return Nothing
Dim i As Item
For Each i In myItemArray
If i.Key = Id Then Return i
Next
Return Nothing
End Get
End Property

Default Public Overloads ReadOnly Property Item(ByVal Index As Integer)
As Item
Get
On Error Resume Next
If Index < 0 Then Return Nothing
If Index > myItemArray.GetUpperBound(0) Then Return Nothing
Return myItemArray(Index)
End Get
End Property

#End Region

#Region " methods "

Public Overloads Sub Add(ByVal Item As Item)
Dim index As Integer = 0
If Not myItemArray Is Nothing Then index = myItemArray.Length
ReDim Preserve myItemArray(index)
myItemArray(index) = Item
RaiseEvent ItemAdded(Item)
End Sub

Public Overloads Sub Add(ByVal Text As String)
Dim index As Integer = 0
If Not myItemArray Is Nothing Then index = myItemArray.Length
ReDim Preserve myItemArray(index)
Dim item As New item(Text:=Text)
myItemArray(index) = Item
RaiseEvent ItemAdded(Item)
End Sub

Public Overloads Function Copy() As ItemArray
Dim destination As New ItemArray
If myItemArray Is Nothing Then Return destination
Dim item As item
For Each item In myItemArray
destination.Add(item)
Next
Return destination
End Function

Public Overloads Sub Copy(ByVal Source As ItemArray)
Dim item As item
For Each item In Source
Me.Add(item)
Next
RaiseEvent ItemsCopied()
End Sub

Public Sub Clear()
myItemArray = Nothing
RaiseEvent ItemsCleared()
End Sub

Public Function GetEnumerator() As IEnumerator Implements
IEnumerable.GetEnumerator
Return New ItemArrayEnumerator(myItemArray)
End Function

Public Overloads Sub Remove(ByVal Key As String)
If myItemArray Is Nothing Then Return
Dim index As Integer
Dim item As item
Dim itemIndex As Integer = -1
For Each item In myItemArray
If item.Key = Key Then
itemIndex = index
Exit For
End If
index += 1
Next
If itemIndex = -1 Then Return
If itemIndex > myItemArray.GetUpperBound(0) Then Return
Remove(itemIndex)
End Sub

Public Overloads Sub Remove(ByVal Index As Integer)
If myItemArray Is Nothing Then Return
If Index = -1 OrElse Index > myItemArray.GetUpperBound(0) Then Return
If Index < myItemArray.GetUpperBound(0) Then
Array.Copy(myItemArray, Index + 1, myItemArray, Index,
myItemArray.GetUpperBound(0) - 1)
End If
Array.Clear(myItemArray, myItemArray.GetUpperBound(0), 1)
RaiseEvent ItemRemoved()
End Sub

#End Region

#Region " internal classes "

Private Class ItemArrayEnumerator

#Region " interfaces "

Implements IEnumerator

#End Region

#Region " variables "

Private myItemArray() As Item
Private myItemIndex As Integer = -1

#End Region

#Region " properties "

Public ReadOnly Property Current() As Object Implements
System.Collections.IEnumerator.Current
Get
Return DirectCast(myItemArray(myItemIndex), Item)
End Get
End Property

#End Region

#Region " methods "

Public Sub New(ByVal Items As Item())
myItemArray = Items
End Sub

Public Function MoveNext() As Boolean Implements
System.Collections.IEnumerator.MoveNext
myItemIndex += 1
If myItemArray Is Nothing Then Return False
Return myItemIndex < myItemArray.Length
End Function

Public Sub Reset() Implements System.Collections.IEnumerator.Reset
myItemIndex = -1
End Sub

#End Region

End Class

#End Region

End Class
 
Peter,
I want to create a multidemensional arraylist. Seeing as they don't
exist

Where you saw that, I just showed a sample to Steve and this is a very
simplefied version of that

Where did you read that they do not exist, you can make multidimensional
arrays of every IList array.

Cor

\\\
Dim x As New ArrayList
For i As Integer = 0 To 9
Dim y As New ArrayList
For j As Integer = 0 To 4
y.Add("")
Next
x.Add(y)
Next
DirectCast(x(2), ArrayList)(2) = "Steve"
MessageBox.Show(DirectCast(x(2), ArrayList)(2).ToString)
///
 
| Where did you read that they do not exist, you can make multidimensional
| arrays of every IList array.

there are a bunch of interfaces out there for that...hash tables, boolean,
list, dictionary, etc. sorry for the overkill on the example but i thought
it would give the op a good idea of how to create a strong-typed enumerable
class of anything...minus the, most-of-the-time, additional interfaces that
come w/ the built-in support...and their scope access to the encapsulted
objects.

anyway, hth somebody.

steve
 
Steve,

Did I say that there is something wrong with your help?

Nice is the only thing I can say.

However I am curious why Peter is saying that an arraylist from an arraylist
is impossible.
As you showed there are a lot of samples using IList arrays.

:-)

Cor

..
 
| Did I say that there is something wrong with your help?


no, not at all. i wasn't meaning to address that...just to explain why my
help was so plentyful with code ;^)

| Nice is the only thing I can say.

cor, you are always nice and i've found your posts now and in the past to be
very helpful.

| However I am curious why Peter is saying that an arraylist from an
arraylist
| is impossible.
| As you showed there are a lot of samples using IList arrays.

i was curious about that too. probably op hasn't investigated the topic and
this was his first stop/source.

cheers cor.
 
Rediming:

From what I can see this is an arraylist within an arraylist. This
was not natively a multidemional arraylist? That must be what the
author ment.

IE. Dim myArray(,) <--- is natively a multidemensional array

Dim myMultPartOne as new arraylist
Dim myMultPartTwo as new arraylist

....add in data to PartTwo...

myMultPartOne.add(myMultPartTwo) <----Now is multidemensional

?

Is that the deal? Thanks for explaining this....
 
Rediming:
From what I can see this is an arraylist within an arraylist. This
was not natively a multidemional arraylist? That must be what the
author ment.

IE. Dim myArray(,) <--- is natively a multidemensional array

Dim myMultPartOne as new arraylist
Dim myMultPartTwo as new arraylist

...add in data to PartTwo...

myMultPartOne.add(myMultPartTwo) <----Now is multidemensional
Yes and that is endless, can be multi multi dimensional.

Cor
 
Back
Top