Visual Studio 2012同时有几个动作?

时间:2014-03-27 14:50:03

标签: visual-studio visual-studio-2012

我正在使用使用Atbash Cipher的VB2012创建一个应用程序(如果您不熟悉,它是一个密码,您用“z”替换“a”,用“y”替换“b”等等) 所以我们可以说代码说:

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Label1.Text = TextBox1.Text

        Label1.Text = Label1.Text.Replace("a", "z")
        Label1.Text = Label1.Text.Replace("z", "a")
    End Sub

,文本框显示为“az”。因此,通过按下按钮,“a”将替换为“z”,因此消息变为“zz”。然后,第二行代码适用,因此“z”被“a”代替,消息变为“aa”而不是“za”。问题是,如何同时运行两行代码,因此“a”在“z”变为“a”的同时变为“z”。谢谢!

1 个答案:

答案 0 :(得分:1)

您可以使用查找表并转换文本。创建新表对象时,它将在内部生成查找表。您可以在任何字符串参数上调用实例方法Transform。这将返回密文。

Public Class AtbashTable
    ''' <summary>
    ''' Lookup table to shift characters.
    ''' </summary>
    Private _shift As Char() = New Char(Char.MaxValue - 1) {}

    ''' <summary>
    ''' Generates the lookup table.
    ''' </summary>
    Public Sub New()
        ' Set these as the same.
        For i As Integer = 0 To Char.MaxValue - 1
            _shift(i) = CChar(i)
        Next
        ' Reverse order of capital letters.
        For c As Char = "A"C To "Z"C
            _shift(CInt(c)) = CChar("Z"C + "A"C - c)
        Next
        ' Reverse order of lowercase letters.
        For c As Char = "a"C To "z"C
            _shift(CInt(c)) = CChar("z"C + "a"C - c)
        Next
    End Sub

    ''' <summary>
    ''' Apply the Atbash cipher.
    ''' </summary>
    Public Function Transform(value As String) As String
        Try
            ' Convert to char array
            Dim a As Char() = value.ToCharArray()
            ' Shift each letter.
            For i As Integer = 0 To a.Length - 1
                Dim t As Integer = CInt(a(i))
                a(i) = _shift(t)
            Next
            ' Return new string.
            Return New String(a)
        Catch
            ' Just return original value on failure.
            Return value
        End Try
    End Function
End Class

Button click事件中的用法:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  AtbashTable x = new AtbashTable();
  Label1.Text = x.Transform(TextBox1.Text);
End Sub