none
Is it possible to set an Inherit class of a derived class RRS feed

  • Question

  • Hi,

    I have a derived class and I want to know if it's possible to give it an instance of the inherit class :

    public partial class GDP_Service : Microsoft.Web.Services3.WebServicesClientProtocol{
            
            private System.Threading.SendOrPostCallback PatientRevise_PRPA_IN201314UV02OperationCompleted;

    ...

    /// <remarks/>
            public GDP_Service() {
                this.SoapVersion = System.Web.Services.Protocols.SoapProtocolVersion.Soap12;
                if ((this.IsLocalFileSystemWebService(this.Url) == true)) {
                    this.UseDefaultCredentials = true;

    ....

    How can I do that ?

    The reason is because I have severals derived class with the same inherits class ans I want to use the same property for all of them.

    thanks a lot,

    Monday, June 11, 2012 3:07 PM

Answers

  • Still not sure what you want.  You left the most central part to your question, the property.

        class A
        {
        }
    
        class B : A { public A BaseClass { get; set; } }
    
        class C : A { public A BaseClass { get; set; } }

    If 'A' class were a Singleton, then all instances of B and C would share the same instance.

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

    Tuesday, June 12, 2012 5:23 PM
  • If I understand you correctly, you want do following, which is possible.

        class Program
        {
            static void Main(string[] args)
            {
                A a = new A();
                B b = new B(a);
                b.PrintB(" - Voodoo");
     
                Console.ReadKey();
            }
        }
     
        public class A
        {
            public string Print(string text)
            {
                return "A" + text;
            }
        }
     
        public class B : A
        {
            private A a;
     
            public B(A a)
            {
                this.a = a;
            }
     
            public void PrintB(string text)
            {
                Console.WriteLine("B" + A.Print(text));
            }
     
            public A A
            {
                get { return this.a; }
            }
        }


    Tuesday, June 12, 2012 6:59 PM
  • No. This is not possible unless you use explicit casting.

    class A
    {
    }
    
    class B:A
    {
    }

    So you cannot set object of A to B. So below statement gives you error,

    B b = new A();  //Compile Error

    but you can do it via casting like below,

    A a = new B();
    B b = (B)a;  

    I hope this helps.

    Please mark this post as answer if it solved your problem. Happy Programming!

    Thursday, June 14, 2012 6:07 AM

All replies

  • Can you post another example of what you need to do using simple class names:  base, derived1, derived2, etc.  It is not clear what it is that you are looking to do.  You have only posted usage of one class, and it is not clear exactly what is the "same property for all of them."

    Thanks, ahead of time.

    Rudy  =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

    Monday, June 11, 2012 3:39 PM
  • Hi Rudy,

    For example:

    A = Microsoft.Web.Services3.WebServicesClientProtocol

    public class B : A

    {

    ...

    }

    public class C : A

    {

    ...

    }

    I want to create an object of A and pass it to the derived classes. Is it possible like that ? Or I have to add some code to my derived classes ?

    Thanks,

    Tuesday, June 12, 2012 8:19 AM
  • Still not sure what you want.  You left the most central part to your question, the property.

        class A
        {
        }
    
        class B : A { public A BaseClass { get; set; } }
    
        class C : A { public A BaseClass { get; set; } }

    If 'A' class were a Singleton, then all instances of B and C would share the same instance.

    Rudy   =8^D


    Mark the best replies as answers. "Fooling computers since 1971."

    http://thesharpercoder.blogspot.com/

    Tuesday, June 12, 2012 5:23 PM
  • If I understand you correctly, you want do following, which is possible.

        class Program
        {
            static void Main(string[] args)
            {
                A a = new A();
                B b = new B(a);
                b.PrintB(" - Voodoo");
     
                Console.ReadKey();
            }
        }
     
        public class A
        {
            public string Print(string text)
            {
                return "A" + text;
            }
        }
     
        public class B : A
        {
            private A a;
     
            public B(A a)
            {
                this.a = a;
            }
     
            public void PrintB(string text)
            {
                Console.WriteLine("B" + A.Print(text));
            }
     
            public A A
            {
                get { return this.a; }
            }
        }


    Tuesday, June 12, 2012 6:59 PM
  • No. This is not possible unless you use explicit casting.

    class A
    {
    }
    
    class B:A
    {
    }

    So you cannot set object of A to B. So below statement gives you error,

    B b = new A();  //Compile Error

    but you can do it via casting like below,

    A a = new B();
    B b = (B)a;  

    I hope this helps.

    Please mark this post as answer if it solved your problem. Happy Programming!

    Thursday, June 14, 2012 6:07 AM