oop - C# : Converting Base Class to Child Class -


i have class, networkclient base class :

using system.io; using system.net.sockets; using system.threading.tasks;  namespace network { using system; using system.collections.generic; using system.linq; using system.text;  public class networkclient {     public networkclient()     {         tcpclient = new tcpclient();     }     public networkclient(tcpclient client)     {         tcpclient = client;     }      public virtual bool isconnected     {         get;         private set;     }     private streamwriter writer { get; set; }     private streamreader reader { get; set; }      private tcpclient tcpclient     {         get;         set;     }      public virtual networkserverinfo networkserverinfo     {         get;         set;     }      public async virtual void connect(networkserverinfo info)     {         if (tcpclient == null)         {             tcpclient=new tcpclient();         }         await tcpclient.connectasync(info.address,info.port);         reader = new streamreader(tcpclient.getstream());         writer = new streamwriter(tcpclient.getstream());     }      public virtual void disconnect()     {         tcpclient.close();                     reader.dispose();          writer.dispose();     }      public async virtual void send(string data)     {         await writer.writelineasync(data);     }      public async virtual task<string> receive()     {         return await reader.readlineasync();     }  } } 

and have child class derived networkclient :

using system.net;  namespace network { using data; using system; using system.collections.generic; using system.linq; using system.text;  public class skyfilterclient : networkclient {     public virtual ipaddress address     {         get;         set;     }      public virtual int port     {         get;         set;     }      public virtual string sessionid     {         get;         set;     }      public virtual user userdata     {         get;         set;     }      protected virtual bool authenticate(string username, string password)     {         throw new system.notimplementedexception();     }  } } 

the problem is, when im trying cast networkclient skyfilterclient. exception thrown, unable cast object of type 'network.networkclient' type 'network.skyfilterclient'.

whats wrong code ? see stream can converted networkstream, memorystream. why networkclient can't converted skyfilter client?

as long object skyfilterclient, cast should work. here contrived example prove this:

using system;  class program {     static void main()     {         networkclient net = new skyfilterclient();         var sky = (skyfilterclient)net;     } }  public class networkclient{} public class skyfilterclient : networkclient{} 

however, if networkclient, cannot magically make become subclass. here example of that:

using system;  class program {     static void main()     {         networkclient net = new networkclient();         var sky = (skyfilterclient)net;     } }  public class networkclient{} public class skyfilterclient : networkclient{} 

however, create converter class. here example of that, also:

using system;  class program {     static void main()     {         networkclient net = new networkclient();         var sky = skyfilterclient.copytoskyfilterclient(net);     } }  public class networkclient {     public int someval {get;set;} }  public class skyfilterclient : networkclient {     public int newsomeval {get;set;}     public static skyfilterclient copytoskyfilterclient(networkclient networkclient)     {         return new skyfilterclient{newsomeval = networkclient.someval};     } } 

but, keep in mind there reason cannot convert way. may missing key information subclass needs.

finally, if want see if attempted cast work, can use is:

if(client skyfilterclient)     cast 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -