[英]An object reference is required for the non-static field, method, or property
我有一个 class 我想从组合框和文本框中获取值,但是当我传递该值时,它显示以下错误:
An object reference is required for the non-static field, method, or property
这是代码
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);
}
很难完全回答,因为您的代码示例不完整。 但是对这个错误的解释可能会有所帮助。
基本上,您正在尝试静态调用另一个 class 上的方法或属性,但该方法或属性未标记为 static。
看看这个示例,它不会编译并抛出你得到的错误:
public class Class1
{
public void DoSomething()
{
}
}
public class Class2
{
public Class2()
{
Class1.DoSomething();
}
}
为了解决这个问题,我们需要像这样创建方法 DoSomething static:
public class Class1
{
public static void DoSomething()
{
}
}
public class Class2
{
public Class2()
{
Class1.DoSomething();
}
}
现在将编译,如果您不希望方法为 static,另一个选项是在调用它之前创建一个实例:
public class Class1
{
public void DoSomething()
{
}
}
public class Class2
{
public Class2()
{
Class1 myClass1 = new Class1();
myClass1.DoSomething();
}
}
如果您的错误在线上发生:
public int dwCommPort = CboComPort.SelectedIndex;
我怀疑你把组合框的名称弄错了,或者智能感知在你没有注意到的情况下选择了错误的项目。 我注意到您以“cbo”开头组合框的名称,但是这个名称是大写的,所以我怀疑这里的名称是错误的。
我不确定从哪里开始,但第一步是您可能想要为您的 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 machineNumber, int baudRate, int commPort,
string IPAddress, int port, int password)
{
dwMachineNumber = machineNumber;
dwBaudrate = baudRate;
dwCommPort = commPort;
dwIPAddress = IPAddress;
dwPort = port;
dwPassWord = password;
}
}
现在,在您的 winform/webpage 中,触发要调用的 OpenDevice 方法的任何操作都将负责构建Device1
object,然后您可以将其传递给 OpenDevice 方法。
从您的 winform 背后的代码中,您需要添加 object 创建。 我假设它来自按钮按下...
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);
}
}
请注意,当您可能希望Int32.Parse(cboMachineNo.SelectedValue)
和其他组合框相同时,您正在使用组合框中的SelectedIndex
。
在这里,我认为这就是你想要做的..
您可能想要创建您的 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;
}
}
然后在您的 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.
}
对于此示例,我将代码添加到按钮的事件处理程序中。 您需要将它添加到您希望它发生的位置。
此代码将从 winform 获取值并将它们传递给 class。 我相信这就是你想要做的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.