By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,995 Members | 1,195 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,995 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a


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

P: n/a
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.