newbie question

  • Thread starter Thread starter Troy
  • Start date Start date
T

Troy

Hello,

I'm new to vb.net so bear with me. I have a car class in my project and need
to declare an array of the car object. I've tried to instantiate using the
following but I cant seem to enumerate any methods or properties in my
class:

dim cars() as Car = new cars(99) {}

any ideas?

thanks
 
Troy said:
I have a car class in my project and need to declare an array of the car
object. I've tried to instantiate using the following but I cant seem to
enumerate any methods or properties in my class:

dim cars() as Car = new cars(99) {}

\\\
Dim Cars(99) As Car
For i As Integer = 0 To Cars.Length - 1
Cars(i) = New Car()
Next i
///
 
: Hello,
:
: I'm new to vb.net so bear with me. I have a car class in my project
: and need to declare an array of the car object. I've tried to
: instantiate using the following but I cant seem to enumerate any
: methods or properties in my class:
:
: dim cars() as Car = new cars(99) {}
:
: any ideas?
:
: thanks


There are a number of approaches. If you know for in advance that you
will have 99 car objects you'll be working with, the simple array syntax
works fine:


Dim CarArray(99) As Car


This has the advantange of being strongly typed because you can only add
Car objects to the array. However, if you don't know in advance the
number of cars you'll be using, this is not a good approach. You have to
declare an array large enough to hold all the cars you think you might
need. However, the odds are good you won't use them all and therefore
your code is wasting memory resources. There may also come a time when
the size of the array you created is too small and you'll have to go
back and change your code.


In this case, you need a collection that can be resized automatically.
There are a number of predefined collection class you can use for this
or you can create your own. For example:


Dim CarListArray As ListArray


This is a dynamic array class that will automatically resize itself as
needed. However, it is very generic. Any object can be added to the
array which may not be acceptable. To prevent this, you can create you
own custom collection class which you can control. For example:


Dim Cars As MyCarCollection
[...]
Public Class MyCarCollection
[...]
End Class

In this case, you could have a private ArrayList member which you can
access via properties. Alternatively, you could inherit your custom
collection from the CollectionBase class.


Here is an example showing how you can use three of the approaches
above. It is trivial, and the custom collection class barely exposes any
useful functionality, but it shows the principles. Read up on
"System.Collections" in the documentation for more information.


Option Strict

Imports Microsoft.VisualBasic
Imports System
Imports System.Collections
Imports System.Text

Public Class MyProject

Public Shared Sub Main()
Const TotalCars As Integer = 10
Dim ndx As Integer

Randomize

Dim CarsArray(TotalCars) As Car
For ndx = 0 To TotalCars
CarsArray(ndx) = New Car()
Next

Dim CarsArrayList As New ArrayList
For ndx = 0 To (CInt(Rnd * TotalCars) + 1)
CarsArrayList.Add(New Car())
Next

Dim Cars As New MyCarCollection
For ndx = 0 To (CInt(Rnd * TotalCars) + 1)
Dim ModelYear As Integer = 1990 + ndx
Cars.Add(New Car("Model", "Make", ModelYear, "VIN"))
Next

Dim MyString As New StringBuilder
With MyString
.Append("There are ")
.Append(CarsArray.Length.ToString)
.Append(" 'Car' objects in the 'CarsArray()' array.")
.Append(vbCrLf)

.Append("The 'CarsArrayList' collection contains ")
.Append(CarsArrayList.Count.toString)
.Append(" 'Car' objects.")
.Append(vbCrLf)

.Append("The custom 'Cars()' collection contains ")
.Append((Cars.Count).ToString)
.Append(" 'Car' Objects")
.Append(vbCrLf)

For ndx = 0 To Cars.Count - 1
.Append(vbTab)
.Append("Item: ")
.Append(ndx.ToString)
.Append(vbTab)
.Append("Year: ")
.Append(Cars(ndx).ModelYear.ToString)
.Append(vbCrLf)
Next
.Append(vbCrLf)
End With

Console.WriteLine()
Console.WriteLine(MyString.ToString)
End Sub

End Class

Public Class MyCarCollection
Private m_Arr As ArrayList

Public Sub New()
m_Arr = New ArrayList
End Sub

Public Readonly Property Count As Integer
Get
Return m_Arr.Count
End Get
End Property

Default Public Property Item(ByVal index As Integer) As Car
Get
Return CType(m_Arr(index), Car)
End Get
Set
m_Arr(index) = value
End Set
End Property

Public Function Add(ByRef item As Car) As Integer
Return m_Arr.Add(item)
End Function

End Class

Public Class Car

Private m_Make As String
Private m_Model As String
Private m_Year As Integer
Private m_Vin As String

Public Sub New()
End Sub

Public Sub New(ByVal Model As String, _
ByVal Make As String, _
ByVal ModelYear As Integer, _
ByVal Vin As String)
m_Make = Make
m_Model = Model
m_Year = ModelYear
m_Vin = Vin
End Sub

Public Property Make() As String
Get
Return m_Make
End Get
Set
m_Make = Value
End Set
End Property

Public Property Model() As String
Get
Return m_Model
End Get
Set
m_Model = Value
End Set
End Property

Public Property ModelYear() As Integer
Get
Return m_Year
End Get
Set
m_Year = Value
End Set
End Property

Public Property Vin() As String
Get
Return m_Vin
End Get
Set
m_Vin = Value
End Set
End Property

End Class


Ralf
 
wow - thx guys.

_AnonCoward said:
: Hello,
:
: I'm new to vb.net so bear with me. I have a car class in my project
: and need to declare an array of the car object. I've tried to
: instantiate using the following but I cant seem to enumerate any
: methods or properties in my class:
:
: dim cars() as Car = new cars(99) {}
:
: any ideas?
:
: thanks


There are a number of approaches. If you know for in advance that you
will have 99 car objects you'll be working with, the simple array syntax
works fine:


Dim CarArray(99) As Car


This has the advantange of being strongly typed because you can only add
Car objects to the array. However, if you don't know in advance the
number of cars you'll be using, this is not a good approach. You have to
declare an array large enough to hold all the cars you think you might
need. However, the odds are good you won't use them all and therefore
your code is wasting memory resources. There may also come a time when
the size of the array you created is too small and you'll have to go
back and change your code.


In this case, you need a collection that can be resized automatically.
There are a number of predefined collection class you can use for this
or you can create your own. For example:


Dim CarListArray As ListArray


This is a dynamic array class that will automatically resize itself as
needed. However, it is very generic. Any object can be added to the
array which may not be acceptable. To prevent this, you can create you
own custom collection class which you can control. For example:


Dim Cars As MyCarCollection
[...]
Public Class MyCarCollection
[...]
End Class

In this case, you could have a private ArrayList member which you can
access via properties. Alternatively, you could inherit your custom
collection from the CollectionBase class.


Here is an example showing how you can use three of the approaches
above. It is trivial, and the custom collection class barely exposes any
useful functionality, but it shows the principles. Read up on
"System.Collections" in the documentation for more information.


Option Strict

Imports Microsoft.VisualBasic
Imports System
Imports System.Collections
Imports System.Text

Public Class MyProject

Public Shared Sub Main()
Const TotalCars As Integer = 10
Dim ndx As Integer

Randomize

Dim CarsArray(TotalCars) As Car
For ndx = 0 To TotalCars
CarsArray(ndx) = New Car()
Next

Dim CarsArrayList As New ArrayList
For ndx = 0 To (CInt(Rnd * TotalCars) + 1)
CarsArrayList.Add(New Car())
Next

Dim Cars As New MyCarCollection
For ndx = 0 To (CInt(Rnd * TotalCars) + 1)
Dim ModelYear As Integer = 1990 + ndx
Cars.Add(New Car("Model", "Make", ModelYear, "VIN"))
Next

Dim MyString As New StringBuilder
With MyString
.Append("There are ")
.Append(CarsArray.Length.ToString)
.Append(" 'Car' objects in the 'CarsArray()' array.")
.Append(vbCrLf)

.Append("The 'CarsArrayList' collection contains ")
.Append(CarsArrayList.Count.toString)
.Append(" 'Car' objects.")
.Append(vbCrLf)

.Append("The custom 'Cars()' collection contains ")
.Append((Cars.Count).ToString)
.Append(" 'Car' Objects")
.Append(vbCrLf)

For ndx = 0 To Cars.Count - 1
.Append(vbTab)
.Append("Item: ")
.Append(ndx.ToString)
.Append(vbTab)
.Append("Year: ")
.Append(Cars(ndx).ModelYear.ToString)
.Append(vbCrLf)
Next
.Append(vbCrLf)
End With

Console.WriteLine()
Console.WriteLine(MyString.ToString)
End Sub

End Class

Public Class MyCarCollection
Private m_Arr As ArrayList

Public Sub New()
m_Arr = New ArrayList
End Sub

Public Readonly Property Count As Integer
Get
Return m_Arr.Count
End Get
End Property

Default Public Property Item(ByVal index As Integer) As Car
Get
Return CType(m_Arr(index), Car)
End Get
Set
m_Arr(index) = value
End Set
End Property

Public Function Add(ByRef item As Car) As Integer
Return m_Arr.Add(item)
End Function

End Class

Public Class Car

Private m_Make As String
Private m_Model As String
Private m_Year As Integer
Private m_Vin As String

Public Sub New()
End Sub

Public Sub New(ByVal Model As String, _
ByVal Make As String, _
ByVal ModelYear As Integer, _
ByVal Vin As String)
m_Make = Make
m_Model = Model
m_Year = ModelYear
m_Vin = Vin
End Sub

Public Property Make() As String
Get
Return m_Make
End Get
Set
m_Make = Value
End Set
End Property

Public Property Model() As String
Get
Return m_Model
End Get
Set
m_Model = Value
End Set
End Property

Public Property ModelYear() As Integer
Get
Return m_Year
End Get
Set
m_Year = Value
End Set
End Property

Public Property Vin() As String
Get
Return m_Vin
End Get
Set
m_Vin = Value
End Set
End Property

End Class


Ralf
 

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

Back
Top