[英]Operator '!' cannot be applied to operand of type long
I am doing some conversion of some old VB code to C#. 我正在将一些旧的VB代码转换为C#。 I am getting this error when converting a couple of properties.
转换几个属性时出现此错误。
"Operator '!' “操作员!!” cannot be applied to operand of type long"
不能应用于long类型的操作数”
This is what the VB code looks like and hoping for some help with this. 这就是VB代码的样子,并希望对此有所帮助。
Public Property Value() As Integer
Get
If (Me.m_Value < 32768) Then
Return Me.m_Value
Else
Return -32768 + (Me.m_Value - 32768)
End If
End Get
Set(ByVal PLCValue As Integer)
Me.m_Value = PLCValue
End Set
End Property
Public Property B0() As Boolean
Get
If ((Me.m_Value And &H1&) = &H1&) Then
Return True
Else
Return False
End If
End Get
Set(ByVal State As Boolean)
If (State = True) Then
Me.m_Value = Me.m_Value Or &H1&
Else
Me.m_Value = Me.m_Value And (Not &H1&)
End If
End Set
End Property
Public Property B1() As Boolean
Get
If ((Me.m_Value And &H2&) = &H2&) Then
Return True
Else
Return False
End If
End Get
Set(ByVal State As Boolean)
If (State = True) Then
Me.m_Value = Me.m_Value Or &H2&
Else
Me.m_Value = Me.m_Value And (Not &H2&)
End If
End Set
End Property
This is what it looks like when converting it. 这就是转换时的样子。 Both set conditions is where I am getting this error.
这两个设置条件都是我收到此错误的地方。 The Or |
或| I can change to casting to (int) and the error goes away but not sure on the else condition how to adjust that?
我可以更改为强制转换为(int)并且错误消失了,但不确定在其他情况下如何进行调整?
public int Value
{
get
{
if ((_value < 32768))
{
return _value;
}
else
{
return -32768 + (_value - 32768);
}
}
set { _value = value; }
}
public bool B0
{
get
{
if (((_value & 0x1L) == 0x1L))
{
return true;
}
else
{
return false;
}
}
set
{
if ((value == true))
{
_value = _value | 0x1L;
}
else
{
_value = _value & (!0x1L);
}
}
}
public bool B1
{
get
{
if (((_value & 0x2L) == 0x2L))
{
return true;
}
else
{
return false;
}
}
set
{
if ((value == true))
{
_value = _value | 0x2L;
}
else
{
_value = _value & (!0x2L);
}
}
}
Thanks! 谢谢!
Complete code, hopefully correct: 完整的代码,希望可以更正:
public int Value
{
get
{
if ((_value < 32768))
{
return _value;
}
else
{
return -32768 + (_value - 32768);
}
}
set { _value = value; }
}
public bool B0
{
get
{
if (((_value & 0x1) == 0x1))
{
return true;
}
else
{
return false;
}
}
set
{
if ((value == true))
{
_value = _value | 0x1;
}
else
{
_value = _value & (~0x1);
}
}
}
public bool B1
{
get
{
if (((_value & 0x2) == 0x2))
{
return true;
}
else
{
return false;
}
}
set
{
if ((value == true))
{
_value = _value | 0x2;
}
else
{
_value = _value & (~0x2);
}
}
}
As I pointed out in my comment and was confirmed by @mataps link, the Not
on the numerical value should be interpreted as a bitwise negate ~
in C#. 正如我在评论中指出的那样,并通过@mataps链接进行了确认,数值上的
Not
应该在C#中解释为按位取反~
。 Also, I dropped the L
literal everywhere, because Value
is of type int
, not long
, so now the compiler hopefully doesn't compain about doing bitwise operations on different types ( int
and long
) 另外,我到处都放了
L
文字,因为Value
是int
类型,不是long
类型,所以现在编译器希望不再对不同类型( int
和long
)进行按位运算。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.