重载功能 [英] Overloading functions

查看:51
本文介绍了重载功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个函数Sum作为....

函数和(ByVal ParamArray值()As Double)As Double

Dim rSum,itm As Double

每个价值每个价值

rSum + = itm

下一个

返回rSum

结束功能


我认为这个功能很通用,但是当我尝试

计算总和时出现问题一系列单打。我真的必须为单个,长整数,短整数,字节等做一个

重载函数吗?

还是有更好的方法吗?


TIA

/ Kejpa

Hi,
I have a function Sum as....
Function Sum(ByVal ParamArray Values() As Double) As Double
Dim rSum, itm As Double
For Each itm In Values
rSum += itm
Next
Return rSum
End Function

This function is general enough I think, but troubles arise when I try to
calculate the sum of an array of singles. Do I really have to make an
overloaded function for single, long, integer, short, byte and whatnot?
Or is there a better approach?

TIA
/Kejpa

推荐答案

Kejpa,


我不会说这是一个更好的方法,因为在一个精心制作的程序中我从来没有看到这种功能的原因




您还可以使用值类型

中的每个数组创建一个重载函数。对于我在这种类型的功能中再次没有理由。


编写代码来执行此功能所做的几乎与

调用相同功能并直接显示发生了什么。


但是当你想要这样做时,我觉得更糟糕。


\\\ \\\

公共舱你好

公共共享子主()

Dim param(3)As Object

D as a Integer = 1

Dim b As Double = 2

Dim c As Short = 3

Dim d As Single = 4

param(0)= a

param(1)= b

param(2)= c

param (3)= d

Dim f As Double = CDbl(Sum(param))

Dim g As Integer = CInt(Sum(param))

Dim h As Single = CSng(Sum(param))

End Sub

公共共享函数和(ByVal ParamArray _

值()作为对象)作为双倍

Dim rSum,它是双倍的

每个值的价值

rSum + = itm

下一页

返回rSum

结束功能

结束班级

///


但是我希望这会有帮助吗?


Cor


" Kejpa" < kS ******* @ saj.fi>

...
Kejpa,

I will not say that it is a better approach, because I never see a reason
for this kind of functions in a well made program.

You can also make an overloaded function with every array in the value type
you want to use. For what I see again no reason in this type of functions.

Writing the code to do what you do in this function is almost the same as
calling the function and shows directly what happens.

However beneath when you want to do it the in my opinion less worse.

\\\
Public Class Hello
Public Shared Sub main()
Dim param(3) As Object
Dim a As Integer = 1
Dim b As Double = 2
Dim c As Short = 3
Dim d As Single = 4
param(0) = a
param(1) = b
param(2) = c
param(3) = d
Dim f As Double = CDbl(Sum(param))
Dim g As Integer = CInt(Sum(param))
Dim h As Single = CSng(Sum(param))
End Sub
Public Shared Function Sum(ByVal ParamArray _
Values() As Object) As Double
Dim rSum, itm As Double
For Each itm In Values
rSum += itm
Next
Return rSum
End Function
End Class
///

However I hope this helps?

Cor

"Kejpa" <kS*******@saj.fi>
...

我有一个函数Sum as ....
函数和(ByVal ParamArray值()为双)作为双重
Dim rSum,它是双重的
每个它的值
rSum + = itm
下一个<返回rSum
结束功能

我认为这个功能很通用,但当我尝试计算一系列单曲的总和时会出现麻烦。我真的必须为单个,长整数,短整数,字节等做一个
重载函数吗?
还是有更好的方法吗?

TIA
/ Kejpa
Hi,
I have a function Sum as....
Function Sum(ByVal ParamArray Values() As Double) As Double
Dim rSum, itm As Double
For Each itm In Values
rSum += itm
Next
Return rSum
End Function

This function is general enough I think, but troubles arise when I try to
calculate the sum of an array of singles. Do I really have to make an
overloaded function for single, long, integer, short, byte and whatnot?
Or is there a better approach?

TIA
/Kejpa



Kejpa,


我不会说这是一个更好的方法,因为在一个制作精良的程序中我从来没有看到过这种函数的原因




你也可以使用值中的每个数组来重载函数键入
你想要使用的
。对于我在这种类型的功能中再次没有理由。


编写代码来执行此功能所做的几乎与

调用相同功能并直接显示发生了什么。


但是当你想要这样做时,我觉得更糟糕。


\\\ \\\

公共舱你好

公共共享子主()

Dim param(3)As Object

D as a Integer = 1

Dim b As Double = 2

Dim c As Short = 3

Dim d As Single = 4

param(0)= a

param(1)= b

param(2)= c

param (3)= d

Dim f As Double = CDbl(Sum(param))

Dim g As Integer = CInt(Sum(param))

Dim h As Single = CSng(Sum(param))

End Sub

公共共享函数和(ByVal ParamArray _

值()作为对象)作为双倍

Dim rSum,它是双倍的

每个值的价值

rSum + = itm

下一页

返回rSum

结束功能

结束班级

///


但是我希望这会有帮助吗?


Cor


" Kejpa" < kS ******* @ saj.fi>

...
Kejpa,

I will not say that it is a better approach, because I never see a reason
for this kind of functions in a well made program.

You can also make an overloaded function with every array in the value type
you want to use. For what I see again no reason in this type of functions.

Writing the code to do what you do in this function is almost the same as
calling the function and shows directly what happens.

However beneath when you want to do it the in my opinion less worse.

\\\
Public Class Hello
Public Shared Sub main()
Dim param(3) As Object
Dim a As Integer = 1
Dim b As Double = 2
Dim c As Short = 3
Dim d As Single = 4
param(0) = a
param(1) = b
param(2) = c
param(3) = d
Dim f As Double = CDbl(Sum(param))
Dim g As Integer = CInt(Sum(param))
Dim h As Single = CSng(Sum(param))
End Sub
Public Shared Function Sum(ByVal ParamArray _
Values() As Object) As Double
Dim rSum, itm As Double
For Each itm In Values
rSum += itm
Next
Return rSum
End Function
End Class
///

However I hope this helps?

Cor

"Kejpa" <kS*******@saj.fi>
...

我有一个函数Sum as ....
函数和(ByVal ParamArray值()为双)作为双重
Dim rSum,它是双重的
每个它的值
rSum + = itm
下一个<返回rSum
结束功能

我认为这个功能很通用,但当我尝试计算一系列单曲的总和时会出现麻烦。我真的必须为单个,长整数,短整数,字节等做一个
重载函数吗?
还是有更好的方法吗?

TIA
/ Kejpa
Hi,
I have a function Sum as....
Function Sum(ByVal ParamArray Values() As Double) As Double
Dim rSum, itm As Double
For Each itm In Values
rSum += itm
Next
Return rSum
End Function

This function is general enough I think, but troubles arise when I try to
calculate the sum of an array of singles. Do I really have to make an
overloaded function for single, long, integer, short, byte and whatnot?
Or is there a better approach?

TIA
/Kejpa



Cor,

我认为你的观点是认为它是无用的功能,但是我希望在

中订购完整的一组统计函数(包括平均值,


你的代码看起来很棒,我预计它会起作用,但它并没有' T。一个

InvalidCastException发生从Single()到Double的转换无效。

不知怎的,似乎整个ParamArray试图转换为Double

而不是每个元素。

你试过代码了吗?


谢谢

/ Kejpa

Cor Ligthert <无************ @ planet.nl>在消息中写道

新闻:%2 **************** @ tk2msftngp13.phx.gbl ...
Cor,
I can see your point in thinking it''s a useless function, but I''d like it in
order to have a "complete" set of statistical functions (including average,
square sum, varians etc etc)

Your code looked great and I anticipated it to work, however it didn''t. An
InvalidCastException occurs Conversion from Single() to Double is not valid.
Somehow it seems like the whole ParamArray tries to convert to a Double
instead of each element.
Did you try your code?

Thanks
/Kejpa
"Cor Ligthert" <no************@planet.nl> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Kejpa,

我不会说这是一个更好的方法,因为在一个制作精良的程序中我从来没有看到这种功能的原因。
你可以还要为要使用的值
类型中的每个数组创建一个重载函数。对于我在这种类型的函数中再次没有理由的原因。
编写代码来完成你在这个函数中做的事情与调用函数几乎相同,并直接显示发生的事情。

然而,当你想要这样做时,我觉得更糟糕。

\\\
公共课您好
公共共享Sub main()
Dim param(3)As Object
Dim a As Integer = 1
Dim b As Double = 2
Dim c As Short = 3
Dim d单身= 4
param(0)= a
param(1)= b
param(2)= c
param(3)= d
Dim f当Double = CDbl(Sum(param))
Dim g As Integer = CInt(Sum(param))
Dim h As Single = CSng(Sum(param))
End Sub
公共共享函数和(ByVal ParamArray _
值()作为对象)作为双重
Dim rSum,它作为双重
为每个它的价值观
rSum + = itm
下一页
返回rSum
结束功能
// @

但是我希望这有帮助?

Cor

Kejpa < kS ******* @ saj.fi>
..
Kejpa,

I will not say that it is a better approach, because I never see a reason
for this kind of functions in a well made program.

You can also make an overloaded function with every array in the value type you want to use. For what I see again no reason in this type of functions.

Writing the code to do what you do in this function is almost the same as
calling the function and shows directly what happens.

However beneath when you want to do it the in my opinion less worse.

\\\
Public Class Hello
Public Shared Sub main()
Dim param(3) As Object
Dim a As Integer = 1
Dim b As Double = 2
Dim c As Short = 3
Dim d As Single = 4
param(0) = a
param(1) = b
param(2) = c
param(3) = d
Dim f As Double = CDbl(Sum(param))
Dim g As Integer = CInt(Sum(param))
Dim h As Single = CSng(Sum(param))
End Sub
Public Shared Function Sum(ByVal ParamArray _
Values() As Object) As Double
Dim rSum, itm As Double
For Each itm In Values
rSum += itm
Next
Return rSum
End Function
End Class
///

However I hope this helps?

Cor

"Kejpa" <kS*******@saj.fi>
..

我有一个函数Sum作为....
函数总和(ByVal ParamArray值()为双倍)作为双重
Dim rSum,它是双重的
每个它的价值
rSum + = itm
下一页
返回rSum
结束功能

我认为这个功能很通用,但是当我尝试
来计算单个数组的总和时会出现麻烦。我真的必须为单个,长整数,短整数,字节等做一个
重载函数吗?
还是有更好的方法吗?

TIA
/ Kejpa
Hi,
I have a function Sum as....
Function Sum(ByVal ParamArray Values() As Double) As Double
Dim rSum, itm As Double
For Each itm In Values
rSum += itm
Next
Return rSum
End Function

This function is general enough I think, but troubles arise when I try to calculate the sum of an array of singles. Do I really have to make an
overloaded function for single, long, integer, short, byte and whatnot?
Or is there a better approach?

TIA
/Kejpa




这篇关于重载功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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