access to subclass properties

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello everyone.

I have an abstract class named Person. I also have a class that inherits
from Person which is named Client.

When I instantiate these as follows:

Dim nuClient as Person
nuClient = New Client

I do not get access to the properties and methods of class Client in VS2003
because the variable is of type Person.
I understand that if I have a Client property that overrides a Person
property, that the Client property or method will be the one that is
executed. This is what I want. However I also want access to the other
Client properties and methods.

What do I need to do to have access to Client's properties and methods?
Change the Person class so that it is not abstract but inheritable?

Thank you for your help.

-Mark
 
Mark Petruszak said:
Dim nuClient as Person
nuClient = New Client : :
What do I need to do to have access to Client's properties and methods?

It's called a typecast.

CType( nuClient, Client).ClientOnlyMethod( "123-45-6789")

The CType conversion function changes nuClient (of type Person) into an
object of type Client for purposes of the ensuing call to ClientOnlyMethod( )
which I'm assuming doesn't exist on base class Person.

If nuClient turns out not to actually be a Client -- then CType( ) will throw
an InvalidCastException.


Derek Harmon
 
Thank you Derek, I hadn't thought of type casting. I'll give it a whirl.

Your assupmption was correct. There are properties and methods peculiar to
Client which don't exist in Person.

Thank you for your help.
 
Mark Petruszak said:
I have an abstract class named Person. I also have a class that
inherits from Person which is named Client.

When I instantiate these as follows:
Dim nuClient as Person
nuClient = New Client

I do not get access to the properties and methods of class Client
in VS2003 because the variable is of type Person.

Correct. Your code states that you wish to treat this object as
the more generic "Person" type, not as the "Client" type. So 'Studio
only gives you access to the Person Properties, Methods, etc.
What do I need to do to have access to Client's properties and
methods?

The simplest way is to put the object into a variable of type Client;
then you can "do" Client things with it.

Dim nuPerson as Client

If you need to do both Person /and/ Client things, your code gets a
little messier, as in

Dim nuClient as Person
nuClient = New Client

nuClient.PersonMethod()
DirectCast( nuClient, Client ).ClientMethod()
Change the Person class so that it is not abstract but inheritable?

Your class definitions are fine as they are.

HTH,
Phill W.
 

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