2011-05-25 100 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

相關問題