即时通讯使用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#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;
}
但是,我在使用此代码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, "");
现在,当我更改下面的代码时,它会出现不同的错误。
答案 0 :(得分:6)
sms.ModemInit
接受short
作为第一个参数。只要您处理VB.Net,就会隐式地将字符串转换为short。这可能是由于编译器的Option Strict
选项,默认情况下禁用该选项。启用后,此选项仅允许隐式widening conversions。禁用时(默认状态),此选项允许隐式narrowing and widening conversions。
在C#中,禁止缩小隐式转换,这就是您翻译的代码失败的原因。因此,您需要显式解析string
值并将解析后的数字传递给方法:
short port = short.Parse(txtPort.Text);
int ModemStatus = sms.ModemInit(port, "");
或者更好的是,使用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, "");
答案 1 :(得分:1)
我会这样做:
short shortValue = 0;
if (short.TryParse(txtPort.Text, out shortValue))
{
... continue using shortValue
}
else
{
...Tell user the value must be a number
}
通过这种方式,您可以处理用户输入非数字的情况(无需借助异常)
答案 2 :(得分:1)
你的问题只是一些铸造问题。第一个与端口号相关,ModemInit
方法需要short
值,但您传递string
,因此您已使用short.TryParse
修复了此问题。< / p>
另一个问题是你的返回类型,ModemInit
方法似乎返回它自己的自定义enum
值,如果所有你感兴趣的是整数值,那么你需要做的就是将其转换为int
。
int ModemStatus = (int)sms.ModemInit(port, "");
答案 3 :(得分:0)
由于错误明确指出,您不能将字符串作为短消息传递。
您需要致电short.Parse()
。