指定常量,而不是不需要的变量

问题描述 投票:0回答:1

假设您使用的是预定义函数,该函数具有不需要的返回参数。要澄清的是,该函数不需要该值或您不需要该结果,或两者都没有。

在通话中使用常量是否有任何缺点或问题?还是仅出于填写电话的目的创建临时变量更好?

目标是减少调用程序中的变量定义。子例程定义不能更改。问题是在这种情况下是否建议/建议提供常量。

Dim res As Double = 2.0#
''' do not need second/third results, third value isn't used
''' calling the function this way saves creating two extra variables
Call AddOneTwoThree(res, 3.0#, 0.0#)

''' this function cannot be changed
Public Sub AddOneTwoThree(ByRef first As Double, ByRef second As Double, ByRef third As Double)
    first = first + second
    second = second + third
    third = third
End Sub
vb.net parameters return-value function-call
1个回答
0
投票

所以我认为tntinmnjmcilhinney提供的评论是我的很好的答案。

0.0#的值不会对输出产生不利影响,则不需要结果,则可以为Nothing ByRef参数指定[Double0.0#

我用Nothing进行了测试,结果与指定0.0#的结果相同。

© www.soinside.com 2019 - 2024. All rights reserved.