locked
Set a Default Value of Functions Optional Argument to Null RRS feed

  • Question

  • How can you set a function optional argument to null?  For example, I want to set the optional argument intCode equal to Null.  These arguments are used to send to a stored procedure as parameters.

    Public Function pubfnc_SetCommentCode(ByVal strFieldName As String, ByVal lngResultID As Int32, Optional ByVal intCode As Int32 = DBNull.Value) As String
    

    Currently, I'm getting an error for DBNull.Value; "Constant expression is required."

    Thanks, Ryan


    Ryan
    • Moved by Reed KimbleMVP, Moderator Friday, August 5, 2011 3:18 PM OP wanted topic in lang instead of general (From:Visual Basic General)
    Thursday, August 4, 2011 9:10 PM

Answers

  • Optional parameters, by definition, must be supplied a default value. DBNull.Value is not sufficient as it means "no value", which conflicts with  the optional parameter rule.

    So, one solution would be to supply actual hard-coded values for your Int or Date or String (Some date that indicates not a valid date, like #01/01/1900# and  perhaps an empty string ("")

    In your case, you are using int - int is not a reference type so it cannot be assign null - it should be assigned a constant (choose a negative number for example instead of NULL)  like intCode as Int32 = -1


    But, realy the better solution would be to not use Optional Parameters at  all and instead use Overloading, which would allow you to write a version of  your method that doesn't take the last parameter at all...


    Ali Hamdar (alihamdar.com - www.ids.com.lb)
    • Proposed as answer by DiegoCattaruzza Friday, August 5, 2011 4:00 PM
    • Marked as answer by Ryan0827 Monday, August 8, 2011 8:43 PM
    Thursday, August 4, 2011 9:45 PM
  • Because DBNull.Value is a field, you can't use it as a default parameter value. You can only use constants, which can be defined at compile time. Anything which isn't known until runtime cannot be used as a default value.

    Really, the best option is just to create an overload:

     

    Public Function pubfnc_SetCommentCode(ByVal strFieldName As String, ByVal IngResultID As Int32, ByVal intCode As Int32?)
     ' Do your stuff here
    End Function
    
    Public Function pubfnc_SetCommentCode(ByVal strFieldName As String, ByVal IngResultID As Int32)
     pubfnc_SetCommentCode(strFieldName, IngResultID, Nothing)
    End Function
    


    EDIT: Or I guess just change Int32 to Int32? and DBNull.Value to Nothing in your original code.

     


    Check out My Blog for tech news, development tips, and other information for geeks like me.

    • Marked as answer by Ryan0827 Monday, August 8, 2011 8:43 PM
    Thursday, August 4, 2011 9:54 PM

All replies

  • How can you set a function optional argument to null?  For example, I want to set the optional argument intCode equal to Null.  These arguments are used to send to a stored procedure as parameters.

     

    Public Function pubfnc_SetCommentCode(ByVal strFieldName As String, ByVal lngResultID As Int32, Optional ByVal intCode As Int32 = DBNull.Value) As String
    

    Currently, I'm getting an error for DBNull.Value; "Constant expression is required."

    Thanks, Ryan


    Ryan
    • Edited by Ryan0827 Thursday, August 4, 2011 7:22 PM forgot to describe the error
    • Moved by Jie BaoModerator Friday, August 5, 2011 3:05 AM (From:Windows Forms General)
    • Merged by Reed KimbleMVP, Moderator Friday, August 5, 2011 3:17 PM duplicate thread
    Thursday, August 4, 2011 7:20 PM
  • In C# you would use an int? parameter with a default value null, no idea how to do in VB though.

    This forum is for Windows Forms class library (classes under the System.Windows.Forms namespace), you should post VB language questions to the VB language forum.



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP

    Thursday, August 4, 2011 8:12 PM
  • Optional parameters, by definition, must be supplied a default value. DBNull.Value is not sufficient as it means "no value", which conflicts with  the optional parameter rule.

    So, one solution would be to supply actual hard-coded values for your Int or Date or String (Some date that indicates not a valid date, like #01/01/1900# and  perhaps an empty string ("")

    In your case, you are using int - int is not a reference type so it cannot be assign null - it should be assigned a constant (choose a negative number for example instead of NULL)  like intCode as Int32 = -1


    But, realy the better solution would be to not use Optional Parameters at  all and instead use Overloading, which would allow you to write a version of  your method that doesn't take the last parameter at all...


    Ali Hamdar (alihamdar.com - www.ids.com.lb)
    • Proposed as answer by DiegoCattaruzza Friday, August 5, 2011 4:00 PM
    • Marked as answer by Ryan0827 Monday, August 8, 2011 8:43 PM
    Thursday, August 4, 2011 9:45 PM
  • have you tried to set it to Nothing instead of DBNull ?

    there are some articles online tht might help. Here's one:

    http://stackoverflow.com/questions/2086281/how-can-i-create-a-nullable-optional-numeric-integer-double-parameter-in-vb-net

    Thursday, August 4, 2011 9:48 PM
  • Because DBNull.Value is a field, you can't use it as a default parameter value. You can only use constants, which can be defined at compile time. Anything which isn't known until runtime cannot be used as a default value.

    Really, the best option is just to create an overload:

     

    Public Function pubfnc_SetCommentCode(ByVal strFieldName As String, ByVal IngResultID As Int32, ByVal intCode As Int32?)
     ' Do your stuff here
    End Function
    
    Public Function pubfnc_SetCommentCode(ByVal strFieldName As String, ByVal IngResultID As Int32)
     pubfnc_SetCommentCode(strFieldName, IngResultID, Nothing)
    End Function
    


    EDIT: Or I guess just change Int32 to Int32? and DBNull.Value to Nothing in your original code.

     


    Check out My Blog for tech news, development tips, and other information for geeks like me.

    • Marked as answer by Ryan0827 Monday, August 8, 2011 8:43 PM
    Thursday, August 4, 2011 9:54 PM
  • It is a value type so will always have a value.

    The suggestion of using a specific value which the function translates in DbNull is proabably the way to go.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"
    Thursday, August 4, 2011 11:17 PM
    Moderator
  • Ali and Reed

    thanks for the reminder. I forgot an Integer parameter must have a default value.

    I've generally stayed away from Optional parameters and used overloaded methods instead.

    Thursday, August 4, 2011 11:55 PM
  • Hi Ryan.

    Your could pass a value for intCode as a STRING perhaps?

    >>

    Public Class Form1
    
      Public Function pubfnc_SetCommentCode(ByVal strFieldName As String, ByVal lngResultID As Int32, Optional ByVal intCode As String = Nothing) As String
    
        Dim returnString As String = ""
        'Your code here:>>
    
        Return returnString
    
      End Function
    
    End Class
    

     

    or use:>>

    Public Class Form1
    
      Public Function pubfnc_SetCommentCode(ByVal strFieldName As String, ByVal lngResultID As Int32, Optional ByVal intCode As String = Microsoft.VisualBasic.vbNullString) As String
    
        Dim returnString As String = ""
        'Your code here:>>
    
        Return returnString
    
      End Function
    
    End Class
    

     



    Regards,

    John

    Click this link to see how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    XNA is coming to VB.Net

    App Hub forums



    Friday, August 5, 2011 12:04 AM
  • Tim,

    What does Int32? mean?


    Ryan
    Friday, August 5, 2011 12:09 AM
  • Int32? means the Nullable Type in .Net (http://msdn.microsoft.com/en-us/library/ms235245.aspx), then this varible can get Nothing value or Int32 value.

    And we cannot set the DBNull.Value to the optinal argument, also the DBNull cannot be casted to Int32, by reflector tool, you could view the method ToInt32 throws one exception:

    Private Function System.IConvertible.ToInt32(ByVal provider As IFormatProvider) As Integer Implements IConvertible.ToInt32
     Throw New InvalidCastException(Environment.GetResourceString("InvalidCast_FromDBNull"))
    End Function
    
    

    So Tim's reply is the answer.

     

    Sincerely,

     


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Friday, August 5, 2011 3:05 AM
    Moderator
  • Yes, as I mentioned in my post - Overloading is always a better solution...
    Ali Hamdar (alihamdar.com - www.ids.com.lb)
    Friday, August 5, 2011 8:48 AM
  • Like Bob said, Int32? is just a shorthand way of writing Nullable<Int32>. See the link he provided for details on the Nullable class.
    Check out My Blog for tech news, development tips, and other information for geeks like me.
    Friday, August 5, 2011 1:11 PM
  • Plese do not duplicate your threads...  You may request that a moderator move the thread to a different forum if you feel like you would get better results from the Language forum instead of the General forum.

    -EDIT-

    Sorry I just realized another moderator moved your second post here instead of merging it with the first.  Let me know if you want this moved back to the general forum (though this is probably more of a language related question).

     


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Friday, August 5, 2011 3:19 PM
    Moderator