arraylist

R

RobcPettit

Hi, hopefully Ill explain my question ok. Im using an arraylist to get
an an array of values from an application, which allows me to use a
com interface to interact with its functions. I have no access to the
code. I use prices.Add(ba.getPrices());, prices bieng my arraylist. It
returns 1 element, which has 6 dimensions. I think thats the wording.
So within prices[0] there is 6 dimensions, 0 to 5. When I use
prices.count it returns 1, how do I get the count of the dimensions.
Regards Robert
 
P

Peter Duniho

Hi, hopefully Ill explain my question ok. Im using an arraylist to get
an an array of values from an application, which allows me to use a
com interface to interact with its functions. I have no access to the
code. I use prices.Add(ba.getPrices());, prices bieng my arraylist. It
returns 1 element, which has 6 dimensions. I think thats the wording.
So within prices[0] there is 6 dimensions, 0 to 5. When I use
prices.count it returns 1, how do I get the count of the dimensions.
Regards Robert

Some actual sample code would be useful here.

It's not really clear what you're trying to do. But it's possible that
instead of the Add() method, you really want the AddRange() method. That
is, you want the ArrayList to have 6 elements if the array returned by
"ba.getPrices()" itself has 6 elements.

On the other hand, if you really want to use Add(), that means that your
ArrayList is eventually going to contain multiple instances of the array
returned by "ba.getPrices()". If so, then what you want is to get the
element from the "prices" array (e.g. "prices[0]") and cast that to the
appropriate array type, and then look at the Length property of that
array. That will tell you how many elements are in the array that was
returned by "ba.getPrices()" and stored in the "prices" ArrayList.

If neither of the above apply to your use of the ArrayList I think it's
likely you're misusing the ArrayList. Again, a code sample would help us
to better understand the situation and help you fix it.

Pete
 
R

RobcPettit

Thankyou for your reply. Addrange was the one, gives me what I need.
Regards Robert
 
D

DSK Chakravarthy

Robert,

Instead of using ArrayList, i recommend you to develop your own class which
is inherited from CollectionBase and implement the .ADD method to it.. so
that you dont have to worry about the index..

HTH,
Chakravarthy
 
R

RobcPettit

Thankyo for your reply. I will read up on CollectionBase. Im still
having problems, I can see the elements but cant access them.
Regard Robert
 
R

RobcPettit

Im struggling with this. I dont understand why though. If I put a
break in my code and look at the prices array I can see 6 element 0 -
5. If I open up [0] I can see a list of values Im trying to get, they
dont have[0][1] etc beside them, just there names in a column, eg
amount1 100, amount2 200. I thought if I could see them I could ref
them as price[0].amount1. Ive tried object 'o = ba.getprices(0)' and
prices.AddRange(ba.getPrices()). I can see the details in both but
cant get at the details. Im reading up, but Im missing something.
Regards Robert
 
J

Jon Skeet [C# MVP]

Im struggling with this. I dont understand why though. If I put a
break in my code and look at the prices array I can see 6 element 0 -
5. If I open up [0] I can see a list of values Im trying to get, they
dont have[0][1] etc beside them, just there names in a column, eg
amount1 100, amount2 200. I thought if I could see them I could ref
them as price[0].amount1. Ive tried object 'o = ba.getprices(0)' and
prices.AddRange(ba.getPrices()). I can see the details in both but
cant get at the details. Im reading up, but Im missing something.

The debugger is showing you the data dynamically - the compiler only
knows about *static* typing.

However, it would really help if you could show all the code,
highlighting the lines which you're confused about - then we can help.
Comment out any lines which don't work, but you don't know why.
 
P

Peter Duniho

Im struggling with this. I dont understand why though. If I put a
break in my code and look at the prices array I can see 6 element 0 -
5. If I open up [0] I can see a list of values Im trying to get, they
dont have[0][1] etc beside them, just there names in a column, eg
amount1 100, amount2 200. I thought if I could see them I could ref
them as price[0].amount1. Ive tried object 'o = ba.getprices(0)' and
prices.AddRange(ba.getPrices()). I can see the details in both but
cant get at the details. Im reading up, but Im missing something.

Your post is fairly confusing and vague. However, it sounds like you're
failing to cast the object stored in the ArrayList back to whatever type
is returned by the "getPrices()" method you mentioned earlier.

One fix would be to perform that cast so that you can get at the members
of the actual data structure. A much better fix would be to use the
List<T> class instead. With that, you use whatever type is being returned
by "getPrices()" (well, the base type for the array that's being returned,
that is), and then when you reference an element in the List<T>, you
already have the correct type.

For example, if you've got this:

class Price
{
public decimal amount1 { get; set; }
}

Price[] getPrices() { ... }

Then you can declare a variable:

List<Price> prices = new List<Price>();

Add the new prices:

prices.AddRange(getPrices());

And access an individual price without casting:

prices[0].amount1;


Pete
 

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