Unusual error message with Arraylist -- please help!!

A

almurph

Hi,

I'm trying to approximate a simple collection property in C#. Yes, I
know C# does not allow parameterized properties. you have implement a
poroperty opf this type either as an inxer or as a pair of methods. I
choose the second optuion and the code is below:


**** CODE AS FOLLOWS *****

private ArrayList mBranch = new ArrayList();


public Tree GetBranch(int I)
{
return (Tree)mBranch;
}

public void SetBranch(int I, Tree T)
{
mBranch = T;

}


*** EIND CODE ****


The problem is though, it does not work. To be specific the "SetBranch
()" method does not work. When i send something down the get the
following error message:

"Index was out of range. Must be non-negative and less than the size
of the collection.
Parameter name: index"


Would appreciate any comments/suggestions/corrections as tyo why this
is occuring. I'm at my wits end.

Thanks,
Al.
 
M

Michael A. Covington

An ArrayList is not an array, so it is a mistake to think of it as being
full of empty positions ready to store data. Those positions don't exist
until data is put in them. If you have not put three items into the
ArrayList then the position for element 2 (the third one) does not exist.

Think of an ArrayList as a list that happens to also be addressable by
subscript -- but only if the elements have been created and put in.

Use the Add method to add elements.
 
A

almurph

An ArrayList is not an array, so it is a mistake to think of it as being
full of empty positions ready to store data.  Those positions don't exist
until data is put in them.  If you have not put three items into the
ArrayList then the position for element 2 (the third one) does not exist.

Think of an ArrayList as a list that happens to also be addressable by
subscript -- but only if the elements have been created and put in.

Use the Add method to add elements.




I'm trying to approximate a simple collection property in C#. Yes, I
know C# does not allow parameterized properties. you have implement a
poroperty opf this type either as an inxer or as a pair of methods. I
choose the second optuion and the code is below:
**** CODE AS FOLLOWS *****
private ArrayList mBranch = new ArrayList();
public Tree GetBranch(int I)
{
return (Tree)mBranch;
}

public void SetBranch(int I, Tree T)
{
mBranch = T;

*** EIND CODE ****

The problem is though, it does not work. To be specific the "SetBranch
()" method does not work. When i send something down the get the
following error message:
"Index was out of range. Must be non-negative and less than the size
of the collection.
Parameter name: index"
Would appreciate any comments/suggestions/corrections as tyo why this
is occuring. I'm at my wits end.
Thanks,
Al.- Hide quoted text -

- Show quoted text -


Thanks, I kind of guessed this. Have implemented an indexer, I think
its better in the long run but thranks for your comments.
Cheers,
Al.
 
G

Gareth Erskine-Jones

Hi,

I'm trying to approximate a simple collection property in C#. Yes, I
know C# does not allow parameterized properties. you have implement a
poroperty opf this type either as an inxer or as a pair of methods. I
choose the second optuion and the code is below:


**** CODE AS FOLLOWS *****

private ArrayList mBranch = new ArrayList();


public Tree GetBranch(int I)
{
return (Tree)mBranch;
}

public void SetBranch(int I, Tree T)
{
mBranch = T;

}


The problem is that when you call SetBranch(1, aTree), you are then
attempting to assign aTree to the item at index 1 in the ArrayList -
but there is no item at that index. Your ArrayList is empty. To
explore this, add a couple of extra methods:

public int GetCount()
{
return mBranch.Count;
}

public void AddBranch(Tree T)
{
mBranch.Add(T);
}



Now, when you first instantiate your object, GetCount() will return 0.
This means there are no items in your collection, and so attempting to
get or set any item will fail.

If you then call:

AddBranch(tree1);
AddBranch(tree2);

Now GetCount() will return 2, and you can call your other methods with
any index which is non-negative and is less than the count - i.e. with
either 0 or 1.

Calling:

SetBranch(1, tree3);

will replace the second item in your collection (tree2) with tree3.
 

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