一个函数名,两个返回类型? [英] One function name, two return types?

查看:109
本文介绍了一个函数名,两个返回类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以有两个函数声明,它们采用相同的

参数,但根据函数的使用方式返回不同的类型?


函数f (x)as string

''返回一个字符串

结束函数


函数f(x)as string()

''返回一个字符串数组

结束函数


''在子程序中的某处:

Dim S As String = f(1)''调用f(x)的字符串版本


Dim A As String()= f(2)''调用f(x)的数组版本


为什么?因为我正在从文件中提取IPTC元数据,而某些元数据有单个值(例如标题),而有些可能有多个值(例如

关键字) 。我只是觉得有一个功能名称看起来更整洁。


Andrew

Is it possible to have two function declarations which take the same
parameters but return different types depending on how the function is used?

function f(x) as string
'' return a string
end function

function f(x) as string()
'' return an array of strings
end function

'' somewhere in a subroutine:

Dim S As String=f(1) '' calls the string version of f(x)

Dim A As String()=f(2) '' calls the array version of f(x)

Why? Because I''m extracting IPTC metadata from a file, and some metadata has
a single value (e.g. the caption) and some may have multiple values (e.g.
the keywords). I just think it would look neater to have one function name.

Andrew

推荐答案



嗨安德鲁,


你的函数可以返回对象然后离开计算出哪个类型是

返回给调用者。一个更复杂的解决方案是构造一个抽象接口或基类的两个类,并创建两个实现

的类。你的功能f()然后可以表现得像一个工厂,拿着X'和

返回你的界面实例。然而,实质上这就是

" Object"返回正在进行,除了你需要将它转换为数组或字符串

,具体取决于TypeOf检查。


Dim o As Object = f(p)


如果TypeOf(o)是.....


ElseIf TypeOf(o)是


Else

抛出新的InvalidOperationException(f返回无效类型)

结束如果



函数f(x)作为对象


如果(...)那么

''返回一个字符串

ElseIf( ......)

''返回一串字符串

否则

不返回

结束如果


结束子


Robin


" Andrew Morton" < ak*@in-press.co.uk.invalidwrote in message

news:%2 **************** @ TK2MSFTNGP05.phx.gbl ...

Hi Andrew,

Your function can return "Object" and then leave working out which type was
returned up to the caller. A more involved solution would be to construct
an abstract interface or base class and create two classes that implement
it. Your function "f()" can then behave like a factory, taking X''s and
returning instances of your interface. However, in essence this is what the
"Object" return is doing, apart from you need to cast it to Array or String
depending on the TypeOf check.

Dim o As Object = f ( p )

If TypeOf (o) Is .....

ElseIf TypeOf (o) Is

Else
Throw New InvalidOperationException ( "f returned an invalid type" )
End If


function f (x) As Object

If ( ... ) Then
'' return a string
ElseIf ( ... )
'' return an array of strings
Else
Return Nothing
End If

End Sub


Robin

"Andrew Morton" <ak*@in-press.co.uk.invalidwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...

是否可以有两个函数声明,它们采用相同的

参数但返回不同类型,具体取决于函数的方式

用过吗?


函数f(x)为字符串

''返回一个字符串

结束函数


函数f(x)as string()

''返回一个字符串数组

结束函数


''在子程序中的某个位置:


Dim S As String = f(1)''调用f(x)的字符串版本


Dim A As String()= f(2)''调用f(x)的数组版本


为什么?因为我正在从文件中提取IPTC元数据,而某些元数据

只有一个值(例如标题),有些可能有多个值

(例如关键字) )。我只是认为一个

函数名称看起来更整洁。


Andrew

Is it possible to have two function declarations which take the same
parameters but return different types depending on how the function is
used?

function f(x) as string
'' return a string
end function

function f(x) as string()
'' return an array of strings
end function

'' somewhere in a subroutine:

Dim S As String=f(1) '' calls the string version of f(x)

Dim A As String()=f(2) '' calls the array version of f(x)

Why? Because I''m extracting IPTC metadata from a file, and some metadata
has a single value (e.g. the caption) and some may have multiple values
(e.g. the keywords). I just think it would look neater to have one
function name.

Andrew



Robin Tucker写道:
Robin Tucker wrote:

嗨安德鲁,


你的函数可以返回"对象"然后退出工作,将

类型返回给调用者。
Hi Andrew,

Your function can return "Object" and then leave working out which
type was returned up to the caller.



这听起来......错了。我会选择两个功能名称。


谢谢,


Andrew

That just sounds... wrong. I''ll go with two function names then.

Thanks,

Andrew


Andrew Morton写道:
Andrew Morton wrote:

是否可以有两个函数声明,它们采用相同的

参数但返回不同的类型取决于如何使用函数?


函数f(x)为字符串

''返回一个字符串

结束函数


函数f(x)as string()

''返回一个字符串数组

结束函数


''子程序中的某个地方:


Dim S As String = f(1)''调用字符串版本f(x)


Dim A As String()= f(2)''调用f(x)的数组版本


为什么?因为我正在从文件中提取IPTC元数据,而某些元数据有单个值(例如标题),而有些可能有多个值(例如

关键字) 。我只是认为它有一个功能名称更简洁。


Andrew
Is it possible to have two function declarations which take the same
parameters but return different types depending on how the function is used?

function f(x) as string
'' return a string
end function

function f(x) as string()
'' return an array of strings
end function

'' somewhere in a subroutine:

Dim S As String=f(1) '' calls the string version of f(x)

Dim A As String()=f(2) '' calls the array version of f(x)

Why? Because I''m extracting IPTC metadata from a file, and some metadata has
a single value (e.g. the caption) and some may have multiple values (e.g.
the keywords). I just think it would look neater to have one function name.

Andrew



返回类型不属于方法签名,所以你不能有一个方法的重载,它采用相同的参数并返回不同的

数据类型。


正如Robin建议的那样,你可以将数据作为对象返回,但是你可以在每次调用后输入

来输入值。


我建议你你只需为这些方法选择不同的名称。

如果从方法名称中清楚地知道

那种数据的方法,那么读代码会更容易返回,这样您就不必检查发送到方法中的

数据,以找出它返回的内容。即使

方法名称变得更长,代码仍然更可读

比你必须在方法的每个调用周围放置一个类型转换。 />

-

G?跑Andersson

_____
http://www.guffa.com


这篇关于一个函数名,两个返回类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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