2012-09-24 160 views
1

即時通訊使用mobitek GSM調制解調器和它使用的源代碼是在VB中。現在我想將代碼轉換爲C#。我遇到的代碼是intModemStatus = SMS.ModemInit(frmModem.txtPort.Text, "")。在此之後,該代碼將與選擇的情況下經過如下:轉換vb代碼到C#

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, "");。它說,

Argument 1cannot convert string to short. the best overloaded method match for mobitekSMSAPI5.ModemInit(short, string) have some invalid argument.

然後我試圖改變int ModemStatus = sms.ModemInit(txtPort.Text, "");但它說相同。

使用mobitek gsm調制解調器,我需要添加MobitekSMSAPI5的參考,我做到了。交換機代碼將確定調制解調器是否已連接,否則。

我真的很希望有人能加緊解決這個問題。我卡在中間,我不知道從哪裏開始。任何形式的幫助表示讚賞。謝謝。

這裏是我的錯誤: 使用此代碼時出現IM:

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, ""); 

enter image description here

現在它時,即時通訊改變如下面的代碼將出現不同的錯誤。

enter image description here

回答

1

你的問題只是一些鑄造問題。第一個與端口號有關,ModemInit方法需要一個short值,但通過了string,因此您已經通過使用short.TryParse修復了該值。

另一個問題是你的回報類型,ModemInit方法好像又回到它自己的定製enum值,如果您的所有感興趣的是整數值,那麼所有你需要做的是將它轉換爲int

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

我終於明白了,非常感謝你!我真的很感激。再次感謝。 –

+0

@sarabrown沒問題,很高興它幫助你:) – James

0

正如錯誤中明確規定,你不能將一個字符串作爲短。您需要撥打short.Parse()

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, ""); 
+0

我認爲這將有助於OP知道*爲什麼*相同的代碼在VB編譯而不是C#(就像我自己)。 VB會對參數或其他東西進行隱式轉換嗎? – James

+0

@James,afaik VB.Net編譯器有Option Strict選項,默認關閉。當禁用此選項時,VB.Net會嘗試執行隱式轉換和分析,就像在這種特殊情況下一樣。 – Andrei

+0

有趣我不知道這也包括參數傳遞。 – James

1

我這樣做:

short shortValue = 0; 
if (short.TryParse(txtPort.Text, out shortValue)) 
{ 
    ... continue using shortValue 
} 
else 
{ 
    ...Tell user the value must be a number 
} 

這樣你處理進入了一個非數字的用戶的狀態(而不是訴諸例外)