locked
Setting a default value for a Unit type parameter RRS feed

  • Question

  • User-1686570997 posted

    I want to include an optional parameter in a function statement that is of the type Unit. For example:

    Public Shared Function NewTable(Optional ByVal TableWidth As Unit) As Table

    Optional parameters require a default value, but I cannot figure out how to set a default value for a Unit type. Help! Thanks!

    Tuesday, April 12, 2011 12:48 PM

Answers

  • User-29804325 posted

    Hi,

    Use Optional parameter sometime will cause some of stuff wrong. The value you specify for the default on an optional parameter must be a constant, whatever Unit.Empty or Unit.Pixel are actually a property and therefore it's value can't be classed as a constant by the compiler.

    To my own opinion, I prefer to use overload method instead of optional parameter.

    Like:

     Public Sub NewTable()
            'Defaults
            'Unit.Empty
            Me.NewTable(Unit.Empty)
     End Sub
    
     Public Sub NewTable(ByVal TableWidth As Unit)
    
     End Sub
    

    Hope this can help you.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 15, 2011 1:59 AM

All replies

  • User761909039 posted

    You can set the empty unit as default value like this..

    Public Shared Function NewTable(Optional ByVal TableWidth As Unit = Unit.Empty) As Tabl

     

    Tuesday, April 12, 2011 2:22 PM
  • User-1686570997 posted

    Thank you for your response. I tried what you suggested, but it says, "Constant expression is required." Why won't it accept Unit.Empty as a constant? What else should I try?

    Tuesday, April 12, 2011 2:40 PM
  • User761909039 posted

    Can you try this one..

    Public Shared Function NewTable(Optional ByVal TableWidth As Unit = Unit.Pixel(0)) As Table

    Tuesday, April 12, 2011 3:31 PM
  • User-1686570997 posted

    I've tried that one too, and I get the same response: "Constant expression is required."

    Tuesday, April 12, 2011 3:51 PM
  • User-1686570997 posted

    Does anyone have any other suggestions for how to supply a default value for an optional parameter of type Unit?

    Tuesday, April 12, 2011 6:05 PM
  • User-29804325 posted

    Hi,

    Use Optional parameter sometime will cause some of stuff wrong. The value you specify for the default on an optional parameter must be a constant, whatever Unit.Empty or Unit.Pixel are actually a property and therefore it's value can't be classed as a constant by the compiler.

    To my own opinion, I prefer to use overload method instead of optional parameter.

    Like:

     Public Sub NewTable()
            'Defaults
            'Unit.Empty
            Me.NewTable(Unit.Empty)
     End Sub
    
     Public Sub NewTable(ByVal TableWidth As Unit)
    
     End Sub
    

    Hope this can help you.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 15, 2011 1:59 AM
  • User-1686570997 posted

    Thank you! That's a helpful suggestion.

    Saturday, April 16, 2011 2:50 PM