Question

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));
}
Was it helpful?

Solution

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)

OTHER TIPS

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.

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top