How to create an array-class ? Please, some help with my code

B

Big Charles

Hello,

I would like to create an array-class to be able to call like:

Dim oMyCar as New MyCar

' After initializing oMyCar, the object has to be like:
oMyCar(0).Brand
oMyCar(0).Wheels.NumberOfWheels
oMyCar(0).Wheels.ColorOfWheels

oMyCar(1).Brand
oMyCar(1).Wheels.NumberOfWheels
oMyCar(1).Wheels.ColorOfWheels

The object has to be an array list itself with properties like Brand
and sub-objects like Wheels.

I think I should create the class like this:

Public Class MyCar

Inherits CollectionBase

Public Wheels As MyWheels
Private mBrand As String

Public Sub New()

Get_Data()

End Sub
Private Function Get_Data()

' DataReader dr has been populated

If dr.HasRows Then
While dr.Read

' I am not sure how to create the array object from here
Me.Add()
mBrand = dr.Item("Brand").ToString()

End While

Wheels = New MyWheels(mBrand)
End If

dr.Close()
End Function

Sub Add(ByVal il As Object)
Me.List.Add(il)
End Sub

Public Property Brand() As String
Get
Return mDescripcion
End Get
Set(ByVal Value As String)
mDescripcion = Value
End Set
End Property
End Class
 
S

sloan

I have objects/object collections ( CollectionBase ) here:

5/24/2006
Custom Objects/Collections and Tiered Development

http://sholliday.spaces.live.com/blog/


You don't want ot have a method like:
Private Function Get_Data()

which mixes objects and database access.

Car should be an object
CarCollection should be an object
Wheel should be an object
WheelCollection should be an object

Car should have Wheels as a property.

Don't mix objects and collections with database access.

Create a Manager or Controller class

CarController
public static(shared) CarCollection GetCars

//here is where you create a new CarCollection, and looop over your
datareader to put new cars into the car collection




....................


I have Customer(s) and Order(s) objects, and collections are the url above.
 
T

Thomas Hansen

Hello,

I would like to create an array-class to be able to call like:

Dim oMyCar as New MyCar

' After initializing oMyCar, the object has to be like:
oMyCar(0).Brand
[snip]

I am not sure how the VB syntax is, but the C# syntax is basically
like thi:

public class MYarray
{
public object this[int idx] { return /*something*/; }
}


In addition you'd probably want to implement a couple of the
IEnumerable interfaces in addition to read up on generics to get it
strongly typed on the content...

Roughly like this:

public class MYArray<T> : IEnumerable<T>
{
public T this[int idx] { return /*something*/; }
}


I haven't ran the code through a compiler, but I think you get the
point...

..t
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top