locked
How to call a method that takes inherited class as parameter? RRS feed

  • Question

  •  Hi,
     
    I have a base class and some classes are inherited from it. I created some overloaded methods that takes those classes as parameters. In a method I only get an instance of the base class whose type is one of its descendant. How can I call it in an easy way? (not creating a Dictionary of inherited classes with values of Type etc.)

    Here is an example:

    class BaseClass{}

    class InheritedA:BaseClass{}

    class InheritedB:BaseClass{}

    methods:

    void DoSthing(InheritedA inst){}
    void DoSthing(InheritedB inst){}


    My calling code:

    private void Method1()
    {
    BaseClass inst=new InheritedA(); //cannot change the way it is given
    DoSthing(inst as ???? ) //I don't know what to insert here: inst as inst.GetType().Name? I really need the instance's class type

    }

    Probably there is an easy way I just don't get it now :)


    Andris
    Andris
    Thursday, October 16, 2008 9:56 PM

Answers

  • Good morning Andris. This forum is for helping customers interoperating with Windows via our open protocol documentation. Your issue does not appear to be related to our protocol documentation. Your question may be better answered by posting on the 'Visual C++ Language' forum at http://social.msdn.microsoft.com/forums/en-US/vclanguage/threads/.

    Regards,
    Bill Wesse

    Escalation Engineer
    • Proposed as answer by Bill Wesse MSFT Friday, October 17, 2008 9:57 AM
    • Marked as answer by David DoBell Tuesday, October 28, 2008 4:02 AM
    Friday, October 17, 2008 9:57 AM

All replies

  • Good morning Andris. This forum is for helping customers interoperating with Windows via our open protocol documentation. Your issue does not appear to be related to our protocol documentation. Your question may be better answered by posting on the 'Visual C++ Language' forum at http://social.msdn.microsoft.com/forums/en-US/vclanguage/threads/.

    Regards,
    Bill Wesse

    Escalation Engineer
    • Proposed as answer by Bill Wesse MSFT Friday, October 17, 2008 9:57 AM
    • Marked as answer by David DoBell Tuesday, October 28, 2008 4:02 AM
    Friday, October 17, 2008 9:57 AM
  • Hi,

    Yes, I know that this forum isn't for that type of question I asked but as I noted in the other duplicate of this message (which ended up in the Sandbox thread), I was unable to set the location of the thread I liked to put this question in (General C# forum) as only 10-12 forums were presented in the listbox (I could put it into Interop, Sandbox, High Capability Systems, Windows Server programming for something, how to create a hybrid car from milk bottles thread etc. :) but the regular forums cannot be found. Can you tell me how could I put it there?
    Andris
    Sunday, October 19, 2008 11:23 AM