ArrayList retrieving value of object
This is getting made in a static void method in the main class. (class being used for input).
private static void Input(ArrayList list)
{
//other code...
Object b = new Object(number, hour, minutes, seconds);
list.Add(b)
}
Next I have another static void method to display the objects within. The thing is i want to group them on the number, and then display all the hours, minutes and seconds for the same number underneath each other. Then the following number(s) with all there corresponding hours, minutes and seconds.
But I am not sure how i can get the number for example the first array, and 2nd and so on.
How can I access this? The syntax below does not work and not sure how to retrieve this value(s).
int var = array[0].number; //does not work
Also how do i make a instance of this object again? I cannot use new again can i?
private static void Output(ArrayList list)
{
while (i < list.Count)
{
//keep track of current group (number)
//announce group
//...
}
}
Reg开发者_如何转开发ards.
This is because you're using a non-generic collection (ArrayList
instead of List<T>
). If you can, it would be much better to use a generic collection. However, if you're forced to use ArrayList
, you've got to cast:
Foo foo = (Foo) list[0];
int x = foo.number;
...
(I hope you haven't really named your own class Object
... I would also recommend against using the name var
as that is a contextual keyword as of C# 3.)
Instead of using an ArrayList
, use a List<T>
where T
is your type of object.
精彩评论