reven
reven

Reputation: 223

Inheritance and collection of objects

i have a basic question.

lets say we have 3 classes:

Class S, class A, class B
A and B inherits S.

A has a property A1 and B has a property B1.

we also have a collection of objects that has A and B objects.

ex. 
Dim c as Collection = new Collection
c.add(new A)
c.add(new B)

Now we want to make a general object that will read from the collection.

ex .
Dim  obj as S

how can we cast obj in order to see properties A1 or B1 according to the class;

Upvotes: 0

Views: 63

Answers (1)

Simon
Simon

Reputation: 6152

You can use TypeOf to test the object type before casting although TryCast might be better depending on what you want to do. Also take a look at DirectCast.

Upvotes: 2

Related Questions