Problem with ArrayList

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bernard Bourée

    Problem with ArrayList

    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(E ntSor)

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

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

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

    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


    --
    Bernard Bourée
    bernard@bouree. net


  • Larry Serflaten

    #2
    Re: Problem with ArrayList


    "Bernard Bourée" <bernard@bouree .net> wrote[color=blue]
    > 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(E ntSor)
    > EntSor.Difinit( "TAAE", "0C", 0, True) : colEntSor.Add(E ntSor)
    > EntSor.Difinit( "QAAE", "kg/s", 0, True) : colEntSor.Add(E ntSor)
    > EntSor.Difinit( "XVAE", "%", 0, True) : colEntSor.Add(E ntSor)
    >
    > What is wrong ?[/color]

    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(N ew EntSor("PAAE", "bar", 0, True))
    colEntSor.Add(N ew EntSor("TAAE", "0C", 0, True))
    colEntSor.Add(N ew EntSor("QAAE", "kg/s", 0, True))
    colEntSor.Add(N ew 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

    Comment

    • Dennis

      #3
      RE: Problem with ArrayList

      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.

      "Bernard Bourée" wrote:
      [color=blue]
      > 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(E ntSor)
      >
      > EntSor.Définit ("TAAE", "°C", 0, True) : colEntSor.Add(E ntSor)
      >
      > EntSor.Définit ("QAAE", "kg/s", 0, True) : colEntSor.Add(E ntSor)
      >
      > EntSor.Définit ("XVAE", "%", 0, True) : colEntSor.Add(E ntSor)
      >
      > 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
      >
      >
      > --
      > Bernard Bourée
      > bernard@bouree. net
      >
      >
      >[/color]

      Comment

      Working...