How do I can create a DYNAMIC object array of my class?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: How do I can create a DYNAMIC object array of my class?

Hybrid View

  1. #1
    Marco Alves Guest

    How do I can create a DYNAMIC object array of my class?


    First, my apologies for my bad english.

    I need to create an dynamic array. This array contain various instances of
    my class. And my class have properties, methods, etc. How do I can make this?

    Regards,

    Marco Alves.

  2. #2
    neutron Guest

    Re: How do I can create a DYNAMIC object array of my class?



    Try using the ArrayList

    Eg:
    ArrayList myAL = new ArrayList();

    Add the onjects to the list...
    something like this:
    myAL.Add("Hello");
    myAL.Add("World");
    myAL.Add("!");

    Dont forget to use the System.Collections

    Neutron


    "Marco Alves" <fatalk1ll3r@brturbo.com.br> wrote:
    >
    >First, my apologies for my bad english.
    >
    >I need to create an dynamic array. This array contain various instances

    of
    >my class. And my class have properties, methods, etc. How do I can make

    this?
    >
    >Regards,
    >
    >Marco Alves.



  3. #3
    Marco Alves Guest

    Re: How do I can create a DYNAMIC object array of my class?


    "neutron" <neutronboy55@aol.com> wrote:
    >
    >
    >Try using the ArrayList
    >
    >Eg:
    >ArrayList myAL = new ArrayList();
    >
    >Add the onjects to the list...
    >something like this:
    >myAL.Add("Hello");
    >myAL.Add("World");
    >myAL.Add("!");
    >
    >Dont forget to use the System.Collections
    >


    Thx, Neutron. I'm already using this. But when I need to get one value i'm
    receiving "erros", see my code:

    my class is called 'Item'

    one property of my class:

    private static int _Versao;
    public int Versao
    {
    set
    {
    _Versao = value;
    }
    get
    {
    return _Versao;
    }
    }


    another class (the class where i declare intances of my 'Item' class
    my array:
    ArrayList al = new ArrayList(); // this variable have a global scope for

    // the class

    Item objItem = new Item();
    in a loop i make the following code:
    objItem.Versao = Int32.Parse(cl.Substring(3));
    al.Add(objItem);

    suposes that i add 2 new items to my array. then, in another method i make
    this:

    int iCount;
    iCount = al.Count;
    Item objItem = new Item(); // current item
    for (i=0;i<iCount;i++)
    {
    objItem = (Item) al[i];
    // Setup the new node
    nodex.Text = objItem.Versao;
    tvwItems.Nodes.Add(nodex);
    }

    finally the problem is:

    supposes that:
    first objItem.Versao is 0
    second objItem.Versao is 1
    third objItem.Versao is 2

    all the items of my array contain de SAME values! Why?

    if you don't understandme, sorry.

  4. #4
    jov0 Guest

    Re: How do I can create a DYNAMIC object array of my class?

    ArrayList myAL = new ArrayList();
    myAL.Add( new Item(parameter1, parameter2) ); ...

    --- OR ---

    class MyArrayList
    {
    protected ArrayList data = new ArrayList();
    public object this[int idx]
    {
    get
    {
    if (idx > -1 && dx < data.Count)
    return (data[idx]);
    else
    return null; // or throw exception
    }
    set
    {
    if (idx > -1 && idx < data.Count)
    data[idx] = value;
    else if (idx == data.Count)
    data.Add(value);
    else
    {
    // Throw exception?
    }
    }
    }
    }

    // then somewhere in application
    ...
    MyArrayList m = new MyArrayList();
    m[0] = new Item(1);
    m[1] = new Item(2);
    m[3] = new Item(3);
    m[0] = new Item(100); // Replaced index 0
    // etc


    "Marco Alves" <fatalk1ll3r@brturbo.com> wrote in message
    news:3c5b0f6e$1@10.1.10.29...
    >
    > "neutron" <neutronboy55@aol.com> wrote:
    > >
    > >
    > >Try using the ArrayList
    > >
    > >Eg:
    > >ArrayList myAL = new ArrayList();
    > >
    > >Add the onjects to the list...
    > >something like this:
    > >myAL.Add("Hello");
    > >myAL.Add("World");
    > >myAL.Add("!");
    > >
    > >Dont forget to use the System.Collections
    > >

    >
    > Thx, Neutron. I'm already using this. But when I need to get one value i'm
    > receiving "erros", see my code:
    >
    > my class is called 'Item'
    >
    > one property of my class:
    >
    > private static int _Versao;
    > public int Versao
    > {
    > set
    > {
    > _Versao = value;
    > }
    > get
    > {
    > return _Versao;
    > }
    > }
    >
    >
    > another class (the class where i declare intances of my 'Item' class
    > my array:
    > ArrayList al = new ArrayList(); // this variable have a global scope for
    >
    > // the class
    >
    > Item objItem = new Item();
    > in a loop i make the following code:
    > objItem.Versao = Int32.Parse(cl.Substring(3));
    > al.Add(objItem);
    >
    > suposes that i add 2 new items to my array. then, in another method i make
    > this:
    >
    > int iCount;
    > iCount = al.Count;
    > Item objItem = new Item(); // current item
    > for (i=0;i<iCount;i++)
    > {
    > objItem = (Item) al[i];
    > // Setup the new node
    > nodex.Text = objItem.Versao;
    > tvwItems.Nodes.Add(nodex);
    > }
    >
    > finally the problem is:
    >
    > supposes that:
    > first objItem.Versao is 0
    > second objItem.Versao is 1
    > third objItem.Versao is 2
    >
    > all the items of my array contain de SAME values! Why?
    >
    > if you don't understandme, sorry.




  5. #5
    Kunle Odutola Guest

    Re: How do I can create a DYNAMIC object array of my class?


    "Marco Alves" <fatalk1ll3r@brturbo.com> wrote in message
    news:3c5b0f6e$1@10.1.10.29...

    > Thx, Neutron. I'm already using this. But when I need to get one value i'm
    > receiving "erros", see my code:
    >
    > my class is called 'Item'
    >
    > one property of my class:
    >
    > private static int _Versao;
    > public int Versao
    > {
    > set
    > {
    > _Versao = value;
    > }
    > get
    > {
    > return _Versao;
    > }
    > }
    >
    >
    > another class (the class where i declare intances of my 'Item' class
    > my array:
    > ArrayList al = new ArrayList(); // this variable have a global scope for
    >
    > // the class
    >
    > Item objItem = new Item();


    This line above NEEDS to be in the loop below too.
    You need to create a new instance of Item to add to the ArrayList each time
    you go round the loop.

    > in a loop i make the following code:
    > objItem.Versao = Int32.Parse(cl.Substring(3));
    > al.Add(objItem);
    >
    > suposes that i add 2 new items to my array. then, in another method i make


    <SNIP>


  6. #6
    Pierre Gauvin Guest

    Re: How do I can create a DYNAMIC object array of my class?


    You have to replace :

    Item objItem = new Item();
    in a loop i make the following code:
    objItem.Versao = Int32.Parse(cl.Substring(3));
    al.Add(objItem);
    close the loop

    with...

    in a loop i make the following code:
    Item objItem = new Item();
    objItem.Versao = Int32.Parse(cl.Substring(3));
    al.Add(objItem);
    close the loop


    In your code all items in the al collection refere to the same objItem and
    the versao property value is equal to the last Int32.Parse(cl.Substring(3))
    of the loop.




  7. #7
    crunch Guest

    Re: How do I can create a DYNAMIC object array of my class?


    "Marco Alves" <fatalk1ll3r@brturbo.com> wrote:
    >
    >"neutron" <neutronboy55@aol.com> wrote:
    >>
    >>
    >>Try using the ArrayList
    >>
    >>Eg:
    >>ArrayList myAL = new ArrayList();
    >>
    >>Add the onjects to the list...
    >>something like this:
    >>myAL.Add("Hello");
    >>myAL.Add("World");
    >>myAL.Add("!");
    >>
    >>Dont forget to use the System.Collections
    >>

    >
    >Thx, Neutron. I'm already using this. But when I need to get one value i'm
    >receiving "erros", see my code:
    >
    >my class is called 'Item'
    >
    >one property of my class:
    >
    >private static int _Versao;
    >public int Versao
    >{
    > set
    > {
    > _Versao = value;
    > }
    > get
    > {
    > return _Versao;
    > }
    >}
    >
    >
    >another class (the class where i declare intances of my 'Item' class
    >my array:
    >ArrayList al = new ArrayList(); // this variable have a global scope for
    >
    > // the class
    >
    >Item objItem = new Item();
    >in a loop i make the following code:
    >objItem.Versao = Int32.Parse(cl.Substring(3));
    >al.Add(objItem);
    >
    >suposes that i add 2 new items to my array. then, in another method i make
    >this:
    >
    >int iCount;
    >iCount = al.Count;
    >Item objItem = new Item(); // current item
    >for (i=0;i<iCount;i++)
    >{
    > objItem = (Item) al[i];
    > // Setup the new node
    > nodex.Text = objItem.Versao;
    > tvwItems.Nodes.Add(nodex);
    >}
    >
    >finally the problem is:
    >
    >supposes that:
    >first objItem.Versao is 0
    >second objItem.Versao is 1
    >third objItem.Versao is 2
    >
    >all the items of my array contain de SAME values! Why?
    >
    >if you don't understandme, sorry.



    In addition to the some of the mistakes that have already been posted: the
    value of 'Versao' is going to be the same for each object since you've declared
    it as static.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center