Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello, i have some codes in c# which i converted in vb then it throws this error saying
operator (-) is not defined for types 'char' and integer 
and it works fine in c#.

here's the c# code and vb code
else if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f'))
                {
                    // get the character's value
                    byte v = (byte)(c - 0x30);
                    if (c >= 'A' && c <= 'F') v = (byte)(c + 0x0a - 'A');
                    if (c >= 'a' && c <= 'f') v = (byte)(c + 0x0a - 'a');

                    if (gotFirstChar == false)
                    {
                        gotFirstChar = true;
                        accum = v;
                    }


in the vb this is the converted code

ElseIf (c >= "0"c AndAlso c <= "9"c) OrElse (c >= "A"c AndAlso c <= "F"c) OrElse (c >= "a"c AndAlso c <= "f"c) Then
                   ' get the character's value
                   Dim v As Byte = CByte(c - &H30)
                   If c >= "A"c AndAlso c <= "F"c Then
                       v = CByte(c + &HA - "A"c)
                   End If
                   If c >= "a"c AndAlso c <= "f"c Then
                       v = CByte(c + &HA - "a"c)
                   End If

                   If gotFirstChar = False Then
                       gotFirstChar = True
                       accum = v


What I have tried:

ElseIf (c >= "0"c AndAlso c <= "9"c) OrElse (c >= "A"c AndAlso c <= "F"c) OrElse (c >= "a"c AndAlso c <= "f"c) Then
                   ' get the character's value
                   Dim v As Byte = CByte(c - &H30)
                   If c >= "A"c AndAlso c <= "F"c Then
                       v = CByte(c + &HA - "A"c)
                   End If
                   If c >= "a"c AndAlso c <= "f"c Then
                       v = CByte(c + &HA - "a"c)
                   End If

                   If gotFirstChar = False Then
                       gotFirstChar = True
                       accum = v
Posted
Updated 13-Mar-17 3:56am
v2
Comments
Jochen Arndt 13-Mar-17 4:42am    
I have edited the question because the whole page view was messed up by some HTML tags in the "What have I tried" section.

Hi Member 11721323,

Rather than using "A"c, "a"c etc., use Asc("A").

E.g.:
ElseIf (c >= Asc("0") AndAlso c <= Asc("9")) OrElse ......................
.........................................................................
This is because in C# character types are automatically casted implicitly, in VB.NET an explicit cast (conversion, to be precise) is required. Asc converts the character to its ASCII equivalent.
 
Share this answer
 
Comments
Richard MacCutchan 13-Mar-17 4:27am    
There is no need to use bold text in your replies.
Jochen Arndt 13-Mar-17 4:39am    
This was caused by some HTML tags in the question itself which messed up the whole page view (and blended out some code).
Richard MacCutchan 13-Mar-17 4:44am    
Thanks, I should have looked more closely.
Member 11721323 13-Mar-17 9:20am    
have tried what you said MR mehedi shams, but it still returns error saying operator (<=) is not defined for types 'char' and integer
That <= etc operations aren't your problem those work fine with char. (Solution 1)

VB
CByte(c + &HA - "A"c)

This is your problem.. you try to calculate something of type Char with an Integer.
I wonder what are you trying to archive anyway? be course.. for getting the Integer of an HEX .. there are already .Net functions

VB
Dim i As Integer = Integer.Parse("12FF", Globalization.NumberStyles.HexNumber)

i => 4863
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900