简体   繁体   中英

An object reference is required for the non-static field, method, or property

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

}

It is difficult to answer completely because you're code sample is incomplete. But an explanation of this error may help.

Basically you're trying to call a method or property on another class statically, but that method or property isn't marked as static.

Take a look at this sample, it will not compile and throw the error you're getting:

public class Class1
{
    public void DoSomething()
    {
    }
}

public class Class2
{
    public Class2()
    {
        Class1.DoSomething();
    }
}

To fix this, we need to make the method DoSomething static like so:

public class Class1
{
    public static void DoSomething()
    {
    }
}

public class Class2
{
    public Class2()
    {
        Class1.DoSomething();
    }
}

This will now compile, another option if you do not want the method to be static is to create an instance before you call it:

public class Class1
{
    public void DoSomething()
    {
    }
}

public class Class2
{
    public Class2()
    {
        Class1 myClass1 = new Class1();
        myClass1.DoSomething();
    }
}

If your error is happening on the line:

public int dwCommPort = CboComPort.SelectedIndex;

I suspect you've got the name of the combo box wrong, or intellisense has selected the wrong item without you noticing. I notice you start the names of your combo boxes with "cbo" however this one has upper case so I suspect the name here is wrong.

I'm not sure where to start here, but the first step is that you probably want to create a constructor for your class. Something like:

    public class Device1
    {
        public int dwMachineNumber;
        public int dwBaudrate;
        public int dwCommPort;
        public string dwIPAddress;
        public int dwPort;
        public int dwPassWord;

        public Device1(int machineNumber, int baudRate, int commPort, 
            string IPAddress, int port, int password)
        {
            dwMachineNumber = machineNumber;
            dwBaudrate = baudRate;
            dwCommPort = commPort;
            dwIPAddress = IPAddress;
            dwPort = port;
            dwPassWord = password;
        }
    }

Now, in your winform/webpage, whatever action triggers the OpenDevice method to be called would be responsible for building the Device1 object, and you could then pass that into the OpenDevice method.

From the code behind your winform, you'd need to add the object creation. I assume it's coming from a button press...

protected void OnClick(object sender, EventArgs e)
{
    Device1 dww=new Device1(cboMachineNo.SelectedIndex,
        9600,
        CboComPort.SelectedIndex,
        tbIPAdd.Text.Trim(),
        int.Parse(tbPort.Text.Trim()),
        int.Parse(tbPwd.Text.Trim())
    );
    OpenDevice(flagValue,dww);
}

private bool OpenDevice(int flag, Device1 dww)
{
    bool result = false;

    int DEVICE_BUSY = 0;
    try
    {
        result = OpenCommPort(dww.dwMachineNumber,
            dww.dwBaudrate, dww.dwCommPort, dww.dwIPAddress, dww.dwPassWord,
            dww.dwPort, flag);

    }
}

Note that you're using SelectedIndex from the combo box, when you probably want Int32.Parse(cboMachineNo.SelectedValue) and the same for the other combo box.

Here, I think this is what you're trying to do..

You maybe want to create your Device1 class to look like this:

public class Device1
{
    public int dwMachineNumber;
    public int dwBaudrate;
    public int dwCommPort;
    public string dwIPAddress;
    public int dwPort;
    public int dwPassWord;

    public Device1(int dwMachineNumber, int dwBaudrate, int dwCommPort, string dwIPAddress, int dwPort, int dwPassWord)
    {
        this.dwMachineNumber = dwMachineNumber;
        this.dwBaudrate = dwBaudrate;
        this.dwCommPort = dwCommPort;
        this.dwIPAddress = dwIPAddress;
        this.dwPort = dwPort;
        this.dwPassWord = dwPassWord;
    }
}

Then in the code-behind of your Winforms form you need to add something similar to this:

    private void btnMyButton_Click(object sender, EventArgs e)
    {
        Device1 myDevice1 = new Device1(cboMachineNo.SelectedIndex, 9600, cboComPort.SelectedIndex, tbIPAdd.Text.Trim(), int.Parse(tbPort.Text.Trim(), int.Parse(tbPwd.Text.Trim());

        // Now do something with your populated Device1 object.
    }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM