没有为类型'char'和整数定义运算符( - ) [英] Operator (-) is not defined for types 'char' and integer

查看:386
本文介绍了没有为类型'char'和整数定义运算符( - )的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我在c#中有一些代码我在vb中转换然后它抛出这个错误说

运算符( - )没有为类型'char'和整数定义

它在c#中工作正常。



这里是c#代码和vb代码

否则if((c> ='0'&& c< ='9')||(c> ='A'&& c< ='F')||(c > ='a'&& c< ='f'))
{
//获取字符的值
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;
}





在vb这是转换后的代码



 ElseIf(c> =0c AndAso c< =9c)OrElse(c> =Ac AndAso c< =Fc)OrElse( c> =ac AndAso c< =fc)然后
'得到角色的值
Dim v As Byte = CByte(c - & H30)
如果c> =Ac又要c< =Fc那么
v = CByte(c +& HA - Ac)
结束如果
如果c> =ac并且也是c< =fc然后
v = CByte(c +& HA - ac)
结束如果


如果gotFirstChar = False那么
gotFirstChar = True
accum = v





我尝试过:



 ElseIf(c> =0c AndAso c< = 9c)OrElse(c> =Ac AndAso c < =Fc)OrElse(c> =ac AndAso c< =fc)然后
'得到角色的值
Dim v As Byte = CByte(c - & H30)
如果c> =Ac又要c< =Fc那么
v = CByte(c +& HA - Ac)
结束如果
如果c> =ac又要c< =fc那么
v = CByte(c +& HA - ac)
结束如果

如果gotFirstChar = False那么
gotFirstChar = True
accum = v

解决方案

您好会员11721323



而不是使用Ac,ac等。,使用Asc(A)



例如:

 ElseIf(c> = Asc(0)AndAlso c< = Asc(9))OrElse .................... .. 
............................................ ........... ..................

这是因为在C#中字符类型是隐式自动转换的,在VB.NET中是一个显式转换(转换,准确) 是必须的。 Asc 将字符转换为ASCII等效字符。


< = etc操作不是你的问题,那些与char一起工作正常。 (解决方案1)



  CByte (c +& HA  -   A c)



这是你的问题..你试着用整数来计算Char类型的东西。

我想知道你还想尝试归档什么?当然..获得HEX的整数..已经有.Net功能



  Dim  i 作为 整数 = 整数 .Parse(  12FF,Globalization.NumberStyles.HexNumber)



i => 4863


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

解决方案

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.


That <= etc operations aren't your problem those work fine with char. (Solution 1)

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

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


i => 4863


这篇关于没有为类型'char'和整数定义运算符( - )的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆