Passing subclass as parameter

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ToChina

    #1

    Passing subclass as parameter

    Hi, I have the following code:

    class A
    {
    }

    class B : A
    {
    }

    and a method of another class which is like so:

    void MethodName(ref A blah)
    {
    blah.Whatever = 0;
    }

    I'm calling the method with the subclass B:

    B subclass = new B();
    MethodName(ref subclass);

    However, I'm getting the compiler error:

    The best overloaded method match for 'MethodName(ref A)' has some invalid
    arguments
    error CS1503: Argument '1': cannot convert from 'ref B' to 'ref A'

    What am I missing? :P


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Passing subclass as parameter

    ToChina,

    You need to cast the instance of B down to A. If you have class C which
    derives from A, and MethodName changes subclass to be an instance of C, then
    you could not assign it to the variable containing the instance of B. So,
    to fix this, you would do this:

    // Create an instance of B
    B subclass = new B();

    // Cast down to A
    A param = subclass;

    // Make the call.
    MethodName(ref param);

    You have to be careful when you are done with param though, as you can't
    guarantee that it will be an instance of type B (well, you could be, if you
    write the code).

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "ToChina" <invalid@email. address> wrote in message
    news:Zxlne.4345 2$G8.32556@text .news.blueyonde r.co.uk...[color=blue]
    > Hi, I have the following code:
    >
    > class A
    > {
    > }
    >
    > class B : A
    > {
    > }
    >
    > and a method of another class which is like so:
    >
    > void MethodName(ref A blah)
    > {
    > blah.Whatever = 0;
    > }
    >
    > I'm calling the method with the subclass B:
    >
    > B subclass = new B();
    > MethodName(ref subclass);
    >
    > However, I'm getting the compiler error:
    >
    > The best overloaded method match for 'MethodName(ref A)' has some invalid
    > arguments
    > error CS1503: Argument '1': cannot convert from 'ref B' to 'ref A'
    >
    > What am I missing? :P
    >[/color]


    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Passing subclass as parameter

      ToChina <invalid@email. address> wrote:[color=blue]
      > class A
      > {
      > }
      >
      > class B : A
      > {
      > }
      >
      > and a method of another class which is like so:
      >
      > void MethodName(ref A blah)
      > {
      > blah.Whatever = 0;
      > }
      >
      > I'm calling the method with the subclass B:
      >
      > B subclass = new B();
      > MethodName(ref subclass);
      >
      > However, I'm getting the compiler error:
      >
      > The best overloaded method match for 'MethodName(ref A)' has some invalid
      > arguments
      > error CS1503: Argument '1': cannot convert from 'ref B' to 'ref A'
      >
      > What am I missing? :P[/color]

      Parameters which are passed by reference need to be *exactly* the same
      type. Imagine if the code in MethodName said:

      blah = new A();

      then by the time the method returned, the "subclass" variable's value
      would no longer be a reference to an instance of the subclass!

      My guess, however, is that you're passing by reference when you really
      don't have to. You certainly don't have to in the example above.

      See http://www.pobox.com/~skeet/csharp/parameters.html for more
      information.

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      Working...