Adding an Object to an ArrayList

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Anchor

    Adding an Object to an ArrayList

    Hi All,

    I was hoping someone could help me out. I am new to VB.NET and
    I don't know the language too well. I am trying to add an object
    instance of a class to an array like in the code snipet below.
    I run through the For Each loop 5 times and I place a reference
    to the object in an ArrayList. Once I skip out of the loop block
    I expect to have 5 different instances of that class in the
    ArrayList. However, instead I have 5 instances of the class but
    they are all of the last one that was added.


    ObjList As ArrayList ' Declared at top of Form Class.
    ObjList = New ArrayList ' In an initialize method.

    ' Code below is in some method in same Form Class.
    Dim Info As InfoClass
    Dim CrntObj as SomeClass

    For Each Info in SomeArray ' 5 Info objects in SomeArray
    CrntObj = myMgr.GetSomeCl assObject(Info)
    ' do some stuff here
    ObjList.Add(Crn tObj)
    Next sInfo


    I know whats happening. I'm storing the reference to the
    CrntObj variable in the ArrayList instead of what it references.
    That's why I have 5 objects in the ArrayList all pointing to
    the last item that was added. But why? I believe this would be
    okay in C#. Can someone please help me out and tell me what I
    am doing wrong?

    Thanks,
    Joe
  • Rick Rothstein

    #2
    Re: Adding an Object to an ArrayList

    Almost everybody in this newsgroup is using VB6 or lower. While you may get
    a stray answer to VB.NET questions here, you should ask them in newsgroups
    devoted exclusively to .NET programming. Look for newsgroups with either the
    word "dotnet" or "vsnet" in their name.

    For the microsoft news server, try these newsgroups...

    microsoft.publi c.dotnet.genera l
    microsoft.publi c.dotnet.langua ges.vb
    microsoft.publi c.vsnet.general

    There are some others, but these should get you started.

    Rick - MVP



    "Anchor" <jmanzi@gtnetin c.com> wrote in message
    news:fb5c52a2.0 404021328.699a2 4ff@posting.goo gle.com...[color=blue]
    > Hi All,
    >
    > I was hoping someone could help me out. I am new to VB.NET and
    > I don't know the language too well. I am trying to add an object
    > instance of a class to an array like in the code snipet below.
    > I run through the For Each loop 5 times and I place a reference
    > to the object in an ArrayList. Once I skip out of the loop block
    > I expect to have 5 different instances of that class in the
    > ArrayList. However, instead I have 5 instances of the class but
    > they are all of the last one that was added.
    >
    >
    > ObjList As ArrayList ' Declared at top of Form Class.
    > ObjList = New ArrayList ' In an initialize method.
    >
    > ' Code below is in some method in same Form Class.
    > Dim Info As InfoClass
    > Dim CrntObj as SomeClass
    >
    > For Each Info in SomeArray ' 5 Info objects in SomeArray
    > CrntObj = myMgr.GetSomeCl assObject(Info)
    > ' do some stuff here
    > ObjList.Add(Crn tObj)
    > Next sInfo
    >
    >
    > I know whats happening. I'm storing the reference to the
    > CrntObj variable in the ArrayList instead of what it references.
    > That's why I have 5 objects in the ArrayList all pointing to
    > the last item that was added. But why? I believe this would be
    > okay in C#. Can someone please help me out and tell me what I
    > am doing wrong?
    >
    > Thanks,
    > Joe[/color]


    Comment

    • Anchor

      #3
      Re: Adding an Object to an ArrayList

      Great! Thanks for the info!

      -- Joe


      "Rick Rothstein" <rickNOSPAMnews @NOSPAMcomcast. net> wrote in message news:<dYCdnUdgL OvaRvDdRVn_iw@c omcast.com>...[color=blue]
      > Almost everybody in this newsgroup is using VB6 or lower. While you may get
      > a stray answer to VB.NET questions here, you should ask them in newsgroups
      > devoted exclusively to .NET programming. Look for newsgroups with either the
      > word "dotnet" or "vsnet" in their name.
      >
      > For the microsoft news server, try these newsgroups...
      >
      > microsoft.publi c.dotnet.genera l
      > microsoft.publi c.dotnet.langua ges.vb
      > microsoft.publi c.vsnet.general
      >
      > There are some others, but these should get you started.
      >
      > Rick - MVP
      >
      >
      >
      > "Anchor" <jmanzi@gtnetin c.com> wrote in message
      > news:fb5c52a2.0 404021328.699a2 4ff@posting.goo gle.com...[color=green]
      > > Hi All,
      > >
      > > I was hoping someone could help me out. I am new to VB.NET and
      > > I don't know the language too well. I am trying to add an object
      > > instance of a class to an array like in the code snipet below.
      > > I run through the For Each loop 5 times and I place a reference
      > > to the object in an ArrayList. Once I skip out of the loop block
      > > I expect to have 5 different instances of that class in the
      > > ArrayList. However, instead I have 5 instances of the class but
      > > they are all of the last one that was added.
      > >
      > >
      > > ObjList As ArrayList ' Declared at top of Form Class.
      > > ObjList = New ArrayList ' In an initialize method.
      > >
      > > ' Code below is in some method in same Form Class.
      > > Dim Info As InfoClass
      > > Dim CrntObj as SomeClass
      > >
      > > For Each Info in SomeArray ' 5 Info objects in SomeArray
      > > CrntObj = myMgr.GetSomeCl assObject(Info)
      > > ' do some stuff here
      > > ObjList.Add(Crn tObj)
      > > Next sInfo
      > >
      > >
      > > I know whats happening. I'm storing the reference to the
      > > CrntObj variable in the ArrayList instead of what it references.
      > > That's why I have 5 objects in the ArrayList all pointing to
      > > the last item that was added. But why? I believe this would be
      > > okay in C#. Can someone please help me out and tell me what I
      > > am doing wrong?
      > >
      > > Thanks,
      > > Joe[/color][/color]

      Comment

      Working...