2
SMSCOMMS SMSEngine = new SMSCOMMS("COM6");

该代码似乎没有将我的论点COM6视为有效的ref string。我该如何解决这个问题?

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

3 回答 3

3

您不能传递临时 with ref,因为被调用的方法必须能够分配给调用者的变量。你为什么一开始就用它?你永远不会分配给COMMPORT.

为什么不只是:

public SMSCOMMS(string COMMPORT)
于 2010-06-14T05:39:51.040 回答
2

ref除非您打算修改调用者传递的实际变量,否则无需传递参数。由于您不能修改字符串文字(根据定义,它是常量),因此对于通过引用传递是无效的。

于 2010-06-14T05:47:10.253 回答
1

您只能ref在传递具有可用参考的东西时使用。这意味着您必须先声明一个变量,然后通过 ref 传递该变量:

string comm = "COM6";
SMSCOMMS SMSEngine = new SMSCOMMS(ref comm);
于 2010-06-14T05:49:14.163 回答