Netorica
Netorica

Reputation: 19347

know if an Optional parameter is not filled in function invoke VB.NET

ok I do have this kind of function

Public Function myDog(name As String, age As Integer,Optional color As String = Nothing) As String
   'todo codes
    return Nothing
End Sub

now my problem is I want to know inside my function how can I find that parameter color was not filled during function invoke

myDog("brown",2) 

now I really don't want to rely if parameter color holds Nothing then its not filled during the function invoke. I really want to know if the function is invoked and filled up until parameter color

Upvotes: 0

Views: 402

Answers (1)

Heslacher
Heslacher

Reputation: 2167

If you need to know if the optional parameter has been set during invocation of the method, you should use overloading for this.

I would replace your method with these 3 methods:

Public Function myDog(name As String, age As Integer) As String
    Return myDog(name, age, Nothing, False)
End Function

Public Function myDog(name As String, age As Integer, color As String) As String
    Return myDog(name, age, color, True)
End Function

Private Function myDog(name As String, age As Integer, color As String, filledColor As Boolean) As String
    'todo codes
    Return Nothing
End Function

Please notice that only the first two methods are public accessible while the third will be private and do all the work. Inside the third you can then check if filledColor will be true.

Upvotes: 2

Related Questions