Using color as an optional parameter In a function inside a class

How can I declare an optional color parameter in some function or sub, as if I were doing it in the usual way (I mean giving some default color for this optional parameter), since the vb.net compiler complains that there is some error in this code. How to solve this problem. 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'

+9
source share
4 answers

MSDN talks about advanced options for Visual Basic

For each optional parameter, you must specify a constant expression as the default value for this parameter. If the expression is Nothing, the default value for the value data type is used as the default value for the parameter.

So you cannot use this syntax, instead you can 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 #, the following

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

In C #, you cannot check the type of value (e.g. color, dot, size, etc.) with a null value. These types are never null, but they have a default value for type- (for example, 0 for integers), so if you need to pass an optional parameter for the type of value, you can create it with a new keyword with values ​​that you would like to use the default value or use the default keyword and let the platform decide which value is the default value for the type. If you let the platform choose, the IsEmpty property will be true.

+19
source

You can 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 
+6
source

In Visual Basic, the solution suggested by @Steve almost works, but setting Color to Nothing does not give what you usually expect; check should be for empty color, not for nothing. Here is an example. It flashes the BackColor of the Windows Forms control for 50 milliseconds if it has .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 
0
source

In the world of .NET colors, there is another feature that will allow you to realize your original intentions. This function is an enumeration called "KnownColor" that can freely translate objects back and forth in System.Drawing.Color. Although it does not have ALL possible colors, it has all the colors I have ever needed. And since this is an enumeration, it has β€œconstants” that work as default 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 

In accordance with

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

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

0
source

All Articles