gpt4 book ai didi

c# - 将VB代码转换为C#

转载 作者:太空宇宙 更新时间:2023-11-03 18:12:14 31 4
gpt4 key购买 nike

即时通讯使用mobitek gsm调制解调器,其使用的源代码在VB中。现在我想将代码转换为C#。我遇到麻烦的代码是intModemStatus = SMS.ModemInit(frmModem.txtPort.Text, "")。之后,代码将通过选择大小写进行如下操作:

intModemStatus = SMS.ModemInit(frmModem.txtPort.Text, "")

Select Case intModemStatus

Case 0
FrmModem.txtText.Text = "GSM Modem Not Connected!"
'[VB - Module1] frmModem.txtText = "GSM Modem Not Connected!"
Exit Sub

Case 1
FrmModem.txtText.Text = "CONNECTED!"
'[VB - Module1] frmModem.txtText = "GSM Modem Connected!"
Exit Sub

Case 2
FrmModem.txtText.Text = "PIN Required!"
'[VB - Module1] frmModem.txtText = "PIN Required!"
Exit Sub

Case 3
FrmModem.txtText.Text = "Incorrect PIN Entered! Warning after 3 tries of incorrect PIN entered, your SIM card will be blocked by TELCO!"
'[VB - Module1] frmModem.txtText = "Incorrect PIN entered! Warning: after 3 tries of incorrect PIN entered, your SIM card will be blocked by TELCO!"
Exit Sub

Case 4
FrmModem.txtText.Text = "Your SIM card is blocked by TELCO!"
'[VB - Module1] frmModem.txtText = "Your SIM card is blocked by TELCO!"
Exit Sub

Case 5
FrmModem.txtText.Text = "Your SIM card has problem!"
'[VB - Module1] frmModem.txtText = "Your SIM card has problem!"
Exit Sub

Case Else
FrmModem.txtText.Text = "GSM Modem Not Connected!"
'[VB - Module1] frmModem.txtText = "GSM Modem Not Connected!"
Exit Sub

End Select


我已经将所有内容都转换为c#包含开关情况,如下所示:

int ModemStatus = sms.ModemInit(txtPort.Text, "");
switch (intModemStatus)
{
case 0:

txtText.Text = "GSM Modem Not Connected!";
//[VB - Module1] frmModem.txtText = "GSM Modem Not Connected!"
return;

break;
case 1:
txtText.Text = "CONNECTED!";
//[VB - Module1] frmModem.txtText = "GSM Modem Connected!"
return;


break;
case 2:
txtText.Text = "PIN Required!";
//[VB - Module1] frmModem.txtText = "PIN Required!"
return;


break;
case 3:
txtText.Text = "Incorrect PIN Entered! Warning after 3 tries of incorrect PIN entered, your SIM card will be blocked by TELCO!";
//[VB - Module1] frmModem.txtText = "Incorrect PIN entered! Warning: after 3 tries of incorrect PIN entered, your SIM card will be blocked by TELCO!"
return;


break;
case 4:
txtText.Text = "Your SIM card is blocked by TELCO!";
//[VB - Module1] frmModem.txtText = "Your SIM card is blocked by TELCO!"
return;


break;
case 5:
txtText.Text = "Your SIM card has problem!";
//[VB - Module1] frmModem.txtText = "Your SIM card has problem!"
return;


break;
default:
txtText.Text = "GSM Modem Not Connected!";
//[VB - Module1] frmModem.txtText = "GSM Modem Not Connected!"
return;


break;
}


但是,我在使用此代码 int ModemStatus = sms.ModemInit(txtPort.Text, "");时遇到麻烦。它说


  参数1无法将字符串转换为short。
  最好的重载方法匹配为mobitekSMSAPI5.ModemInit(short,string)有一些无效的参数。


然后我尝试更改 int ModemStatus = sms.ModemInit(txtPort.Text, "");,但它说的相同。

要使用mobitek gsm调制解调器,我需要添加MobitekSMSAPI5的引用,但是我做到了。开关代码将确定调制解调器是否已连接。

我真的希望有人加紧解决这个问题。我卡在中间,我不知道从哪里开始。任何帮助都值得赞赏。谢谢。

这是我的错误:
当我使用此代码时,它会显示:

 short port;
if (!short.TryParse(txtPort.Text, out port))
{
throw new Exception("Failed to parse port");
// or any other handling - depends on your needs
}

int ModemStatus = sms.ModemInit(port, "");




现在,当我更改如下代码时,它似乎出现了不同的错误。

最佳答案

您的问题只是几个强制性问题。第一个与端口号有关,ModemInit方法期望一个short值,但是您传递一个string,因此您已经使用short.TryParse修复了该问题。

另一个问题是您的返回类型,ModemInit方法似乎返回它自己的自定义enum值,如果您感兴趣的只是整数值,则只需将其转换为int

int ModemStatus = (int)sms.ModemInit(port, "");

关于c# - 将VB代码转换为C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12564674/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com