如何在此方案中使用可选参数 [英] How to use optional paramters in this scenario

查看:70
本文介绍了如何在此方案中使用可选参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有这样的功能



Hi All,

I have a function like this

public shared sub myfunctionname(optional Byval Dataset1 as Dataset,optional Byval xml as XmlDocument)

         If(Dataset1.tables.count>0) Then
              'Do something
         End If
         
           'Similarly for xml

         If(xml.HasChildNodes)

            'Do Something
         End  If


   End Sub



如果我调用上面的函数,如 myfunctionname(,xml)



然后在检查条件时 If(Dataset1.tables.count> 0)当数据集中没有表格时会抛出异常。与xml类似的情况。



对此有任何建议不胜感激!!!!!!



提前致谢。


If i call the above function like myfunctionname(,xml)

Then while checking the condition If(Dataset1.tables.count>0) this will throw an exception when the dataset has no tables in it.And similar case with xml.

Any suggestions on this would be appreciated!!!!!!

Thanks in advance.

推荐答案

尝试创建自定义类使用重载方法 [ ^ ]。



Try to create custom class with overloaded methods[^].

Public Class MyClassWithOverloadedMethods

    public shared sub myfunctionname(Byval Dataset1 as Dataset)

         If(Dataset1.tables.count>0) Then
              'Do something
         End If

    End Sub

    public shared sub myfunctionname(optional Byval Dataset1 as Dataset,optional Byval xml as XmlDocument)
           'Similarly for xml

         If(xml.HasChildNodes)
            'Do Something
         End  If

    End Sub

End Class





如何使用?



How to use?

Dim mc As MyClassWithOverloadedMethods = New MyClassWithOverloadedMethods()
mc.myfunctionname(DatasetObject)
'or
'mc.myfunctionname(XmlObject)





无论传入 myfunctionname 程序的对象类型执行正确的程序;)



No matter of type of object passed into myfunctionname program executes proper procedure ;)


你需要将可选参数值设置为首先

You need to set the optional parameter values to something first
public shared sub myfunctionname(optional Byval Dataset1 as Dataset=nothing,optional Byval xml as XmlDocument=nothing)

'first check if anything has been submitted then apply your business logic
        If(not DataSet1 is nothing) then
            'Do Something
         End  If


         if (not xml is nothing) then

            If(xml.HasChildNodes)
              'Do Something
           End  If
         end if

    End Sub





我希望这有助于....



I hope this helps....


这篇关于如何在此方案中使用可选参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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