替换VB中的函数

时间:2013-04-07 20:45:04

标签: vb.net function replace

我收到此错误:“String”类型的值无法转换为“1维数组字符串”

当我尝试使用替换函数替换时 - 在将文本转换为md5hash时使用空格。

这是我的代码:

    Public Shared Function GetMD5Hash(ByVal TextToHash As String) As String
    If TextToHash = "" Or TextToHash.Length = 0 Then
        Return String.Empty
    End If

    Dim md5 As MD5 = New MD5CryptoServiceProvider()
    Dim toHash As Byte() = Encoding.Default.GetBytes(TextToHash)
    Dim result As Byte() = md5.ComputeHash(toHash)

    Return System.BitConverter.ToString(result)
End Function


Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click

    Dim tempPassToken As String() = GetMD5Hash(TextBox2.Text).Replace("-", "")
    Dim passMD5 As String = ""

    For i = 0 To tempPassToken.Length - 1
        passMD5 = passMD5 & tempPassToken(i)
    Next

1 个答案:

答案 0 :(得分:1)

更改此行:

Dim tempPassToken As String() = GetMD5Hash(TextBox2.Text).Replace("-", "")

到此:

Dim tempPassToken As String = GetMD5Hash(TextBox2.Text).Replace("-", "") 

使用括号告诉系统您正在定义一个字符串数组,而不是一个字符串。并且,正如错误所示,您无法将字符串分配给数组。