Click here to Skip to main content
15,908,674 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,

I have a function like this

VB
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

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.
Posted
Updated 4-Apr-13 8:00am
v2
Comments
Sergey Alexandrovich Kryukov 4-Apr-13 13:39pm    
Exception information, please. Are you sure it's thrown in this line? Provide exception stack, just in case.
—SA

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

VB
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?
VB
Dim mc As MyClassWithOverloadedMethods = New MyClassWithOverloadedMethods()
mc.myfunctionname(DatasetObject)
'or
'mc.myfunctionname(XmlObject)


No matter of type of object passed into myfunctionname program executes proper procedure ;)
 
Share this answer
 
Comments
waj1206 5-Apr-13 0:36am    
I cannot call the function by passing only one parameter.We have to call the function like this
If i am passing only xml then i have o pass like this I CANNOT PASS LIKE THIS myfuncionname(xml)


I have to pass only like this myfunctionname( , xml) here we have to give the first parameter empty else it will throw an error that incorrect type is passed as parameter because it expects the first parameter as datset.
Maciej Los 5-Apr-13 1:39am    
I have no idea why you can't use overloaded method... If you use only one parameter at time, second is not necessary.
Please, read Sergey's comment.
You need to set the optional parameter values to something first
VB
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....
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900