passing polymorphic types by reference

J

Jeremy

C++ guy here. Why can't I do this?

class A
{
public int member = 0;
}

class B : A
{
}

class Form
{
void Main()
{
B ObjB = new B();
DoSomethingWithMember(ref B); <------ This won't compile
}

void DoSomethingWithMember(ref A)
{
A.member++;
}
}

Error is:
error CS1503: Argument '1': cannot convert from 'ref B' to 'ref A''

How am I supposed to use polymorphic types if I can't chuck objects
around by referencing their base?

Cheers,
Jeremy
 
J

Jeremy

C++ guy here. Why can't I do this?

class A
{
public int member = 0;

}

class B : A
{

}

class Form
{
void Main()
{
B ObjB = new B();
DoSomethingWithMember(ref B); <------ This won't compile
}

void DoSomethingWithMember(ref A)
{
A.member++;
}

}

Error is:
error CS1503: Argument '1': cannot convert from 'ref B' to 'ref A''

How am I supposed to use polymorphic types if I can't chuck objects
around by referencing their base?

Cheers,
Jeremy

DoSomethingWithMember(ref B); should of course be
DoSomethingWithMember(ref ObjB);
 
H

Hans Kesting

(e-mail address removed) explained :
C++ guy here. Why can't I do this?

class A
{
public int member = 0;
}

class B : A
{
}

class Form
{
void Main()
{
B ObjB = new B();
DoSomethingWithMember(ref B); <------ This won't compile
}

void DoSomethingWithMember(ref A)
{
A.member++;
}
}

Error is:
error CS1503: Argument '1': cannot convert from 'ref B' to 'ref A''

How am I supposed to use polymorphic types if I can't chuck objects
around by referencing their base?

Cheers,
Jeremy

In your code, you don't need the "ref" keyword. You can still access
(and change) members if you pass the reference by value.

You only would need a "ref" if you planned on changing the reference
itself:

void DoSomethingWithA(ref A objA)
{
objA = new A();
}

And this also explains your problem: as the code could change that
reference (as you stated by using that "ref" keyword), it needs to land
in an "A" (or a baseclass of "A"), not in some derived class.

Hans Kesting
 
N

Nils Gösche

C++ guy here. Why can't I do this?

class A
{
public int member = 0;
}

class B : A
{
}

class Form
{
void Main()
{
B ObjB = new B();
DoSomethingWithMember(ref B); <------ This won't compile
}

void DoSomethingWithMember(ref A)
{
A.member++;
}
}

Error is:
error CS1503: Argument '1': cannot convert from 'ref B' to 'ref A''

Consider:

void DoSomethingWithMember(ref A ObjA)
{
ObjA = new A();
ObjA.member++;
}

Now back in Main, ObjB contains an object of type A but not B which
kinda sucks.
How am I supposed to use polymorphic types if I can't chuck objects
around by referencing their base?

Why are you calling by reference in the first place? Just pass ObjB
without the ref.

Regards,
 
J

Jeremy

In your code, you don't need the "ref" keyword. You can still access
(and change) members if you pass the reference by value.

You only would need a "ref" if you planned on changing the reference
itself:
Hans Kesting

Aha! Yes, I was forgetting that C# passes around references to objects
rather than the objects themselves.
Many thanks!
 
R

Rudy Velthuis

C++ guy here. Why can't I do this?

class A
{
public int member = 0;
}

class B : A
{
}

class Form
{
void Main()
{
B ObjB = new B();
DoSomethingWithMember(ref B); <------ This won't compile

Because if you have a parameter that must be passed by reference, you
can't simply pass a derived class. Now if you simply passed by value,
it should work:

class Form
{
void Main()
{
B ObjB = new B();
DoSomethingWithMember(B); // <------ This should compile
}

void DoSomethingWithMember(A)
{
A.member++;
}
}


--
Rudy Velthuis http://rvelthuis.de

"If you need more than five lines to prove something, then you
are on the wrong track"
-- Edgser W. Dijkstra's mother [ibid, p. 55]
 
R

Rudy Velthuis

Rudy said:
Because if you have a parameter that must be passed by reference, you
can't simply pass a derived class. Now if you simply passed by value,
it should work:

class Form
{
void Main()
{
B ObjB = new B();
DoSomethingWithMember(B); // <------ This should compile
}

void DoSomethingWithMember(A)
{
A.member++;
}
}

I see that was explained already. The example where a new A is created
explains it all.
 
M

Michael Starberg

Rudy Velthuis said:
(e-mail address removed) wrote:
DoSomethingWithMember(B); // <------ This should compile

No it should not. I think you ment passing ObjB.

- Michael Starberg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top