469,645 Members | 1,941 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,645 developers. It's quick & easy.

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

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

Feb 2 '07 #1
2 1484


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.



"Big Charles" <ch**********@yahoo.comwrote in message
news:11*********************@l53g2000cwa.googlegro ups.com...
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

Feb 2 '07 #2
On Feb 2, 9:48 pm, "Big Charles" <cherediat...@yahoo.comwrote:
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

--
http://ajaxwidgets.com
Free ASP.NET Ajax Widgets NOW!

Feb 2 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Winston | last post: by
2 posts views Thread by Wendell Wilkie | last post: by
17 posts views Thread by yinglcs | last post: by
reply views Thread by Steve Chaplin | last post: by
2 posts views Thread by Boki | last post: by
1 post views Thread by Ronald S. Cook | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.