重载具有不同返回类型但具有相同参数列表的方法 [英] Overloading a method with different return types but the same parameter lists

查看:96
本文介绍了重载具有不同返回类型但具有相同参数列表的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。


我正在尝试重载方法,以便我有四个可能的工作

副本。这四种方法之间的唯一区别是通过

搜索什么以及以什么方式返回结果。所以我有以下内容。


public static ArrayList GetRoles(int userID)

public static ArrayList GetRoles(string username)

public static string GetRoles(int userID)//错误1

公共静态字符串GetRoles(字符串用户名)//错误2


但我收到以下内容错误。


错误1.

C:\Projects\acmsproject\trunk \acms \ source \ DataAcce ss \DatabaseAccess。 cs(117):

Class''acms.DataAccess.DatabaseAccess''已经定义了一个名为

''GetRoles''的成员,其参数类型相同


错误2.

C:\Projects\acmsproject\trunk\acms\source \ DataAcce ss \DatabaseAccess.cs(120):

类''acms.DataAccess.DatabaseAccess''已经定义了一个名为

''GetRoles''的成员,其参数类型相同


如何重载这些方法提供我想要的功能。

提前谢谢。


Ryan Taylor

解决方案

Hello Ryan,

如何重载这些方法以提供我想要的功能。在此先感谢。




你不能。

也许它在C#2中得到支持!?


-

问候

Jochen


Jochen Kalmbach< no ****** **************@holzma.de>写道:

如何重载这些方法以提供我想要的功能。提前谢谢。



你不能。
也许它在C#2中得到支持!?




不,这不是,谢天谢地。我一个人不想要哪种方法

被称为由我对返回值做的决定。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复小组,请不要给我发邮件


< blockquote>抱歉这是不可能的。

建议重命名方法GetRoleList或其他东西。


这是C#的限制,因为它受支持中级语言。


-

问候,

DennisJDMyrén

奥斯陆Kodebureau

" Ryan Taylor" < RT ***** @ stgeorgeconsulting.com>在消息中写道

新闻:Oj ************** @ TK2MSFTNGP12.phx.gbl ...

你好。

我正在尝试重载方法,以便我有四个可能的工作副本。这四种方法之间的唯一区别是通过
搜索以及以何种方式返回结果。所以我有以下几点。

public static ArrayList GetRoles(int userID)
public static ArrayList GetRoles(string username)
公共静态字符串GetRoles(int userID)//错误1
public static string GetRoles(string username)//错误2

但我收到以下错误。

错误1.

C:\Projects\acmsproject\trunk \acms \source \DataAcce ss \DatabaseAccess.cs(117):Class''acms.DataAccess.DatabaseAccess''已经定义了一个名为
的成员''GetRoles''具有相同的参数类型

错误2.

C:\Projects \acmsproject\trunk\acms\source \ DataAcce ss \DatabaseAccess.cs(120):类''acms.DataAccess.DatabaseAccess''已经定义了一个名为
''GetRoles''的成员,具有相同的参数类型

我怎样才能超载这些方法提供了我想要的功能。
提前致谢。
<瑞安泰勒



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

解决方案

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


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


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



这篇关于重载具有不同返回类型但具有相同参数列表的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆