vb.net中的web方法:可选参数 - 对于重载来说太多了 [英] web method in vb.net: optional parameters - too many for overloading

查看:72
本文介绍了vb.net中的web方法:可选参数 - 对于重载来说太多了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。



老话题但有点扭曲 - 我搜索过但无法找到答案。我知道我不能使用带有web方法的默认值的可选参数,所以我必须使用函数重载但是...我看到一个可选参数的例子,我有大约10个!如果我理解3个可选参数,我将需要7个重载函数(3个用于1个参数,3个用于2个,1个用于整个3个)所以我需要10个多少?很多!必须有更好的方法,不是吗?并且请不要告诉我使用WCF - 我现在不能切换到这个并且我必须使用WSDL



非常感谢帮助

Hello everybody

Old subject but with a twist - I searched and couldn''t find answer to that. I know I can''t use optional parameters with default values with web method so I have to use function overloading but...I saw examples of one optional parameter and I have around 10! If I understand well for 3 optional parameters I will need 7 overloaded functions (3 for 1 parameter, 3 for 2 and 1 for the whole 3) so for how many I need for 10? a lot! there must be a better way, no? and please don''t tell me to use WCF - I can''t switch to that now and I have to use WSDL

thanks a lot for helping

推荐答案

当我有这样的场景时,我通常有一种方法可以实际完成工作并具有所有参数。然后我的每个覆盖都调用它,但是为它不使用的parms设置一个空字符串或零或任何默认值。这样你就拥有了10个不同版本的方法,但实际上你只需要使用真正完成工作的代码来维护一个方法。像这样:

When I have a scenario like this, I generally have one method that actually does the work and has ALL of the parameters. Then each of my overrides calls it, but sets an empty string or a zero or whatever default value to the parms that it doesn''t use. This way you have your 10 different versions of the method, but you really only have to maintain one method with the code that really does the work. Like this:
Public Overloads Sub MyMethod(ByVal strParm1 As String)
    MyMethodWork(strParm1, String.Empty, 0)
End Sub

Public Overloads Sub MyMethod(ByVal strParm1 As String, ByVal strParm2 As String)
    MyMethodWork(strParm1, strParm2, 0)
End Sub

Public Overloads Sub MyMethod(ByVal strParm1 As String, ByVal strParm2 As String, ByVal intParm3 As Integer)
    MyMethodWork(strParm1, strParm2, intParm3)
End Sub

Private Sub MyMethodWork(ByVal strParm1 As String, ByVal strParm2 As String, ByVal intParm3 As Integer)
    'Do actual work here.
End Sub



或者,您可以随时调用包含所有参数的方法,并在调用时设置默认值。我比使用重载更经常这样做。



-----更新----

您还可以添加以下方法:


Alternatively, you could just always call a method that has ALL of the parms and set default values when you are calling it. I do this more often than using overloads.

----- Update ----
You could also add these methods:

Public Overloads Sub MyMethod(ByVal intParm3 As Integer)
    MyMethodWork(String.Empty, String.Empty, intParm3)
End Sub

Public Overloads Sub MyMethod(ByVal strParm1 As String, ByVal intParm3 As Integer)
    MyMethodWork(strParm1, String.Empty, intParm3)
End Sub





但是你不能有一个传递strParm1和intParm3的方法,然后有一个传递strParm2和intParm3的ANOTHER方法。因为这两种方法都有一个字符串parm和一个整数parm所以它不知道你要调用哪一个。所以任何组合,只要你不加倍数据类型。



But you can''t have a method that passes strParm1 and intParm3 and then have ANOTHER method that passes strParm2 and intParm3. Because both methods would have a string parm and an integer parm so it won''t know which one you are trying to call. So any combination as long as you don''t double up on the data types.


请看我对这个问题的评论。



糟糕的术语是在很久以前创造的,超载。太多的初学者都非常困惑。没有任何东西实际上是超载,只是因为没有加载。语法的这个方面根本不需要特殊术语。就是这样:只要编译器可以通过call语句告诉另一个,就可以使用不同的,甚至是不相关的方法来使用相同的名称。当然,参数列表应该足够不同,否则编译器将无法解析实际调用的方法。如果你还考虑了继承这样的事情,你会发现即使使用合法的方法声明,call语句也可能以模糊的方式编写。 (我是否必须一次又一次地解释它?这些例子是微不足道的,以及可能的解决方案。)



不,我不坐你对术语超载感到困惑。 你对一些更奇怪的东西感到困惑。有可能在参数列表中声明许多参数组合并不意味着你必须声明所有。此外,它只会造成麻烦。



您可以创建尽可能多的方法,因为它似乎对您的类型的使用有用,不再。



-SA
Please see my comment to the question.

Bad term was coined quite a while ago, "overloading". Too many beginners have been highly confused. Nothing is actually "overloaded", just because nothing is "loaded". This aspect of syntax does not need a special term at all. It''s just this: different, possibly even unrelated methods are allowed to use the same name, as soon as it is possible for a compiler to tell on from another by the call statement. Naturally, the parameter lists should be "different enough", otherwise a compiler won''t be able to resolve the method to be actually called. And if you also take into account such things as inheritance, you will see that even with legitimate method declarations, the call statement may be written in a ambiguous way. (Do I even have to explain it, again and again? Such examples are trivial, as well as the possible resolutions.)

No, I don''t sat you are confused with the term "overloading". You are confused with something more weird. Having a possibility to declare many combinations of parameters in the parameter lists does not mean you have to declare then all. Moreover, it would only cause troubles..

You can create as many methods as it seems to be useful for the using of your type, no more.

—SA


这篇关于vb.net中的web方法:可选参数 - 对于重载来说太多了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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