Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inherited class as reference

I would like to use a function that has a parameter of type A. I would like to pass it a class B which derived from A. But C# does not want to.

class A
{
 int m_a;
}

class B : A
{
 int m_b;
}

void ShowandEditData( ref A _myvar)
{
 ...
}

Now, i would like to do something like this:

B myB = new B();

ShowandEditData(ref myB);

I have tried many things like casting or using the base attribute. I guess I'm doing it wrong.

Is it possible to do that with C#?

--Code Edited due to pseudocode creating confusion . (sorry, first post)

like image 517
Xavier Avatar asked Dec 26 '22 12:12

Xavier


1 Answers

just remove the ref from your method declaration. Like so:

void ShowData(A _myvar)
{
 ...
}
like image 81
Klaus Byskov Pedersen Avatar answered Dec 28 '22 07:12

Klaus Byskov Pedersen