【问题标题】:How to pass a Array as a parameter in this procedure?如何在此过程中将数组作为参数传递?
【发布时间】:2013-07-06 03:34:31
【问题描述】:

我有这个程序:

    ''' <summary>
    ''' Append text to the current text.
    ''' </summary>
    ''' <param name="text">The text to append</param>
    ''' <param name="forecolor">The font color</param>
    ''' <param name="backcolor">The Background color</param>
    ''' <param name="font">The font of the text</param>
    Public Sub Append_Text(ByVal text As String, _
                          ByVal forecolor As Color, _
                          Optional ByVal backcolor As Color = Nothing, _
                          Optional ByVal font As Font = Nothing)

        Dim index As Int32 = MyBase.TextLength
        MyBase.AppendText(text)
        MyBase.SelectionStart = index
        MyBase.SelectionLength = MyBase.TextLength - index
        MyBase.SelectionColor = forecolor
        If Not backcolor = Nothing Then MyBase.SelectionBackColor = backcolor
        If font IsNot Nothing Then MyBase.SelectionFont = font
        MyBase.SelectionStart = MyBase.TextLength
        MyBase.SelectionLength = 0

    End Sub

我这样调用程序:

RichTextLabel1.Append_Text("My ", Color.White, color.transparent, New Font("Arial", 12, FontStyle.Bold))
RichTextLabel1.Append_Text("RichText-", Color.White, , New Font("Arial", 12, FontStyle.Bold))

我的问题是我是否可以通过使用这样的参数数组进行一次重载(以及如何进行修改)来调用 proc:

RichTextLabel1.Append_Text( _
{"My ", Color.White, Color.Transparent, New Font("Arial", 12, FontStyle.Bold)}, _
{"RichTextLabel", Color.White, Nothing, New Font("Arial", 16, FontStyle.Bold)})

(那段代码显然不起作用)

【问题讨论】:

  • 满足您的要求的唯一方法是拥有Object 的二维或​​锯齿状数组,因此没有类型安全性。为什么调用该方法两次如此糟糕?它已经足够好了。
  • @pswg 感谢您的评论,我只是想简化 proc 的使用,不要写太多代码行,也不要调用太多 proc,也许我这次太乐观了因为您的话似乎很难进行这种修改,也许您可​​以向我解释一下:“对象的二维或锯齿状数组”
  • 看我的回答。我试图说明每个选项的用法,并详细解释它们的问题。

标签: .net arrays vb.net overloading procedure


【解决方案1】:

您必须使用two-dimensional array

Public Sub Append_Text(ByVal parameters As Object(,))
    If UBound(parameters, 2) <> 3 Then
        Throw new ArgumentException("Array was not the correct size", "parameters")
    End If

    For i As Integer = 0 To UBound(parameters, 1)
        Append_Text( _
            CType(parameters(i, 0), String), _
            CType(parameters(i, 1), Color), _
            CType(parameters(i, 2), Color), _
            CType(parameters(i, 3), Font))
    Next
End Sub

RichTextLabel1.Append_Text({ _
    {"My ", Color.White, Color.Transparent, New Font("Arial", 12, FontStyle.Bold)}, _
    {"RichTextLabel", Color.White, Nothing, New Font("Arial", 16, FontStyle.Bold)} _
})

或者这个使用jagged array:

Public Sub Append_Text(ByVal parameters As Object()())
    For Each p In parameters
        If UBound(p) <> 3 Then
            Throw new ArgumentException("Array was not the correct size", "parameters")
        End If

        Append_Text( _
            CType(p(i)(0), String), _
            CType(p(i)(1), Color), _
            CType(p(i)(2), Color), _
            CType(p(i)(3), Font))
    Next
End Sub

RichTextLabel1.Append_Text({ _
    New Object(){"My ", Color.White, Color.Transparent, New Font("Arial", 12, FontStyle.Bold)}, _
    New Object(){"RichTextLabel", Color.White, Nothing, New Font("Arial", 16, FontStyle.Bold)} _
})

但最接近您所要求的是带有 ParamArray 关键字的锯齿状数组:

Public Sub Append_Text(ByVal ParamArray parameters As Object()())
    ' Same as Above
End Sub

RichTextLabel1.Append_Text( _
    {"My ", Color.White, Color.Transparent, New Font("Arial", 12, FontStyle.Bold)}, _
    {"RichTextLabel", Color.White, Nothing, New Font("Arial", 16, FontStyle.Bold)})

当然,所有情况的问题在于您完全失去了编译时类型安全性。没有什么可以阻止用户传入错误类型的参数或错误数量的参数(尽管我添加了简单的运行时检查来说明如何做到这一点)。最终,如果目标是减少添加多个项目所需的代码行数,请注意,简单地调用该方法两次实际上比您要求的代码行数更少:

' 2 lines of code
RichTextLabel1.Append_Text("My ", Color.White, Color.Transparent, New Font("Arial", 12, FontStyle.Bold))
RichTextLabel1.Append_Text("RichTextLabel", Color.White, Nothing, New Font("Arial", 16, FontStyle.Bold))

' 3 lines of code
RichTextLabel1.Append_Text( _
    {"My ", Color.White, Color.Transparent, New Font("Arial", 12, FontStyle.Bold)}, _
    {"RichTextLabel", Color.White, Nothing, New Font("Arial", 16, FontStyle.Bold)})

【讨论】:

  • 谢谢,锯齿状数组示例不工作,它在“p”上抛出错误,第一个工作正常,但你能告诉我如何将二维数组传递给程序吗?我已经阅读了第一个和第二个链接,但我无法理解传递数组的部分,我尝试混合使用 、 {} 和 ()。
  • @ElektroHacker 抱歉,请参阅我的更新答案。我还提供了有关如何调用每个版本的示例。
猜你喜欢
  • 2017-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-14
相关资源
最近更新 更多