在VB

2013-04-07 23 views
1

替換功能I得到這個錯誤:類型「字符串」的值不能轉化爲在VB

「字符串的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

您正試圖將String分配給String()類型的變量。從tempPassToken中刪除括號,使其不是數組。這也消除了For循環的需要。 – 2013-04-07 20:58:15

+0

感謝你的工作。 – 2013-04-08 18:22:31

回答

1

改變這一行:

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

這樣:

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

使用括號告訴你定義一個字符串數組系統,而不是單個字符串。而且,如錯誤所示,您無法將字符串分配給數組。