本文整理匯總了VB.NET中System.ParamArrayAttribute類的典型用法代碼示例。如果您正苦於以下問題:VB.NET ParamArrayAttribute類的具體用法?VB.NET ParamArrayAttribute怎麽用?VB.NET ParamArrayAttribute使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了ParamArrayAttribute類的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: New
Public Class Temperature
Private temp As Decimal
Public Sub New(temperature As Decimal)
Me.temp = temperature
End Sub
Public Overrides Function ToString() As String
Return ToString("C")
End Function
Public Overloads Function ToString(format As String) As String
If String.IsNullOrEmpty(format) Then format = "G"
Select Case format
Case "G", "C"
Return temp.ToString("N") + " °C"
Case "F"
Return (9 * temp / 5 + 32).ToString("N") + " °F"
Case "K"
Return (temp + 273.15d).ToString("N") + " °K"
Case Else
Throw New FormatException(String.Format("The '{0}' format specifier is not supported", _
format))
End Select
End Function
Public Sub Display(<[ParamArray]()> formats() As String)
If formats.Length = 0 Then
Console.WriteLine(Me.ToString("G"))
Else
For Each format As String In formats
Try
Console.WriteLine(Me.ToString(format))
' If there is an exception, do nothing.
Catch
End Try
Next
End If
End Sub
End Class
示例2: Main
Public Module Example
Public Sub Main()
Dim temp1 As New Temperature(100)
Dim formats() As String = { "C", "G", "F", "K" }
' Call Display method with a string array.
Console.WriteLine("Calling Display with a string array:")
temp1.Display(formats)
Console.WriteLine()
' Call Display method with individual string arguments.
Console.WriteLine("Calling Display with individual arguments:")
temp1.Display("C", "F", "K", "G")
Console.WriteLine()
' Call parameterless Display method.
Console.WriteLine("Calling Display with an implicit parameter array:")
temp1.Display()
End Sub
End Module
輸出:
Calling Display with a string array: 100.00 °C 100.00 °C 212.00 °F 373.15 °K Calling Display with individual arguments: 100.00 °C 212.00 °F 373.15 °K 100.00 °C Calling Display with an implicit parameter array: 100.00 °C