Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Color As Optional Parameter In a function within a class

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'

like image 588
Ahmed Nazmy Avatar asked Apr 16 '13 19:04

Ahmed Nazmy


People also ask

What is optional parameter function?

What are Optional Parameters? By definition, an Optional Parameter is a handy feature that enables programmers to pass less number of parameters to a function and assign a default value.

Why are optional parameter added?

Optional Parameters are parameters that can be specified, but are not required. This allows for functions that are more customizable, without requiring parameters that many users will not need.


2 Answers

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.

like image 79
V. Wheeler Avatar answered Oct 06 '22 21:10

V. Wheeler


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.

like image 44
Steve Avatar answered Oct 06 '22 20:10

Steve