1

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

0

4 Answers 4

6

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, "");
8
  • I think it would help the OP to know why the same code compiles in VB but not C# (as I would myself). Does VB do implicit casting on parameters or something?
    – James
    Sep 24, 2012 at 12:09
  • @James, afaik VB.Net compiler has option Option Strict, which is switched off by default. When this option is disabled, VB.Net tries to perform implicit conversions and parsings, like in this particular case.
    – Andrei
    Sep 24, 2012 at 12:17
  • Interesting I wasn't aware that this also included parameter passing.
    – James
    Sep 24, 2012 at 12:20
  • @James i believe that when im putting the dots after the sms, it should appear ModemInit as it part of the reference class. but it wont shows up and giving the casting error instead. it might be true that i have a casting issue but seriously i have no idea now to solve it. i also put the error snippet for better understanding of my problem. i hope it helps.
    – sara brown
    Sep 24, 2012 at 12:24
  • @sarabrown the error you have updated your question with doesn't appear to be the same error you originally stated. The first one is solved if you use any of the solutions provided (as you are passing a string when the function expects a short). As clarified by @Andrei this works in VB (by default) has OPTION STRICT off which means the compiler will automatically attempt the cast for you. However, your other error seems to suggest your return type of your ModemInit function is wrong.
    – James
    Sep 24, 2012 at 12:38
1

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, "");
1
  • i finally got it.thank you thank you so much! i really appreciate. thanks once again.
    – sara brown
    Sep 24, 2012 at 16:07
1

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)

0

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

You need to call short.Parse().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.