problem accessing members of class

  • Thread starter Thread starter Jon Agiato
  • Start date Start date
J

Jon Agiato

Hello all,

Here is the jist of my situation:
I have an ArrayList of class A.
class A contains a Stack of class B.
I need to add instances of class B to the Stack in each instance of class A
within the ArrayList like so (except correct, hehe):
classA[blahblah].stackInA.add(new classB(blah));

The problem is, this syntax gives me the following error:
'object' does not contain a definition for 'stackInA'

Thanks!
 
Jon Agiato said:
Here is the jist of my situation:
I have an ArrayList of class A.
class A contains a Stack of class B.
I need to add instances of class B to the Stack in each instance of class A
within the ArrayList like so (except correct, hehe):
classA[blahblah].stackInA.add(new classB(blah));

The problem is, this syntax gives me the following error:
'object' does not contain a definition for 'stackInA'

You need to cast:

((ClassA)classA[blahblaj]).stackInA.add(new ClassB(blah));
 
Hi Jon,

That's cause ArrayList[Index] return an object, therefore you have to cast
it to the correct type:
((ClassA)classA[index]).stackInA.add(new classB(blah));

To avoid this you better use a strong type collection.

Hope this help,
 
Jon Agiato said:
Thank you for your help! This behaviour is interesting, could you please
explain the reasoning behind it? I have extensive experience with C++ and
am a relative novice to C# and find this peculiar. Does C# use a lot of
casting?

When it comes to collections, yes. ArrayList's access methods/indexer
have a return type of just "object" - you need to cast to tell the
compiler which type you're expecting to come back.

Generics should help this a lot in the next version of C# though -
they're similar to (but not the same as) C++ templates.
 
Hello Ignacio,
I am looking for a good reference book that would detail the .NET framework
classes, would you have any suggestions? Thank you, your comments were
helpful!

--
Jon Agiato
(e-mail address removed)
AOL IM: agiatojon
http://www.agiato.net
Ignacio Machin said:
Hi Jon,

That's cause ArrayList[Index] return an object, therefore you have to cast
it to the correct type:
((ClassA)classA[index]).stackInA.add(new classB(blah));

To avoid this you better use a strong type collection.

Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Jon Agiato said:
Hello all,

Here is the jist of my situation:
I have an ArrayList of class A.
class A contains a Stack of class B.
I need to add instances of class B to the Stack in each instance of
class
A
within the ArrayList like so (except correct, hehe):
classA[blahblah].stackInA.add(new classB(blah));

The problem is, this syntax gives me the following error:
'object' does not contain a definition for 'stackInA'

Thanks!
--
Jon Agiato
(e-mail address removed)
AOL IM: agiatojon
http://www.agiato.net
 
Wrox has a slew of good books. The Professional C#, 2nd. edition is quite
good I think (although I own most of their books). Books are great :-)
 
Back
Top