ACP
ACP

Reputation: 35264

Am I using passing my argument correctly with ref?

SMSCOMMS SMSEngine = new SMSCOMMS("COM6");

The code doesn't seem to take my argument of COM6 as a valid ref string.How can I solve this?

public class SMSCOMMS
{
   public SMSCOMMS(ref string COMMPORT)
   {
    SMSPort = new SerialPort();
    SMSPort.PortName = COMMPORT;
    SMSPort.BaudRate = 9600;
    SMSPort.Parity = Parity.None;
    SMSPort.DataBits = 8;
    SMSPort.StopBits = StopBits.One;
    SMSPort.Handshake = Handshake.RequestToSend;
    SMSPort.DtrEnable = true;
    SMSPort.RtsEnable = true;
    SMSPort.NewLine = System.Environment.NewLine;
    ReadThread = new Thread(
        new System.Threading.ThreadStart(ReadPort));
}

Upvotes: 2

Views: 433

Answers (3)

cHao
cHao

Reputation: 86595

There's no need to pass a ref param unless you're intending to modify the actual variable the caller's passed. Since you can't modify a string literal (it's constant, by definition), it's not valid for passing by reference.

Upvotes: 2

jrista
jrista

Reputation: 33010

You can only use ref when you are passing something that has a usable reference. That means you have to declare a variable first, then pass that variable by ref:

string comm = "COM6";
SMSCOMMS SMSEngine = new SMSCOMMS(ref comm);

Upvotes: 1

Matthew Flaschen
Matthew Flaschen

Reputation: 285077

You can't pass a temporary with ref, because the called method must be able to assign to the caller's variable. Why are you using it to begin with? You never assign to COMMPORT.

Why not just:

public SMSCOMMS(string COMMPORT)

Upvotes: 3

Related Questions