Problem with ArrayList

B

Bernard Bourée

I have defined a Class called EntSor (see code ) with a procedure Sub
Définit which assign the values of this class.

Then I have defined an Arraylist with
Dim colEntSor As New ArrayList()

The following code assign the values of the last lign to all elements of the
collection:

EntSor.Définit("PAAE", "bar", 0, True) : colEntSor.Add(EntSor)

EntSor.Définit("TAAE", "°C", 0, True) : colEntSor.Add(EntSor)

EntSor.Définit("QAAE", "kg/s", 0, True) : colEntSor.Add(EntSor)

EntSor.Définit("XVAE", "%", 0, True) : colEntSor.Add(EntSor)

What is wrong ?

Thanks

Bernard

==============================================

Public Class EntSor

Private mNomVal As String

Private mNomUnit As String

Private mDonnées As Boolean

Private mValeur As Decimal

Public Property NomVal() As String

Get

Return mNomVal

End Get

Set(ByVal Value As String)

mNomVal = Value

End Set

End Property

Public Property NomUnit() As String

Get

Return mNomUnit

End Get

Set(ByVal Value As String)

mNomUnit = Value

End Set

End Property

Public Property Données() As Boolean

Get

Return mDonnées

End Get

Set(ByVal Value As Boolean)

mDonnées = Value

End Set

End Property

Public Property Valeur() As Decimal

Get

Return mValeur

End Get

Set(ByVal Value As Decimal)

mValeur = Value

End Set

End Property

Public Sub Définit(ByVal sNomVal As String, _

ByVal sNomunité As String, _

ByVal dVal As Decimal, _

ByVal bDonnées As Boolean)

With Me

..NomVal = sNomVal

..NomUnit = sNomunité

..Valeur = dVal

..Données = bDonnées

End With

End Sub

End Class
 
G

Guest

Not sure what you are trying to do exactly. If you are trying to instantiate
the Entsor class, you should put your initialization variables in the class
using a Public New sub such as

Public Class Entsor

public New (mystring1 as string, mystring2 as string, myinteger as
integer, _
myBool as boolean)

'Do you Initialization here

end sub

End Class

Dim colEntSor As New ArrayList()

colEntsor.Add (New Entsor("PAAE", "bar", 0, True))
colEntsor.Add (New Entsor("TAAE", "°C", 0, True))
colEntsor.Add (New Entsor("QAAE", "kg/s", 0, True))
colEntsor.Add (New Entsor("XVAE", "%", 0, True))

Bottom line is that you have to create an instance of the class to add it to
an Arraylist or actually, to do anyting with it.

Hope this helps.
 
L

Larry Serflaten

Bernard Bourée said:
I have defined a Class called EntSor (see code ) with a procedure Sub
Difinit which assign the values of this class.

Then I have defined an Arraylist with
Dim colEntSor As New ArrayList()

The following code assign the values of the last lign to all elements of the
collection:

EntSor.Difinit("PAAE", "bar", 0, True) : colEntSor.Add(EntSor)
EntSor.Difinit("TAAE", "0C", 0, True) : colEntSor.Add(EntSor)
EntSor.Difinit("QAAE", "kg/s", 0, True) : colEntSor.Add(EntSor)
EntSor.Difinit("XVAE", "%", 0, True) : colEntSor.Add(EntSor)

What is wrong ?

You are adding the exact same object to the list. When you want
different objects (or when you want to create an object) you have
to use the New keyword. Your code does not use New at all
so you are adding the same object 4 times.

Add a public sub called New to your class that accepts all the
same parameters as Difinit, then use it like:

colEntSor.Add(New EntSor("PAAE", "bar", 0, True))
colEntSor.Add(New EntSor("TAAE", "0C", 0, True))
colEntSor.Add(New EntSor("QAAE", "kg/s", 0, True))
colEntSor.Add(New EntSor("XVAE", "%", 0, True))

To support the syntax:

Dim EntSorA As New EntSor

You would have to also add a sub called New that accepts no
parameters (This one is added by default, if you do not declare
a New sub yourself VB adds a parameter-less constructor for you).

When you define a New sub that uses parameters, VB skips adding
the parameter-less one, so you have to add that back yourself.

HTH
LFS
 

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