sara brown
sara brown

Reputation: 1067

converting vb code to c#

im using mobitek gsm modem and the source code it used is in VB. now i want to convert the code into c#. the code that i have trouble with is intModemStatus = SMS.ModemInit(frmModem.txtPort.Text, ""). after that, the code will go through with select case as below:

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

i have converted everything into c# includes with the switch case like this:

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;
        }

however, i am having trouble with this code int ModemStatus = sms.ModemInit(txtPort.Text, "");. it said that

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

then i tried to change int ModemStatus = sms.ModemInit(txtPort.Text, ""); but it says the same.

to use the mobitek gsm modem, i required to add reference of MobitekSMSAPI5 and i did. the switch code will determine if the modem have been connected or else.

i am really hoping for someone to step up solving this problem. im stuck in the middle and i did not know where to start. any kinds of helps are appreciated. thank you.

here is my error: when im using this code it appears:

 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

now it appears different error when im changing the code like below.

enter image description here

Upvotes: 1

Views: 577

Answers (4)

James
James

Reputation: 82096

Your problem is just a couple of casting issues. The first one is related to the port number, the ModemInit method expects a short value but your passing a string, so you have already fixed that by using short.TryParse.

The other issue is your return type, the ModemInit method seems to return it's own custom enum value, if all your interested in is the integer value then all you need to do is cast it as an int.

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

Upvotes: 1

Andrei
Andrei

Reputation: 56688

sms.ModemInit accepts short as a first parameter. As long as your were dealing with VB.Net, conversion of string into short was done implicitly. It was possible due to compiler's Option Strict option, which is disabled by default. When enabled this option allows only implicit widening conversions. When disabled (default state) this option allows both implicit narrowing and widening conversions.

In C# however narrowing implicit conversions are forbidden, and this is why your translated code fails. So you need to parse your string value explicitly and pass a parsed number to the method:

short port = short.Parse(txtPort.Text);
int ModemStatus = sms.ModemInit(port, "");

or, even better, use TryParse to avoid possible exceptions:

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

Upvotes: 6

Justin Harvey
Justin Harvey

Reputation: 14672

I would do this:

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

This way you handle the condition of an user entering a non-number (without resorting to exceptions)

Upvotes: 1

SLaks
SLaks

Reputation: 887225

As the error clearly states, you can't pass a string as a short.

You need to call short.Parse().

Upvotes: 0

Related Questions