Class in arraylist

P

Peter

I saw this example and was wondering how I would driectly assign
information out of this structure:

Public Class guitar
Private m_make As String
Private m_model As String
Private m_year As Short

Public Sub New(ByVal make, ByRef model, ByVal year)
m_make = make
m_model = model
m_year = year
End Sub
Public Property make() As String
Get
Return m_make
End Get
Set(ByVal Value As String)
m_make = Value
End Set
End Property
'<<<<<<<<<<<<<<<<<<<<<<<<<<<...Inset Other properties
here>>>>>>>>>>>>>>>>>>
End Class

Private al as New Arraylist()

al.Add(New guitar("Gibson", "Les Paul", 1958))
al.Add(New guitar("Fender", "Jazz Bass", 1964))
al.Add(New guitar("Guild", "Bluesbird", 1971))


So now if I want to grab and assign one of these values driectly from
the array list to another string how would I do that?
 
I

Imran Koradia

Something like this:

Dim make as String = DirectCast(al.Item(0), guitar).make

Basically, you have guitar objects stored in your arraylist. You can iterate
through the arraylist to retrieve the various guitar objects. However, since
the Item property of the ArrayList returns an object, you'll need to cast it
to your guitar object before accessing the property of the guitar object
which is why the DirectCast above.

hope that helps..
Imran.
 
H

Herfried K. Wagner [MVP]

Peter said:
I saw this example and was wondering how I would driectly assign
information out of this structure:

Public Class guitar
Private m_make As String
Private m_model As String
Private m_year As Short

Public Sub New(ByVal make, ByRef model, ByVal year)

.... in addition to the other replies: Why is 'make' not declared 'As
String'? 'year' should be 'Short', and 'model' IMO doesn't need to be
passed 'ByRef'.
 

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

Top