I have a class from which I want to get values from combo box and textboxes, but when I pass the value it shows the following error:
An object reference is required for the non-static field, method, or property
Here is the code
public class Device1
{
public int dwMachineNumber = cboMachineNo.SelectedIndex;
public int dwBaudrate = 9600;
public int dwCommPort = CboComPort.SelectedIndex;
public string dwIPAddress = tbIPAdd.Text.Trim();
public int dwPort = int.Parse(tbPort.Text.Trim());
public int dwPassWord = int.Parse(tbPwd.Text.Trim());
}
private bool OpenDevice(int flag)
{
bool result = false;
int DEVICE_BUSY = 0;
Device1 dww = new Device1();
try{
result = OpenCommPort(dww.dwMachineNumber,
dww.dwBaudrate,
dww.dwCommPort,
dww.dwIPAddress,
dww.dwPassWord,
dww.dwPort,
flag);
}
Here, I think this is what you’re trying to do..
You maybe want to create your Device1 class to look like this:
Then in the code-behind of your Winforms form you need to add something similar to this:
For this example, I added the code to the event handler of a button. You need to add it where you want it to happen.
This code will take the values from the winform and pass them to the class. I believe this is what you want to do.