1

私はこの手順を持っています:

    ''' <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 を 1 回だけ呼び出すオーバーロード (および変更方法) を作成できるかどうかです。

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

(そのコードは明らかに機能しません)

4

1 に答える 1

1

2 次元配列を使用してこれを行う必要があります。

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)} _
})

またはこれをジャグ配列を使用して:

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 回呼び出すだけで、実際には求めているコードの行数が少なくなることに注意してください。

' 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)})
于 2013-07-06T03:59:16.760 に答える