vb.net 在类中的函数中使用颜色作为可选参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16045479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Color As Optional Parameter In a function within a class
提问by Ahmed Nazmy
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:
如何在某些函数或子函数中声明可选颜色参数,就像我以正常方式(我的意思是为该可选参数提供一些默认颜色)那样做,因为 vb.net 编译器抱怨说存在一些错误代码。我该如何解决这个问题。示例代码如下:
Public Shared Function SomeFunction(ByVal iParam As Integer, Optional ByVal oColor As Color = Color.Black)
End Function
The compiler does not accept '=Color.Black'
编译器不接受'=Color.Black'
回答by Steve
MSDN says about Optional Parametersfor Visual Basic
MSDN 说关于Visual Basic 的可选参数
For each optional parameter, you must specify a constantexpression 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.
对于每个可选参数,您必须指定一个常量表达式作为该参数的默认值。如果表达式的计算结果为 Nothing,则使用 value 数据类型的默认值作为参数的默认值。
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
用C#编写的相同代码如下
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 newkeyword with the values you would like to use as default or use the defaultkeywordand let the framework decide which value is the default for the type. If you let the framework choose then the IsEmptyproperty will be true.
在 C# 中,您不能针对空值测试值类型(如颜色、点、大小等)。这些类型从不为空,但它们具有该类型的默认值(例如 0 表示整数),因此,如果您需要为值类型传递可选参数,您可以使用带有new您想要的值的关键字创建它用作默认值或使用default关键字并让框架决定哪个值是该类型的默认值。如果让框架选择,那么IsEmpty属性将为真。
回答by dbasnett
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
回答by V. Wheeler
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:
.NET 色彩世界中还有另一个功能可以让您实现您的初衷。该功能是一个名为“KnownColor”的枚举,它可以自由地来回转换为 System.Drawing.Color 对象。虽然它没有所有可能的颜色,但它拥有我曾经需要的所有颜色。并且因为它是一个枚举,所以它具有作为可选参数中的默认值说明符的“常量”。例子:
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.
如果 Color 是使用 FromName(String) 方法或 FromKnownColor(KnownColor) 方法从预定义颜色创建的,则可以使用 System.Drawing.Color.ToKnownColor() 函数转换回KnownColor 枚举中的值。否则它将返回值 0。
回答by Neil Dunlop
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:
在 Visual Basic 中,@Steve 提出的解决方案几乎有效,但将 Color 设置为 Nothing 并不能达到您通常期望的效果;检查需要针对 Empty Color,而不是 Nothing。这是一个例子。只要它有一个 .ForeColor,它就会闪烁 Windows 窗体控件的 BackColor 50 毫秒:
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

