Using Color As Optional Parameter In a function within a class
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.
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.
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