sar*_*own 1 c# vb.net vb6-migration
即时通讯使用mobitek gsm调制解调器,它使用的源代码是在VB中.现在我想将代码转换为c#.我遇到麻烦的代码是intModemStatus = SMS.ModemInit(frmModem.txtPort.Text, "").之后,代码将通过select case进行如下:
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
Run Code Online (Sandbox Code Playgroud)
我已将所有内容转换为c#includes,其开关案例如下:
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;
}
Run Code Online (Sandbox Code Playgroud)
但是,我遇到了这个代码的问题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, "");
Run Code Online (Sandbox Code Playgroud)

现在,当我改变代码如下所示时出现不同的错误.

sms.ModemInit接受short作为第一个参数.只要您处理VB.Net,就会隐式地将字符串转换为short.这可能是由于编译器的Option Strict选项,默认情况下禁用.启用后,此选项仅允许隐式扩展转换.禁用时(默认状态),此选项允许隐式缩小和扩展转换.
在C#中,禁止缩小隐式转换,这就是您翻译的代码失败的原因.因此,您需要string显式地解析您的值并将解析后的数字传递给方法:
short port = short.Parse(txtPort.Text);
int ModemStatus = sms.ModemInit(port, "");
Run Code Online (Sandbox Code Playgroud)
或者,更好的是,使用TryParse来避免可能的异常:
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, "");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
484 次 |
| 最近记录: |