2013-04-26 104 views
0

我有一個Windows應用程序,它使用串行端口向微處理器發送消息和從微處理器接收消息。如何訪問此方法中的變量

該應用程序工作正常,做什麼應該做的。現在,我需要對從串行接收的數據進行一些闡述,我想在SetText方法中訪問變量「value」。 如何從其他方法或類訪問該變量的內容? 感謝您的幫助。

delegate void SetTextCallback(string text); 
    private void SetText(string text) 
    { 
     if (this.txtOutput.InvokeRequired) 
     { 
      SetTextCallback d = new SetTextCallback(SetText); 
      this.BeginInvoke(d, new object[] { text }); 


     } 
     else 
     { 
      txtOutput.AppendText(text); 
     } 



     // capture messages from serial port 
     if (txtOutput.Text.Length > 0) 
     { 
      MatchCollection mc = Regex.Matches(txtOutput.Text, @"(\+|-)?\d+"); 
      if (mc.Count > 0) 
      { 
       long value = long.Parse(mc[mc.Count - 1].Value); 


       if (value > 1 && value < 1000) 
       { 
        textBox2.Text = value.ToString(); 
       } 
       else if (value < 2000 && value > 1000) 
       { 
        value = value - 1000; 
        textBox3.Text = value.ToString(); 
       } 

      } 
     } 
    } 
    private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) 
    { 

     try 
     { 
      SetText(serialPort1.ReadExisting()); 
     } 

     catch (Exception ex) 
     { 
      SetText(ex.ToString()); 
     } 

    } 
+0

通過傳遞它,或使其公開或使其他方法/類別可達到。 – Patashu 2013-04-26 10:42:51

+2

公開財產? – DGibbs 2013-04-26 10:43:04

+0

創建一個屬性是全球.. – pordi 2013-04-26 10:43:58

回答

0

這是我做了什麼,這是現在working.Thanks給大家的好建議。我很確定,我將盡快在應用程序的其他開發中使用您的示例。

internal long value; 
     private void SetText(string text) 
     { 
      if (this.txtOutput.InvokeRequired) 
      { 
       SetTextCallback d = new SetTextCallback(SetText); 
       this.BeginInvoke(d, new object[] { text }); 


      } 
      else 
      { 
       txtOutput.AppendText(text); 
      } 
      // capture messages from serial port 
      if (txtOutput.Text.Length > 0) 
      { 
       MatchCollection mc = Regex.Matches(txtOutput.Text, @"(\+|-)?\d+"); 

       if (mc.Count > 0) 
       { 
        value = long.Parse(mc[mc.Count - 1].Value); 
        if (value > 1 && value < 1000) 
        { 
         textBox2.Text = value.ToString(); 
        } 
        else if (value < 2000 && value > 1000) 
        { 
         value = value - 1000; 
         textBox3.Text = value.ToString(); 
        } 

       } 
      } 
     } 
1

您可以通過訪問其他類變量「靜態」變量或實例變量

public class Demo1 
{ 
    //Static variable can be accessed without instantiating an instance of Demo1 
    public static int Number;  //Demo1.Number 
    public string Info {get;set;} 
} 

public class AnotherClass 
{ 
    void DoSth() 
    { 
     Demo1.Number ++; 
    } 
} 

,或者如果你有demo1的實例,說demo1Instance

demo1Instance.Info="Sth you like"; 
+0

謝謝你的例子,但我無法使用它。你能對我的代碼做一個實際的例子嗎? – FeliceM 2013-04-26 11:11:04

+0

現在正在工作。感謝大家的支持。 – FeliceM 2013-04-26 14:50:04

2

考慮這個:

設爲財產

public long Value { get; set; } 

在您的代碼中使用此代碼。

if (txtOutput.Text.Length > 0) 
     { 
      MatchCollection mc = Regex.Matches(txtOutput.Text, @"(\+|-)?\d+"); 
      if (mc.Count > 0) 
      { 
       value = long.Parse(mc[mc.Count - 1].Value); 


       if (value > 1 && value < 1000) 
       { 
        textBox2.Text = value.ToString(); 
       } 
       else if (value < 2000 && value > 1000) 
       { 
        value = value - 1000; 
        textBox3.Text = value.ToString(); 
       } 

      } 

如果您想確保此屬性保留其值,則使用靜態值。

public static long Value { get; set; } 
+0

@ PradipKT,考慮我是初學者。你在哪裏放置公共靜態長值{get;組; }? SetText之外不起作用。 – FeliceM 2013-04-26 11:01:10

+0

緊接着類聲明 – pordi 2013-04-26 12:40:03

2

如果數據將要使用一個以上的地方,那麼不要猶豫,只需要創建一個類是這些方法之間共享輸出變量的列表。爲該類中的變量創建屬性。現在爲全局類創建一個對象,並將檢索到的值從微處理器分配給這個全局聲明對象中的屬性。你可以在任何地方訪問它。由於這是一個Windows應用程序,數據將保留,直到您清除或應用程序關閉。

這是代碼。我在Windows應用程序中有一個文本框和兩個按鈕。一個按鈕用於獲取數據,另一個用於顯示數據。數據是使用文本框從用戶獲得的。在點擊顯示數據按鈕獲取數據後,它將顯示推送到對象的數據的次數。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
    { 
public partial class Form1 : Form 
{ 
    // Declare Global Variable 
    DataHolder objDataHolder = new DataHolder(); 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     // Here use your code to load the data retrieved from Microprocessor 
     objDataHolder.UserData = txtUserData.Text; 
    } 

    private void button2_Click(object sender, EventArgs e) 
    { 
     MessageBox.Show(objDataHolder.UserData); 
    } 
} 

// Class to hold Data 
public class DataHolder 
{ 
    // Here have a list variables that you need to maintain that are retrieved from Microrocessor. 
    private string _userdata = string.Empty; 

    // Here have a list Properties that you need to maintain that are retrieved from Microrocessor. 
    public string UserData 
    { 
     get 
     { 
      return _userdata; 
     } 
     set 
     { 
      _userdata = value; 
     } 
    } 
} 

}

+0

謝謝,這個概念很明確,但我需要一個關於我的代碼的實例。我是一個初學者,爲了完成這個任務而奮鬥。 – FeliceM 2013-04-26 11:13:28

+0

非常感謝。我將更改我的代碼以包含您編碼的功能。再次感謝,真的很感謝。 – FeliceM 2013-04-26 12:15:12

+0

隨時歡迎FeliceM – 2013-04-26 12:17:02