Question

How can I declare an optional color parameter in some function or sub, as if I do that in the normal way (I mean to give some default color for that optional parameter) as the vb.net compiler complains that there is some error in that code. How do I resolve this issue. Sample code below:

Public Shared Function SomeFunction(ByVal iParam As Integer, Optional ByVal oColor As Color = Color.Black)

        End Function

The compiler does not accept '=Color.Black'

Was it helpful?

Solution

MSDN says about Optional Parameters for Visual Basic

For each optional parameter, you must specify a constant expression as the default value of that parameter. If the expression evaluates to Nothing, the default value of the value data type is used as the default value of the parameter.

So you can't use that syntax, instead you could write something like this

Private Sub Test(a As Integer, Optional c As Color = Nothing)
    If c = Nothing Then
        c = Color.Black ' your default color'
    End If
    ......
End Sub

The same code written in C# is the following

private void Test(int a, Color c = default(Color))
{
    if (c.IsEmpty)
        c = Color.Black;
}

In C# you cannot test a Value type (like Color, Point, Size etc...) against a null value. These types are never null, but they have a default value for the type-(Like 0 for integers), so, if you need to pass an optional parameter for a value type you could create it with the new keyword with the values you would like to use as default or use the default keyword and let the framework decide which value is the default for the type. If you let the framework choose then the IsEmpty property will be true.

OTHER TIPS

You could overload the method

''' <summary>
''' requires two parameters
''' </summary>
''' <param name="a">an integer</param>
''' <param name="c">a color</param>
''' <remarks></remarks>
Private Sub Test(a As Integer, c As Color)
    'Your function
End Sub

''' <summary>
''' one parameter, uses default color of black
''' </summary>
''' <param name="a">an integer</param>
''' <remarks></remarks>
Private Sub Test(a As Integer)
    Test(a, Color.Black)
End Sub

There is another feature in the .NET color world that will allow you to carry out your original intentions. The feature is an enumeration called "KnownColor" which can freely translate back and forth to System.Drawing.Color objects. While it doesn't have ALL possible colors, it has all the colors I have ever needed. And because it is an enumeration, it has "constants" that work as default value specifiers in an optional argument. Example:

Private Sub Test(a As Integer, Optional kc As KnownColor = KnownColor.Black)
    Dim MyColor As System.Drawing.Color = Color.FromKnownColor(kc)
    ......
End Sub

According to

https://docs.microsoft.com/en-us/dotnet/api/system.drawing.color.toknowncolor

you can use the System.Drawing.Color.ToKnownColor() function to translate back to a value in the KnownColor enumeration if the Color is created from a predefined color by using either the FromName(String) method or the FromKnownColor(KnownColor) method. Otherwise it will return the value 0.

In Visual Basic, the solution proposed by @Steve nearly works but setting Color to Nothing does not do what you might normally expect; the check needs to be for the Empty Color, not for Nothing. Here is an example. It Flashes a Windows Forms Control's BackColor for 50 milliseconds, so long as it has a .ForeColor:

Sub Flash(c As Control, Optional FlashColor As Color = Nothing,
          Optional Duration As Integer = 50)
    If FlashColor.Equals(System.Drawing.Color.Empty) Then FlashColor = Color.Red
    Try
        Dim CurrColor = c.BackColor
        c.BackColor = FlashColor
        c.Refresh()
        System.Threading.Thread.Sleep(Duration)
        c.BackColor = CurrColor
        c.Refresh()
    Catch ex as Exception
    End Try
End Sub

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top