-
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.
-
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.
-
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.
-
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.
-
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>
-
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.
-
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
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|