问题描述
||
我有一个要从组合框和文本框获取值的类,但是当我传递值时,它显示以下错误:
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);
}
解决方法
在这里,我认为这是您正在尝试做的事情。
您可能要创建Device1类,如下所示:
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中获取值并将它们传递给类。我相信这是您想要做的。
,很难完全回答,因为您的代码示例不完整。但是对此错误的解释可能会有所帮助。
基本上,您试图静态调用另一个类上的方法或属性,但是该方法或属性未标记为静态。
看一下这个示例,它将不会编译并抛出您得到的错误:
public class Class1
{
public void DoSomething()
{
}
}
public class Class2
{
public Class2()
{
Class1.DoSomething();
}
}
为了解决这个问题,我们需要像下面这样使方法DoSomething成为静态的:
public class Class1
{
public static void DoSomething()
{
}
}
public class Class2
{
public Class2()
{
Class1.DoSomething();
}
}
现在将进行编译,如果您不希望方法是静态的,则另一个选择是在调用它之前创建一个实例:
public class Class1
{
public void DoSomething()
{
}
}
public class Class2
{
public Class2()
{
Class1 myClass1 = new Class1();
myClass1.DoSomething();
}
}
如果您的错误发生在网上:
public int dwCommPort = CboComPort.SelectedIndex;
我怀疑您的组合框名称错误,或者intellisense没有注意而选择了错误的项目。我注意到您以\“ cbo \”开头了组合框的名称,但是该组合框是大写的,因此我怀疑这里的名称是错误的。
,我不确定从哪里开始,但是第一步是您可能想为您的类创建一个构造函数。就像是:
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 /网页中,任何触发OpenDevice方法被调用的操作都将负责构建Device1
对象,然后您可以将其传递给OpenDevice方法。
从Winform背后的代码中,您需要添加对象创建。我认为这是来自按钮的按下...
protected void OnClick(object sender,EventArgs e)
{
Device1 dww=new Device1(cboMachineNo.SelectedIndex,CboComPort.SelectedIndex,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)
,而另一个组合框也使用you11ѭ时,则使用组合框中的SelectedIndex
。