2009-10-22 107 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; 
    } 
} 

希望,它帮助。