Yuuta
Yuuta

Reputation: 43

C# to VB.Net - HexString

I am converting a Program from c# to vb.net and everything worked fine. There is now 1 line of code left that I cant seem to convert over to VB.Net. Would be awesome if someone could help me out with this.

C# Code:

if (num2 > 9)
            {
                text += ((char)(num2 - 10 + 65)).ToString();
            }

This is how I tried it:

If num2 > 9 Then
            text += CChar((num2 - 10 + 65)).ToString()
        End If

For "num2 - 10 + 65" It gives me the Error Code:

"Integer Values can not be converted to Char"

.

what did I do wrong?

Update: I fixed it myself by just changing CChar to Chr. Thats it.

Fixed Code:

 If num2 > 9 Then
            text += Chr(num2 - 10 + 65).ToString()
 End If

Upvotes: 1

Views: 102

Answers (2)

Lajos Arpad
Lajos Arpad

Reputation: 76426

In Visual Basic += is &= and you need to convert your parameter to string, so change the line to

text &= CChar(Convert.ToChar(num2 - 10 + 65)).ToString()

Upvotes: -1

jmcilhinney
jmcilhinney

Reputation: 54417

Another way to write that C# code in a less language-specific way would be like so:

if (num2 > 9)
{
    text += Convert.ToChar(num2 - 10 + 65);
}

You should not have any issue converting that to VB. Mind you, you should use the actual concatenation operator (&) in VB, rather than the addition operator (+). They behave the same way in many cases but not all.

If num2 > 9 Then
    text &= Convert.ToChar(num2 - 10 + 65)
End If

The ToString call is pointless because you can concatenate a Char with a String.

Upvotes: 2

Related Questions