I'm trying to implement the An even better solution version of the top answer to this question, offered by Fredrik Mork. Since I am new to stack overflow, I don't have enough reputation points to add a comment to his answer, asking him directly. How do I get his attention to this question? @FredrikMork ? @fredrik-mork ?
In Fredrik's solution, he provides 3 pieces of example code. First is the interface declaration, second shows the class with the property that needs to be manipulated from the other class, then 3rd is the other class, but I don't know what the code in the other class does
Please note that I am new to C# and OOP ( I am working my way through //Step by step Microsoft Visual C# 2013 by John Sharp) @JohnSharp
I'm doing a Windows Forms solution in .NET 2.0(Using API to control a Bluetooth dongle that was made in .NET 2.0), in VS 2013.
I made the interface based on the answer to the question mentioned above, except in my case I'm trying to add a string generated in CCINS_Comm class to a listbox in the form of my Form1 class.
So, in Program.cs I have:
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
}
}
}
};
}
}
}
Then, in Form1.cs I have
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);
}
}
}
Before you mark this as a duplicate question and ruin any chances of me getting help, please note that I have searched on the error code and have found no answers that relate to problems with interfaces.
When I run this with debugging turned on, I get An unhandled exception of type 'System.NullReferenceException' occurred in EP1 Additional information: Object reference not set to an instance of an object.
I don't understand why I get Object reference not set to an instance of an object. I'm already checking to make sure the string I intend to pass is not empty.
EDIT1: this is not a duplicate question to this question, as none of that answers relate to interfaces. This question is about how to correctly construct an interface.
EDIT2: I'm updating the code with my latest attempt to solve the problem. Still doesn't work, but I think I'm closer.
EDIT3: Edited code to reflect the solution provided that gets me past the original error but now another error happens. Also moved these edit notes down to the bottom of the question text box.