newbie qestion about dervided class

  • Thread starter Thread starter Jabco
  • Start date Start date
J

Jabco

I was a VB programmer, new to C#. Hope you can understand what I'm
trying to say:

I have a Class B dervided from base Class A.
Now I have an object that is an instance of A.
How can I make an object from B that references to the instance of A?
 
why you need that?
try the other way around and it will work without problems.
 
why you need that?
try the other way around and it will work without problems.
Alex,

I realize this idea could be impossible in VB.NET. Hope can find some
luck in C#.

I have a class:

public class MyTcpClient: TcpClient
{
public MyTcpClient()
{
}

public bool IsConnected()
{
return this.Client.Connected;
}
}

Then I use it in VB.NET:

tcpClient = Listener.AcceptTcpClient() 'compiler disallows this impilict
conversion

or

tcpClient = CType(Listener.AcceptTcpClient(), MyTcpClient) 'got invalid
cast exception


Thanks for your help anyway.
 
You've got it backwards.

You can always safely cast from the derived class to the base class because any method call or property access is guaranteed to work. The opposite is certainly not true and that's why you can't do it
 

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