locked
Optional Parameters RRS feed

  • Question

  • User-893857529 posted

     I am having problems with optional parameters, I have declared a method as follows

     

    Function OrgTree(Optional ByVal OrganizationGuid as String = "00000000-0000-0000-0000000000000") As DataSet
    
    Return SqlHelper(cn, "sp_OrgTree", OrganizationGuid)
     
    End Function
    
    

    This tells me that the value of "g" cannoy be empty... ? whatever...

    I put an if statement in there to see what is going on, OrganizationGuid is equal to nothing. How is that possible? I have declared it as a string and gve it value, when when it falls into the method it "is nothing". How can i make sure that it gets value?

    Friday, October 16, 2009 9:37 AM

Answers

  • User-952121411 posted

    Its an optional parameter, so if i pass it nothing, then it should take on the value i assigned to the option
     

    This depends on how you meant to use the word 'Nothing'.  The word 'Nothing' here is being used in (2) different ways and may be causing confusion.  It should be stated in terms of "Passing in an argument as the Optional parameter that has a value = 'Nothing', or not passing any value in as the Optional parameter at all"

    If you pass an argument into the method in that Optional parameter, regardless if the argument has a value of Nothing or some other value, it is considered that you did pass something in and the Optional value should not be used.  Remember, a variable having a value = 'Nothing' is a valid value to be tested for, so assuming to use the Optional default value just because the argument value passed in = 'Nothing' is incorrect.

    If you need to use the default value in a scenario where the string argument value passed in for the 'OrganizationGuid' = Nothing then the following code can be added at the beginning of your method:

    'If the argument value passed in has a a value = 'Nothing' then go ahead and use the default value
    If OrganizationGuid Is Nothing Then
         OrganizationGuid = "00000000-0000-0000-0000000000000"
    End If

    Actually, the best way if possibly to prevent this is to initialize your strings upon creating them.  In fact, if you do not initialize a string and pass it inot a method as the last example highlighted, you will actually get a design time warning stating the following:

    "Variable 'MyString' is used before it has been assigned a value. A null reference exception could result at runtime."

    The best way is to initialize a string with a value, like I show below:

    Dim MyString As String = String.Empty

    If you built this method for other users to call, and can not control if the String was initialized with a value or not, then the code I wrote at the beginning of this post will work for you.

    Hope this helps! Smile

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 16, 2009 2:18 PM

All replies

  • User397347636 posted

    What does your call to the method look like?

    You're probably passing a null string, and the 'SqlHelper' method doesn't like that. 

    Friday, October 16, 2009 10:48 AM
  • User-893857529 posted

    understood... the question is why?

    I set the optional parameters value, why does it come through as nothing? 

    Friday, October 16, 2009 12:04 PM
  • User397347636 posted

    What is the value of the argument that you are passing to the method?

    If the string is uninitialized, it'll be 'Nothing'.

     

    Friday, October 16, 2009 12:24 PM
  • User-893857529 posted

    Its an optional parameter, so if i pass it nothing, then it should take on the value i assigned to the option, it is coming in as nothing and staying nothing, thats my problem 

    Friday, October 16, 2009 12:30 PM
  • User397347636 posted

    If you omit the argument, it'll default to the value you specified, but if you pass it an unitialized string, it'll use that value.

    e.g.,

    OrgTree() -> this will use the value you specified in the method header

    Dim s As String

    OrgTree(s) -> this will use the value 'Nothing'

     

    Friday, October 16, 2009 12:52 PM
  • User-952121411 posted

    Its an optional parameter, so if i pass it nothing, then it should take on the value i assigned to the option
     

    This depends on how you meant to use the word 'Nothing'.  The word 'Nothing' here is being used in (2) different ways and may be causing confusion.  It should be stated in terms of "Passing in an argument as the Optional parameter that has a value = 'Nothing', or not passing any value in as the Optional parameter at all"

    If you pass an argument into the method in that Optional parameter, regardless if the argument has a value of Nothing or some other value, it is considered that you did pass something in and the Optional value should not be used.  Remember, a variable having a value = 'Nothing' is a valid value to be tested for, so assuming to use the Optional default value just because the argument value passed in = 'Nothing' is incorrect.

    If you need to use the default value in a scenario where the string argument value passed in for the 'OrganizationGuid' = Nothing then the following code can be added at the beginning of your method:

    'If the argument value passed in has a a value = 'Nothing' then go ahead and use the default value
    If OrganizationGuid Is Nothing Then
         OrganizationGuid = "00000000-0000-0000-0000000000000"
    End If

    Actually, the best way if possibly to prevent this is to initialize your strings upon creating them.  In fact, if you do not initialize a string and pass it inot a method as the last example highlighted, you will actually get a design time warning stating the following:

    "Variable 'MyString' is used before it has been assigned a value. A null reference exception could result at runtime."

    The best way is to initialize a string with a value, like I show below:

    Dim MyString As String = String.Empty

    If you built this method for other users to call, and can not control if the String was initialized with a value or not, then the code I wrote at the beginning of this post will work for you.

    Hope this helps! Smile

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 16, 2009 2:18 PM