473,507 Members | 11,372 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

class working as array

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
Nov 21 '05 #1
7 1542
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
Nov 21 '05 #2
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)
///
Nov 21 '05 #3
| 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
Nov 21 '05 #4
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

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

Nov 21 '05 #5
| 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.
Nov 21 '05 #6
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....
Nov 21 '05 #7
> 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
Nov 21 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2036
by: Johan Bergman | last post by:
Hi, Maybe someone can help me with this one. The following describes a somewhat simplified version of my problem, but I think it will be sufficient. I want to use class factories (virtual...
3
2305
by: matthurne | last post by:
I'm doing a chapter 12 exercise from Accelerated C++ ... writing a string-like class which stores its data in a low-level way. My class, called Str, uses a char array and length variable. I've...
6
1433
by: thomasp | last post by:
For those who gave advice on the shortfalls of my first attempt at writing a vb.net class, Thank You. I hope that I was able to apply some of your advice to this larger atempt. At first I didn' t...
3
3739
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
3
6324
by: raylopez99 | last post by:
Below is my problem. I've narrowed it down to one thing: my unfamiliarity on how class instances are instantiated in an array. This is because the "un-array" / "non-array" version of the program...
0
7221
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7109
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7313
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7481
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
3190
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1537
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
411
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.