473,395 Members | 1,692 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Reference base class parameter, is this possible...

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.

Nov 16 '05 #1
5 1665
<"Dan =o\)" <danielbass [at] postmaster [dot] co [dot] uk>> wrote:
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 Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
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.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<"Dan =o\)" <danielbass [at] postmaster [dot] co [dot] uk>> wrote:
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 Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #3

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)" <danielbass [at] postmaster [dot] co [dot] uk> wrote in message
news:eE**************@TK2MSFTNGP14.phx.gbl...
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.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<"Dan =o\)" <danielbass [at] postmaster [dot] co [dot] uk>> wrote:
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 Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 16 '05 #4
<"Dan =o\)" <danielbass [at] postmaster [dot] co [dot] uk>> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
<"Dan =o\)" <danielbass [at] postmaster [dot] co [dot] uk>> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Gordon Moore | last post by:
Hi, I'm new to using xml/xslt and although I can create an xml document using the dataset.WriteXml statement, and I have created an xslt to transform the xml into the output I want, I have to...
5
by: Javier Campos | last post by:
WARNING: This is an HTML post, for the sake of readability, if your client can see HTML posts, do it, it doesn't contain any script or virus :-) I can reformat a non-HTML post if you want me to (and...
7
by: Santi | last post by:
I have two classes: Product and Fruit which inherits from Product. If I try to narrow the reference to the base type by a cast, I always get a reference to the inherited type. For example: ...
0
by: Richard Gregory | last post by:
Hi, I have the wsdl below, for an Axis web service, and when I select Add Web Refernce in Visual Studio the proxy is missing a class representing the returnedElementsType (see reference.cs below...
6
by: roland.bali | last post by:
Hi, Here is the basic setup, my base class is Shoe which has a child class called Sandal. I would like to create objects by calling Sandal.Load. But without overloading Load in Sandal and...
13
by: docschnipp | last post by:
Hi, I have a bunch of object derived from the same base class. They all share the same constructor with some parameters. Now, instead of using a large switch() statement where I call every...
3
by: josh | last post by:
Hi I noticed that when I make a function with a base class parameter that is a reference than at run-time the compiler calls a base class function also if I have passed a derived object. But if...
10
by: JurgenvonOerthel | last post by:
Consider the classes Base, Derived1 and Derived2. Both Derived1 and Derived2 derive publicly from Base. Given a 'const Base &input' I want to initialize a 'const Derived1 &output'. If the...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.