2011-05-25 103 views
0

我有一个类,我想从组合框和文本框中获取值,但是当我传递该值时,它显示以下错误:非静态字段,方法或属性需要对象引用

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

} 
+1

tb *定义在哪里?而你错过了一些代码....如果你需要帮助,请发布一个更连贯的代码块。 – Nix 2011-05-25 15:18:56

+0

in form.designer – Mano 2011-05-25 15:21:45

+0

它直接获得价值,但是当我通过它传递它时,它显示以下错误 – Mano 2011-05-25 15:22:40

回答

0

在这里,我想这是你想要做什么..

你也许想创建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中获取值并将它们传递给类。我相信这是你想要做的。

+0

谢谢这就是我想要的 – Mano 2011-05-26 08:53:44

0

我不知道在这里开始,但第一步是,你可能要为你的类的构造函数。喜欢的东西:

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

    } 
} 

请注意,您在使用SelectedIndex从下拉列表框,当你可能想Int32.Parse(cboMachineNo.SelectedValue)与同为其他组合框。

+0

我做到了这一点,但是当我通过构造函数它通过我显示相同的错误 – Mano 2011-05-25 15:30:09

+0

这是一个winform – Mano 2011-05-25 15:30:36

+0

我只是简单的想要从winfoam(组合框,文本box)into class – Mano 2011-05-25 15:37:53

1

由于代码示例不完整,很难完全回答。但是这个错误的解释可能会有所帮助。

基本上你试图静态调用另一个类的方法或属性,但该方法或属性没有标记为静态。

看看这个样本,它不会编译和扔你得到的错误:

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; 

我怀疑你已经得到了组合框的名字写错了,或者智能感知已经选择了错误的项目而没有给出提示。我注意到你用“cbo”开始你的组合框的名字,但是这个有大写,所以我怀疑这里的名字是错误的。

+0

如果他有一个方法在同一个类中声明,那么这行就没有错。 – drharris 2011-05-25 15:41:42

+0

我没有这SomeObject – Mano 2011-05-25 15:42:29

+0

你可以帮我一个忙 – Mano 2011-05-25 15:45:21

相关问题