2009-10-22 104 views
0

在ASP.NET C#中,如何設置從目前值類的靜態的變量值在非靜態類.edx:我也叫靜態類staticA和一個非靜態類B稱爲system.WEb.UI.Page。我目前在B類的一些價值觀,我想設置爲靜態類A的屬性值,這樣我可以在整個項目在一個靜態類訪問非靜態類的價值

任何想法使用它呢?

回答

3
staticA.AValue = b.BValue 
+0

是b是B類對象嗎? – Shyju 2009-10-22 06:03:31

+0

是的,如果你是在B類的方法,你可以寫BValue – 2009-10-22 06:42:01

2

「正確」的方法是通過您的具體實例B的混淆類及其實例!!!)到A的方法,將複製的任何性質(或其他值)它需要。

1

見下面的例子:

public static class staticA 
{ 
    /// <summary> 
    /// Global variable storing important stuff. 
    /// </summary> 
    static string _importantData; 

    /// <summary> 
    /// Get or set the static important data. 
    /// </summary> 
    public static string ImportantData 
    { 
     get 
     { 
      return _importantData; 
     } 
     set 
     { 
      _importantData = value; 
     } 
    } 
} 

和CLASSB

public partial class _classB : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     // 1. 
     // Get the current ImportantData. 
     string important1 = staticA.ImportantData; 

     // 2. 
     // If we don't have the data yet, initialize it. 
     if (important1 == null) 
     { 
      // Example code only. 
      important1 = DateTime.Now.ToString(); 
      staticA.ImportantData = important1; 
     } 

     // 3. 
     // Render the important data. 
     Important1.Text = important1; 
    } 
} 

希望,它幫助。