我的代码适用于VB语法,但不适用于C#语法

时间:2019-05-30 19:31:38

标签: c# vb.net

在我的vb.net中,我有以下代码:

Private WithEvents RF As SiritController2._0.RFIDController

Public Sub StartReaders()
    RF = New SiritController2._0.RFIDController(HP.RfIdAddress, HP.RfIdUserName, HP.RfIdPassword, HP.RfIdAntena)
EndSub

在我的C#中,我有以下代码:

private RFIDController reader;

private void Start()
{
    reader = new RFIDController(HP.RfIdAddress, "HP.RfIdUserName", "HP.RfIdPassword", HP.RfIdAntena);
    reader.DetectRFID += Reader_DetectRFID;
}

这两个代码在相同的构造函数上调用相同的类

public RFIDController(string HostIP, string userName, string userPass, string AntennaNoPort = "1")
{
  RFIDController.__ENCAddToList((object) this);
  this.SvcTimerInterval = 45000;
  this.ReaderTimeOut = 1000;
  this.RaiseEventDelay = 0;
  this._lastReadRFTagNumber = string.Empty;
  this._LRRFT = new RFIDController.LastReadRFTagNumber();
  try
  {
    if (string.IsNullOrEmpty(userName) | string.IsNullOrEmpty(userPass))
      throw new Exception("Reader username or password cannot be empty.");
    this.IPAddress = HostIP;
    this.ReaderUser = userName;
    this.ReaderPW = userPass;
    this.AntennaNo = AntennaNoPort;
    this.Connect();
  }
  catch (Exception ex)
  {
    ProjectData.SetProjectError(ex);
    throw new Exception("Error occured while instantiating controller : " + ex.InnerException.ToString());
  }
}

为什么我得到

的错误
  

“对象引用未设置为实例”

在我的C#代码中而不在我的VB代码中?经检查,我可以将我的参数装满。  我真的需要您的帮助

1 个答案:

答案 0 :(得分:-1)

reader = new RFIDController(HP.RfIdAddress, "HP.RfIdUserName", "HP.RfIdPassword", HP.RfIdAntena);

应该是:

reader = new RFIDController(HP.RfIdAddress, HR.RfIdUserName, HP.RfIdPassword, HP.RfIdAntena);

也不是每个异常都具有InnerException。这样可能会导致NullReferenceException,从而在catch块调用时隐藏真正的错误:

ex.InnerException.ToString()