CollectionBase Item Member Access

  • Thread starter Thread starter DelGator
  • Start date Start date
D

DelGator

Foundation...Accessing derived class members through a variable of
their base class is just a matter of casting the variable to the
derived class.

How would you access the derived class members in a collection of type
base class where the collection is derived from CollectionBase?

I know that I'm going to feel like a fool when this is resolved, but I
have racked my brain, and almost brought down the Google servers,
single-handedly, searching for the answer.

Thanks in advance...
 
It's not clear what you're trying to do here. Are you trying to access items
that have been added to a collection class derived from CollectionBase? Or
have you added member variables to the derived class that you're trying to
access?

Could you give some more information?

..ARN.
 
Sorry...

I have a base class, let's call it a "Row", that I have created derived
classes from. So, let's call the derived classes "FileRow" and
"CookieRow". I have also created a "Row" type collection class, based
on CollectionBase.

The derived classes, "FileRow" and "CookieRow" have additional
properties from those inherited from "Row". I want to be able to access
those additional properties from within the collection.

I know how to access the derived class members when dealing with simple
base class variables. You would cast...

(FileRow)Row.derivedClassProperty

What I need is the process for accessing them inside the collection.

Hope this is clearer.

Thanks in advance...
 
Based on your further explanation, I think that the following may help.

Let's suppose the FileRow class has an integer called fileInt and the
CookieRow class has an int called cookieInt. Suppose the collection class
has a method called DoSomething(). Then you could do something like the
following:

public void DoSomething()
{
foreach( Row r in this )
{
int dRow;
if( r is FileRow )
dRow = ((FileRow)r).fileInt;
else
dRow = ((CookieRow)r).cookieInt;
}
}

The key here is the if( r is FileRow) statement. This identifies r as in
instance of the FileRow class. Then you use the (odd-looking) cast to get to
the FileRow member field.

I hope this helps.

..ARN.
 

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

Back
Top