avgcoder
avgcoder

Reputation: 392

Replace Function in VB

I get this error: Value of type 'String' cannot be converted to '1-dimensional array of String'

When I try to use the replace function to replace - with an empty space when converting text to md5hash.

This is my code:

    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

Upvotes: 1

Views: 572

Answers (1)

Danielle Stone
Danielle Stone

Reputation: 170

Change this line:

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

to this:

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

using the parentheses tells the system you are defining an array of strings, not a single string. And, as the error suggests, you can't assign a string to an array.

Upvotes: 1

Related Questions