我有一个已转换为 C# 的 VB.net 项目。代码的一部分在 C# 中给出了与在 VB.Net 中不同的结果。有人可以告诉我这里的 C# 代码有什么问题以及如何解决吗?
VB.Net:
Private Sub mco_GetDataReturn(ByVal TransactionNumber As Short, ByVal DataType As ModiconComunications.ModiconComObject.GetDataType, ByVal Success As Boolean, ByVal UserData() As Byte) Handles mco.GetDataReturn
OrderNumber = ""
HeatNumber = ""
If UserData.GetUpperBound(0) < 17 Then
'MsgBox("ModComm1_GetDataReturn - Not enough data returned from PLC read : " & UBound(UserData, 1) + 1 & " bytes")
ReDim Preserve UserData(0 To 17)
ElseIf UserData.GetUpperBound(0) > 17 Then
'MsgBox("ModComm1_GetDataReturn - Too much data returned from PLC read : " & UBound(UserData, 1) + 1 & " bytes")
ReDim Preserve UserData(0 To 17)
End If
OrderNumber = System.Text.Encoding.ASCII.GetChars(UserData, 0, 8)
OrderRelease = System.Text.Encoding.ASCII.GetChars(UserData, 8, 2)
HeatNumber = System.Text.Encoding.ASCII.GetChars(UserData, 10, 6)
PieceNumber = UserData(16) * 256 + UserData(17)
End Sub
C#:
private void mco_GetDataReturn(short TransactionNumber, ModiconComunications.ModiconComObject.GetDataType DataType, bool Success, byte[] UserData)
{
OrderNumber = "";
HeatNumber = "";
if (UserData.GetUpperBound(0) < 17)
{
Array.Resize(ref UserData, 18);
}
else if (UserData.GetUpperBound(0) > 17)
{
Array.Resize(ref UserData, 18);
}
OrderNumber = System.Text.Encoding.ASCII.GetString(UserData, 0, 8).ToString();
OrderRelease = System.Text.Encoding.ASCII.GetString(UserData, 8, 2).ToString();
HeatNumber = System.Text.Encoding.ASCII.GetString(UserData, 10, 6).ToString();
PieceNumber = UserData[16] * 256 + UserData[17];
}
具体问题在于 PieceNumber 的最后一条语句。在 VB.Net 中,该语句产生实际的片数,但在 C# 代码中它产生 0(零)。这些陈述有何不同?PieceNumber 变量在两个项目中都声明为 int。