C#winforms .NET2.0类接口问题

时间:2016-05-30 19:48:16

标签: c# winforms interface

我正在尝试实施由Fredrik Mork提供的this question的最佳答案的 更好的解决方案 版本。由于我不熟悉堆叠溢出,我没有足够的声誉点来为他的回答添加评论,直接问他。我如何引起他对这个问题的关注? @FredrikMork? @ fredrik-mork?

在Fredrik的解决方案中,他提供了3段示例代码。首先是接口声明,第二个是显示需要从另一个类操作的属性的类,然后是第三个是另一个类,但我不知道其他类中的代码是什么

请注意我是C#和OOP的新手(我正在逐步完成//由John Sharp逐步推出Microsoft Visual C#2013)@JohnSharp

我在VS 2013中使用.NET 2.0(使用API​​来控制在.NET 2.0中制作的蓝牙适配器)中执行Windows窗体解决方案。

我根据上述问题的答案制作了界面,除了在我的情况下我试图将我在CCIN_Comm类中生成的字符串以我的Form1类的形式添加到列表框中。

所以,在Program.cs中我有:

namespace EP1
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }

    class CCINS_Comm : IDisposable
    {
        // Other stuff like constants
        //                  members
        //                  properties




        // combined constructors  this was fine...
        public CCINS_Comm(IForm1Interface form1)
        {
            m_communicator = null;
            m_deviceCb = null;
            m_bleMgrCb = null;
            m_peerDevice = null;
            m_gattClientCb = null;
            this.form1 = form1;
        }


        /// <summary>
        /// Start scan
        /// </summary>
        public CyApiErr StartScan()
        {
            return StartScanHelper();
        }

     /// <summary>
    /// Setup the scan result handler to log the discovered BLE devices
    /// </summary>
    private void SetupScanResultHandler()
        {
            m_scanCb.ScanResultHandler = (result) =>
            {
                if (result != null)
                {
                    StringBuilder SB = new StringBuilder();
                    foreach (var item in result.ScanRecords)
                    { 
                        SB.Length = 0;
                        SB.AppendFormat("Peer device: [{0:X12}, {1}], ADV_TYPE: {2}, RSSI: {3}",
                            item.PeerDeviceAddress.Address,
                            item.PeerDeviceAddress.AddressType,
                            item.AdvertisementType,
                            item.RSSI);
                        if(SB.ToString() != "")
                        {
                            string str = SB.ToString();    // Troubleshooting
                            form1.AddToScanResultsList(str); // Object reference not set to an instance of an object Error happens here
                        }

                    }
                }
            };
        }
   }
}

然后,在Form1.cs中我有

namespace EP1
{

    // This is the interface.  this is in the right place (not in either class)
    interface IForm1Interface
    {
        void AddToScanResultsList(string resultString);
    }



    public partial class Form1 : Form, IForm1Interface
    {

        CCINS_Comm ccinsComm; 


        public Form1()
        {
            InitializeComponent();
            InitalizeStimControls();
            ccinsComm = new CCINS_Comm(this);
        }


       // various methods handling manipulation of controls, etc.


        // method that the interface refers to 
        public void AddToScanResultsList(string resultString)
        {
            listBoxScanResults.Items.Add(resultString);
        }

    }
}

在您将此标记为重复问题并破坏我获得帮助的任何机会之前,请注意我已搜索错误代码并且未找到与界面问题相关的答案。

当我在打开调试的情况下运行它时,我得到一个未处理的类型&#39; System.NullReferenceException&#39;发生在EP1中附加信息:对象引用未设置为对象的实例。

我不明白为什么我没有将Object引用设置为对象的实例。我已经检查过以确保我打算传递的字符串不为空。

EDIT1:这不是this question的重复问题,因为没有一个答案与接口有关。这个问题是关于如何正确构建一个接口。

EDIT2:我用我最近尝试解决问题来更新代码。仍然没有用,但我想我离我更近了。

EDIT3:编辑代码以反映提供的解决方案让我超过原始错误但现在又发生了另一个错误。还将这些编辑注释下移到问题文本框的底部。

1 个答案:

答案 0 :(得分:1)

您的错误来自这一行:

CCINS_Comm ccinsComm = new CCINS_Comm(); 

您正在创建CCINS_Comm的新实例,但默认的无参数构造函数未设置CCINS_Comm.form1的值,默认为null(因此为NullReferenceException)。要解决此问题,您需要在ccinsComm构造函数中初始化Form1,并将实现IForm1Interface的对象作为参数传递给它。