Newbie having problems with an ArrayList

A

Abe Frohnman

Hello all,

I'm having problems using an ArrayList. The two lines of relevant code
are here:

roomsArray.Add(new Area(roomNumber));

labelRoomNumber.Text = "You're currently building room number " +
(Area)(roomsArray[0]).RoomNum;

When I try to compile this, I get this error message: 'object' does not
contain a definition for 'RoomNum'.

How can I access a method from my Area class when its part of an ArrayList?

Thanks!

~af
 
J

Jon Skeet

Abe Frohnman said:
I'm having problems using an ArrayList. The two lines of relevant code
are here:

roomsArray.Add(new Area(roomNumber));

labelRoomNumber.Text = "You're currently building room number " +
(Area)(roomsArray[0]).RoomNum;

When I try to compile this, I get this error message: 'object' does not
contain a definition for 'RoomNum'.

How can I access a method from my Area class when its part of an ArrayList?

Currently it's being parsed as:

(Area) ( roomsArray[0].RoomNum )

You just need to cast slightly differently:

((Area)(roomsArray[0])).RoomNum
 
A

Abe Frohnman

So does the dot operator have a higher precedence than casting? In C++
its the other way around.

Jon said:
Abe Frohnman said:
I'm having problems using an ArrayList. The two lines of relevant code
are here:

roomsArray.Add(new Area(roomNumber));

labelRoomNumber.Text = "You're currently building room number " +
(Area)(roomsArray[0]).RoomNum;

When I try to compile this, I get this error message: 'object' does not
contain a definition for 'RoomNum'.

How can I access a method from my Area class when its part of an ArrayList?


Currently it's being parsed as:

(Area) ( roomsArray[0].RoomNum )

You just need to cast slightly differently:

((Area)(roomsArray[0])).RoomNum
 
J

Jon Skeet

Abe Frohnman said:
So does the dot operator have a higher precedence than casting? In C++
its the other way around.

To be honest, I've never learned the precedence - I put enough brackets
in to make it absolutely obvious to me where the cast goes, and that's
always enough for the compiler :)
 
L

Lee Alexander

In C++ its the other way around.
I think you will find . and -> are higher precedence in C++ as well

Regards
Lee
..
Abe Frohnman said:
So does the dot operator have a higher precedence than casting? In C++
its the other way around.

Jon said:
Abe Frohnman said:
I'm having problems using an ArrayList. The two lines of relevant code
are here:

roomsArray.Add(new Area(roomNumber));

labelRoomNumber.Text = "You're currently building room number " +
(Area)(roomsArray[0]).RoomNum;

When I try to compile this, I get this error message: 'object' does not
contain a definition for 'RoomNum'.

How can I access a method from my Area class when its part of an
ArrayList?


Currently it's being parsed as:

(Area) ( roomsArray[0].RoomNum )

You just need to cast slightly differently:

((Area)(roomsArray[0])).RoomNum
 

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