[英]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:我有一个 class 我想从组合框和文本框中获取值,但是当我传递该值时,它显示以下错误:
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.基本上,您正在尝试静态调用另一个 class 上的方法或属性,但该方法或属性未标记为 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:为了解决这个问题,我们需要像这样创建方法 DoSomething static:
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:现在将编译,如果您不希望方法为 static,另一个选项是在调用它之前创建一个实例:
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.
我注意到您以“cbo”开头组合框的名称,但是这个名称是大写的,所以我怀疑这里的名称是错误的。
I'm not sure where to start here, but the first step is that you probably want to create a constructor for your class.我不确定从哪里开始,但第一步是您可能想要为您的 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.现在,在您的 winform/webpage 中,触发要调用的 OpenDevice 方法的任何操作都将负责构建
Device1
object,然后您可以将其传递给 OpenDevice 方法。
From the code behind your winform, you'd need to add the object creation.从您的 winform 背后的代码中,您需要添加 object 创建。 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.请注意,当您可能希望
Int32.Parse(cboMachineNo.SelectedValue)
和其他组合框相同时,您正在使用组合框中的SelectedIndex
。
Here, I think this is what you're trying to do..在这里,我认为这就是你想要做的..
You maybe want to create your Device1 class to look like this:您可能想要创建您的 Device1 class 看起来像这样:
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:然后在您的 Winforms 表单的代码隐藏中,您需要添加与此类似的内容:
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.此代码将从 winform 获取值并将它们传递给 class。 I believe this is what you want to do.
我相信这就是你想要做的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.