473,748 Members | 10,539 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Overloading and method signatures.

So this is ok:

class foo {
int myMethod(string , int) { ... }
int myMethod(string , double) { ... }
}

But this is not:

class foo {
int myMethod(string ) { ... }
double myMethod(string ) { ... }
}

The compiler complains "Class 'foo' already defines a member called
'myMethod' with the same parameter types'. Well, yes that's true, but my
question is "why on earth is the return type not considered part of a
method's signature for purposes of overload disambiguation? "

Can someone with perhaps a deeper understanding of C# clue me in as to why
this is the case? When calling the method in contexts such as:

int I = myMethod("Bob") ;
double D = myMethod("Sue") ;

There's no ambiguity about which implementation is meant. In contexts such
as:

Decimal Num = 1.0 + myMethod("Dave" ) + 7;

The compiler could simply require a cast to disambiguiate which type is
intended. The C# compiler doesn't seem to have any problem requiring silly
casts in all sorts of other places, so why would it be so bad to require
this:

Decimal Num = 1.0 + (double)myMetho d("Dave") + 7;

Perhaps I'm just irritated that C# is making my job more difficult at the
moment because return type is _not_ part of a method's signature, but I'd
like to understand why and hear any suggestions as to ways to handle
situations where you do want to have two methods with the same name that
differ only in return type.
Nov 16 '05 #1
7 2115
Hi,

very simple:

object o = foo_instance.my Method( "which one to call?")

Now imagine the complexity when you return a reference type , this why you
need to differentiate in parameters and the return is not considered

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Marcos Stefanakopolus" <ta*******@hotm ail.com> wrote in message
news:42******** @news.microsoft .com...
So this is ok:

class foo {
int myMethod(string , int) { ... }
int myMethod(string , double) { ... }
}

But this is not:

class foo {
int myMethod(string ) { ... }
double myMethod(string ) { ... }
}

The compiler complains "Class 'foo' already defines a member called
'myMethod' with the same parameter types'. Well, yes that's true, but my
question is "why on earth is the return type not considered part of a
method's signature for purposes of overload disambiguation? "

Can someone with perhaps a deeper understanding of C# clue me in as to why
this is the case? When calling the method in contexts such as:

int I = myMethod("Bob") ;
double D = myMethod("Sue") ;

There's no ambiguity about which implementation is meant. In contexts
such as:

Decimal Num = 1.0 + myMethod("Dave" ) + 7;

The compiler could simply require a cast to disambiguiate which type is
intended. The C# compiler doesn't seem to have any problem requiring
silly casts in all sorts of other places, so why would it be so bad to
require this:

Decimal Num = 1.0 + (double)myMetho d("Dave") + 7;

Perhaps I'm just irritated that C# is making my job more difficult at the
moment because return type is _not_ part of a method's signature, but I'd
like to understand why and hear any suggestions as to ways to handle
situations where you do want to have two methods with the same name that
differ only in return type.

Nov 16 '05 #2
So you could say that problem is because the compiler will get confused
as it is being *helpful* and wants to do the implicit conversion.

I guess this is a choice made in designing the language. If it was
chosen to flag this as a compile time error, you would be able to have
method overloading w/same parameters but with different return type.
IIRC, this is how C++ behaves as well.

-----
Ajay Kalra
aj*******@yahoo .com

Nov 16 '05 #3
The simple answer is because of precisely the example you gave: there
are many situations in which the compiler can't determine which one to
call.

The more complex answer involves looking at the solution you proposed,
and seeing it from the compiler's point of view. You suggested this bit
of code to disambiguate the situation:

class foo {
int myMethod(string ) { ... }
double myMethod(string ) { ... }
}

Decimal Num = 1.0 + (double)myMetho d("Dave") + 7;

where the cast to (double) tells the compiler which method to call.
However, compilers don't see things that way.

As soon as you evaluate 'myMethod("Dave ")', it becomes a value to be
used in a calculation. The compiler doesn't "look ahead," as such, to
see what kind of value it's going to need in the expression, or to what
kind of value this value is going to be cast. That's all "in the
future" as it were.

This isn't unique to C#, by the way. I know of no language that allows
overloads that will distinguish between two methods based on their
return values alone. In particular, C++ and Java, which provided
inspiration for C#, don't allow it, either.

Nov 16 '05 #4
Bruce Wood <br*******@cana da.com> wrote:
This isn't unique to C#, by the way. I know of no language that allows
overloads that will distinguish between two methods based on their
return values alone. In particular, C++ and Java, which provided
inspiration for C#, don't allow it, either.


IL itself does, interestingly enough. The method calls specify the
return type of the called method.

The CTS allows it, but the CLS doesn't - in other words, any class
which has two methods defined which differ only by return type doesn't
have to be consumed by a CLS-compliant language.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
We need to add class name or instance ref in this code
Decimal Num = 1.0 + (double)myMetho d("Dave") + 7;


Foo f = new Foo();

Decimal Num = 1.0 + (double) f.myMethod("Dav e") + 7;

now the cast to double is invalid!!

Nov 16 '05 #6
> The compiler complains "Class 'foo' already defines a member called
'myMethod' with the same parameter types'. Well, yes that's true, but my question is "why on earth is the return type not considered part of a method's signature for purposes of overload disambiguation? " This is because, the compiler (any compiler i know of) doesnt force you
either to assign the return value that a method or function returns to
appropriate type.

wrt. the example oulined by you, i can do away as follows:

private void CallFoo()
{
myMethod('foo') ;
}

The callFoo() is really not concerned about the return value of
myMethod but is only making the call to myMethod(), there is no basis
here for the compiler to figure out at run time as to which version of
myMethod() is called.

Thanks,

Raaj, Umapathy.

Marcos Stefanakopolus wrote: So this is ok:

class foo {
int myMethod(string , int) { ... }
int myMethod(string , double) { ... }
}

But this is not:

class foo {
int myMethod(string ) { ... }
double myMethod(string ) { ... }
}

The compiler complains "Class 'foo' already defines a member called
'myMethod' with the same parameter types'. Well, yes that's true, but my question is "why on earth is the return type not considered part of a method's signature for purposes of overload disambiguation? "

Can someone with perhaps a deeper understanding of C# clue me in as to why this is the case? When calling the method in contexts such as:

int I = myMethod("Bob") ;
double D = myMethod("Sue") ;

There's no ambiguity about which implementation is meant. In contexts such as:

Decimal Num = 1.0 + myMethod("Dave" ) + 7;

The compiler could simply require a cast to disambiguiate which type is intended. The C# compiler doesn't seem to have any problem requiring silly casts in all sorts of other places, so why would it be so bad to require this:

Decimal Num = 1.0 + (double)myMetho d("Dave") + 7;

Perhaps I'm just irritated that C# is making my job more difficult at the moment because return type is _not_ part of a method's signature, but I'd like to understand why and hear any suggestions as to ways to handle
situations where you do want to have two methods with the same name that differ only in return type.


Nov 16 '05 #7
Thanks to everyone who replied. After thinking about it, I realized that
some part of my brain has been spoiled by Perl. Perl provides an expression
context when evaluating expressions, so that code can check whether it's
supposed to return a number or a string or a list or whatever, and then do
the right thing. C# acts in a vaguely similar way when it lets you say
things like:

int theNumber = 7;
Console.WriteLi ne("the number is " + theNumber);

Knowing that the expression is in "string context", the compiler calls
ToString on theNumber for you. Nice feature, but it led me to thinking that
knowledge of the calling context was something that the compiler would take
into account in a more general way. Sadly, not the case.

I'll post a separate thread to discuss possible work-arounds.
"Bruce Wood" <br*******@cana da.com> wrote in message
news:11******** *************@f 14g2000cwb.goog legroups.com...
The simple answer is because of precisely the example you gave: there
are many situations in which the compiler can't determine which one to
call.

The more complex answer involves looking at the solution you proposed,
and seeing it from the compiler's point of view. You suggested this bit
of code to disambiguate the situation:

class foo {
int myMethod(string ) { ... }
double myMethod(string ) { ... }
}

Decimal Num = 1.0 + (double)myMetho d("Dave") + 7;

where the cast to (double) tells the compiler which method to call.
However, compilers don't see things that way.

As soon as you evaluate 'myMethod("Dave ")', it becomes a value to be
used in a calculation. The compiler doesn't "look ahead," as such, to
see what kind of value it's going to need in the expression, or to what
kind of value this value is going to be cast. That's all "in the
future" as it were.

This isn't unique to C#, by the way. I know of no language that allows
overloads that will distinguish between two methods based on their
return values alone. In particular, C++ and Java, which provided
inspiration for C#, don't allow it, either.

Nov 16 '05 #8

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

Similar topics

3
2888
by: Iyer, Prasad C | last post by:
I am new to python. I have few questions a. Is there something like function overloading in python? b. Can I overload __init__ method Thanks in advance regards
6
1649
by: james b | last post by:
Hi all, I'm trying to do something with method overloading and I can't seem to get it to work my code is along the lines of public int method1(int a, int b, int c){ //method body } public int method1(int a, int b){ method1(a,b,null}
31
2292
by: | last post by:
Hi, Why can I not overload on just the return type? Say for example. public int blah(int x) { }
6
1218
by: Armin Zingler | last post by:
Hi group, why can't I overload these procedures? Private Sub proc(ByVal arg As String()) End Sub Private Sub proc(ByVal ParamArray arg As String()) End Sub
6
6172
by: Fernando Berretta | last post by:
Hello, Is there some way to truly Overload a WebMethod (Without using MessageName) ?? Thanks in advance, Fernando
10
3381
by: Mihai Osian | last post by:
Hi everyone, Given the code below, can anyone tell me: a) Is this normal behaviour ? b) If it is, what is the reason behind it ? I would expect the A::method(int) to be inherited by B. Compiler: gcc 4.1, Linux Thanks,
11
28141
by: placid | last post by:
Hi all, Is it possible to be able to do the following in Python? class Test: def __init__(self): pass def puts(self, str): print str
15
1304
by: AJ | last post by:
Why does the following behave as it does...? public class Base { public void Add( byte b ) { } } public class TestClass : Base
22
3620
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float v);
0
8831
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9374
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9325
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9249
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6076
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4607
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4876
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3315
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.