Reference base class parameter, is this possible...

  • Thread starter Thread starter Dan =o\)
  • Start date Start date
D

Dan =o\)

given this brief scenario, is this possible somehow?

class A
{
public SomeMethod()
{
}
}


class B : public A
{
public AnotherMethod()
{
}
}

class C : public A
{
public AnotherMethod()
{
}
}


then in my code to have some parameter:

void myMethod ( ref A MyAobj )
{
// do some stuff like calling SomeMethod()
}


finally, someone in my code I do this:

// ...

B MyBobj = new B();
C MyCobj = new C();

myMethod ( ref B );
myMethod ( ref C );

// ...


The compiler says firstly that the type "ref A" is
expected on "myMethod", so i changed it to this:

// ...

B MyBobj = new B();
C MyCobj = new C();

myMethod ( ref (A)B );
myMethod ( ref (A)C );

// ...


Of course this won't work on a reference, because an
lvalue is expected.

So is it possible to do what I'm trying to do?

Thanks.

Daniel.
 
given this brief scenario, is this possible somehow?

<snip>

I don't think so - but you haven't given any reason to use ref
parameters in the first place. If your methods are currently returning
void, why not just pass the parameter by value and return the new value
as a normal return value?

Just in case you're confused about the point of passing parameters by
reference, see
http://www.pobox.com/~skeet/csharp/parameters.html
 
Jon,

Thanks for the reply.

The parameters are reference because I'm populating a DataGrid via a method.
The datagrid is bound to data, and can have the visibility property set to
true or false depending on the results. Passing this in as value wouldn't
work because I'd be left with a copy of the datagrid object, rather than the
object itself.

The reason I'm in this position is I wanted to make the method more generic
so I could bind data through my method to DataLists as well. With this in
mind I change the parameter to their base class since the only properties
that are accessed by the method are in the base class (or its ancestors).
[ I.E. DataSource, DataBind, and Visible. ]

Thanks again.

Dan.
 
what overloading like this?

void myMethod ( ref B MyBobj )
{
A AObj = (A)MyBobj;
myMethod ( ref AObj );
}
void myMethod ( ref C MyCobj )
{
A AObj = (A)MyCobj;
myMethod ( ref AObj );
}

void myMethod ( ref A MyAobj )
{
// do some stuff like calling SomeMethod()
}




Dan =o) said:
Jon,

Thanks for the reply.

The parameters are reference because I'm populating a DataGrid via a
method.
The datagrid is bound to data, and can have the visibility property set to
true or false depending on the results. Passing this in as value wouldn't
work because I'd be left with a copy of the datagrid object, rather than
the object itself.

The reason I'm in this position is I wanted to make the method more
generic so I could bind data through my method to DataLists as well. With
this in mind I change the parameter to their base class since the only
properties that are accessed by the method are in the base class (or its
ancestors). [ I.E. DataSource, DataBind, and Visible. ]

Thanks again.

Dan.
 
what overloading like this?

void myMethod ( ref B MyBobj )
{
A AObj = (A)MyBobj;
myMethod ( ref AObj );
}
void myMethod ( ref C MyCobj )
{
A AObj = (A)MyCobj;
myMethod ( ref AObj );
}

void myMethod ( ref A MyAobj )
{
// do some stuff like calling SomeMethod()
}

That would work, but you'd then need to do:

MyBObj = (B)AObj;

at the end of the first method and

MyCObj = (C)AObj;

at the end of the second method, otherwise you might as well pass by
value again.
 
The parameters are reference because I'm populating a DataGrid via a method.
The datagrid is bound to data, and can have the visibility property set to
true or false depending on the results. Passing this in as value wouldn't
work because I'd be left with a copy of the datagrid object, rather than the
object itself.

<snip>

I'm not at all sure I follow this. What would be creating a copy of the
datagrid object? DataGrid is a reference type, so you're already
passing *a* reference in. Unless you're actually changing the value of
the parameter (rather than the value of some of the members of the
object that the parameter value is a reference to) you don't need to
(and shouldn't) pass the reference by reference.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
Back
Top