473,386 Members | 1,699 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,386 software developers and data experts.

Overloading a method with different return types but the same parameter lists

Hello.

I am trying to overload a method so that I have four possible working
copies. The only difference between the four methods is what to search by
and in what manner to return the results. So I have the following.

public static ArrayList GetRoles(int userID)
public static ArrayList GetRoles(string username)
public static string GetRoles(int userID) // Error 1
public static string GetRoles(string username) // Error 2

But I am receiving the following errors.

Error 1.
C:\Projects\acmsproject\trunk\acms\source\DataAcce ss\DatabaseAccess.cs(117):
Class 'acms.DataAccess.DatabaseAccess' already defines a member called
'GetRoles' with the same parameter types

Error 2.
C:\Projects\acmsproject\trunk\acms\source\DataAcce ss\DatabaseAccess.cs(120):
Class 'acms.DataAccess.DatabaseAccess' already defines a member called
'GetRoles' with the same parameter types

How can I overload these methods to provide the functionality that I want.
Thanks in advance.

Ryan Taylor
Nov 16 '05 #1
9 20568
Hello Ryan,
How can I overload these methods to provide the functionality that I
want. Thanks in advance.


You can´t.
Maybe it is supported in C# 2 !?

--
Greetings
Jochen
Nov 16 '05 #2
Jochen Kalmbach <no********************@holzma.de> wrote:
How can I overload these methods to provide the functionality that I
want. Thanks in advance.


You can´t.
Maybe it is supported in C# 2 !?


No, it's not, thank goodness. I for one don't want which method is
called to be determined by what I do with the returned value.

--
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
Sorry that is impossible.
An advice would be to rename the methods GetRoleList or something.

This is a limitation of C#, as it is supported in the Intermediate Language.

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Ryan Taylor" <rt*****@stgeorgeconsulting.com> wrote in message
news:Oj**************@TK2MSFTNGP12.phx.gbl...
Hello.

I am trying to overload a method so that I have four possible working
copies. The only difference between the four methods is what to search by
and in what manner to return the results. So I have the following.

public static ArrayList GetRoles(int userID)
public static ArrayList GetRoles(string username)
public static string GetRoles(int userID) // Error 1
public static string GetRoles(string username) // Error 2

But I am receiving the following errors.

Error 1.
C:\Projects\acmsproject\trunk\acms\source\DataAcce ss\DatabaseAccess.cs(117): Class 'acms.DataAccess.DatabaseAccess' already defines a member called
'GetRoles' with the same parameter types

Error 2.
C:\Projects\acmsproject\trunk\acms\source\DataAcce ss\DatabaseAccess.cs(120): Class 'acms.DataAccess.DatabaseAccess' already defines a member called
'GetRoles' with the same parameter types

How can I overload these methods to provide the functionality that I want.
Thanks in advance.

Ryan Taylor

Nov 16 '05 #4
Thanks. I won't be chasing my tail any longer. I hope it is supported in
2.0.

Ryan Taylor
Nov 16 '05 #5
Can't do, not in 2.0 either IIRC. Return your own collection class (i.e.
inherits ICollection). Your ToString() method (or other) on the collection
class would return the string so you could:

MyRoles roles = GetRoles(2);
Console.WriteLine(roles.count);
Console.WriteLine(roles.ToString());
roles = GetRoles("joe");

Then you only need two overloads.

public class MyRoles : ICollection
{
private ArrayList roles = new ArrayList()
....
public override string ToString()
{
StringBuilder sb = new StringBuilder();
foreach(object in roles)
{
// Do something to format.
}
return sb.ToString();
}
}
--
William Stacey, MVP

"Ryan Taylor" <rt*****@stgeorgeconsulting.com> wrote in message
news:Oj**************@TK2MSFTNGP12.phx.gbl...
Hello.

I am trying to overload a method so that I have four possible working
copies. The only difference between the four methods is what to search by
and in what manner to return the results. So I have the following.

public static ArrayList GetRoles(int userID)
public static ArrayList GetRoles(string username)
public static string GetRoles(int userID) // Error 1
public static string GetRoles(string username) // Error 2

But I am receiving the following errors.

Error 1.
C:\Projects\acmsproject\trunk\acms\source\DataAcce ss\DatabaseAccess.cs(117): Class 'acms.DataAccess.DatabaseAccess' already defines a member called
'GetRoles' with the same parameter types

Error 2.
C:\Projects\acmsproject\trunk\acms\source\DataAcce ss\DatabaseAccess.cs(120): Class 'acms.DataAccess.DatabaseAccess' already defines a member called
'GetRoles' with the same parameter types

How can I overload these methods to provide the functionality that I want.
Thanks in advance.

Ryan Taylor


Nov 16 '05 #6
It is not possible and it should not be possible in C# 2!
How can compiler know which version you would like to use if you call

GetRoles(id);

and ignore the return type. This is the reason why function overloading does
not include the return type.

However you can implement these method as below.

void GetRoles(int id, out ArrayList al);
void GetRoles(string username, out ArrayList al);
void GetRoles(int id, out string s);
void GetRoles(string username, out string s);

Anyway, these are a bit ugly, in my opinion !!!

Best regards
/C.Aroonyingmongkol

"Ryan Taylor" <rt*****@stgeorgeconsulting.com> wrote in message
news:Oj**************@TK2MSFTNGP12.phx.gbl...
Hello.

I am trying to overload a method so that I have four possible working
copies. The only difference between the four methods is what to search by
and in what manner to return the results. So I have the following.

public static ArrayList GetRoles(int userID)
public static ArrayList GetRoles(string username)
public static string GetRoles(int userID) // Error 1
public static string GetRoles(string username) // Error 2

But I am receiving the following errors.

Error 1.
C:\Projects\acmsproject\trunk\acms\source\DataAcce ss\DatabaseAccess.cs(117): Class 'acms.DataAccess.DatabaseAccess' already defines a member called
'GetRoles' with the same parameter types

Error 2.
C:\Projects\acmsproject\trunk\acms\source\DataAcce ss\DatabaseAccess.cs(120): Class 'acms.DataAccess.DatabaseAccess' already defines a member called
'GetRoles' with the same parameter types

How can I overload these methods to provide the functionality that I want.
Thanks in advance.

Ryan Taylor

Nov 16 '05 #7
C.Aroonyinmongkol wrote:
It is not possible and it should not be possible in C# 2!
How can compiler know which version you would like to use if you call

GetRoles(id);

and ignore the return type. This is the reason why function overloading does
not include the return type.


The other reason is that passing a by-result overloaded
method as an argument to another method, which in turn
has overloaded parameters, is ambigous:

class A {
string Get();
int Get();
}

class B {
void Process(string s);
void Process(int i);
}

b.Process(a.Get());

which Get() should be called? you'll end up having to cast:

b.Process((int)Get());

and the benefit of by-result overloading is gone.

Bye
Rob
Nov 16 '05 #8
Ryan,
If you can handle not having the methods be static, one way I get around
this is to implement different interfaces:

public interface IRolesArrayList
{
ArrayList GetRoles(int userID)
ArrayList GetRoles(string username)
}

public interface IRolesString
{
string GetRoles(int userID)
string GetRoles(string username)
}

public class MyClass : IRolesArrayList, IRolesString
{
ArrayList IRolesArrayList.GetRoles(int userID)
{
//implement code
}

ArrayList IRolesArrayList.GetRoles(string userName)
{
//implement code
}

string IRolesArrayList.GetRoles(int userID)
{
//implement code
}

string IRolesArrayList.GetRoles(string userName)
{
//implement code
}
}

--
Lateralus [MCAD.Net]
"Ryan Taylor" <rt*****@stgeorgeconsulting.com> wrote in message
news:Oj**************@TK2MSFTNGP12.phx.gbl...
Hello.

I am trying to overload a method so that I have four possible working
copies. The only difference between the four methods is what to search by
and in what manner to return the results. So I have the following.

public static ArrayList GetRoles(int userID)
public static ArrayList GetRoles(string username)
public static string GetRoles(int userID) // Error 1
public static string GetRoles(string username) // Error 2

But I am receiving the following errors.

Error 1.
C:\Projects\acmsproject\trunk\acms\source\DataAcce ss\DatabaseAccess.cs(117): Class 'acms.DataAccess.DatabaseAccess' already defines a member called
'GetRoles' with the same parameter types

Error 2.
C:\Projects\acmsproject\trunk\acms\source\DataAcce ss\DatabaseAccess.cs(120): Class 'acms.DataAccess.DatabaseAccess' already defines a member called
'GetRoles' with the same parameter types

How can I overload these methods to provide the functionality that I want.
Thanks in advance.

Ryan Taylor

Nov 16 '05 #9
Wow. Thanks everyone for the great support. I have a pretty simple need, so
I went for a simple solution. I named my function that return an ArrayList
as GetRolesList(...) and the function that returns a string I named
GetRoles(...). GetRoles(...) then calls GetRolesList(...) and loops through
the resulting array to build my string and return that to the user.

There are definately some interesting things that can be done in C#. I am a
newbie at the whole .NET philosophy, but so far the Microsoft juice isn't as
sour as it once was.

Thanks again for the wide support.

Ryan Taylor
Nov 16 '05 #10

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

Similar topics

5
by: Murat Tasan | last post by:
so i have a situation that i know cannot be solved directly, and i've already worked around it, but i was hoping to find an explanation as to why this behavior is as it is... i have an abstract...
2
by: Aryeh M. Friedman | last post by:
If I have something like this: class NumberException { }; class Number { public: ... virtual unsigned long getValue() {throw(NumberException);}; ...
10
by: Peter | last post by:
Hi, how can I do this (I don't really want to do this but what I do want the same function name but have different return types and the compiler keeps saying "public function .... they differ only...
2
by: Bit byte | last post by:
I have a base (abstract) class with a public method foo delared as: virtual BaseClass* foo(..)=0; I wnat to derive two classes A and B from Baseclass so that I return a pointer for A and B...
3
by: hareen | last post by:
hi can any one tell how to handle .NET Dll Methods Which have return types Generic Lists and DataTables
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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
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...

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.