2012-03-12 43 views
0

我已經創建了包含3個項目的解決方案。如何將每個屬性創建爲靜態類?

//Project Name: ClientProject 
    public class UserDetails 
    { 
     public static int ID { get; set; } 
     public static string Name { get; set; } 
     public static string Email { get; set; } 
    } 

這上面的類應設置一次,當用戶登錄之後,我想跨越整個解決方案訪問這些信息。

與管理,SalesInfo項目一樣。

//Project Name: Administration 
    public class Admin 
    { 
     public static UserDetails Details 
     { 
      //Here i would like to return UserDetails 
      get; 
     } 
     public static int DepartmentID { get; set; } 
     public static string Phone { get; set; } 
     public static string Head { get; set; } 
    } 

    //Project Name: SalesInfo 
    public class Sales 
    { 
     public static UserDetails Details 
     { 
      //Here i would like to return UserDetails 
      get; 
     } 
     public static DateTime Date { get; set; } 
     public static string Item { get; set; } 
     public static int Price { get; set; } 
    } 

任何回答,意見或建議將高度讚賞

+0

這是一個網絡應用程序?贏表格? WPF?信息應該可以被所有人接受嗎?還是這個「每個用戶」?你看過設計模式嗎? – Chuck 2012-03-12 11:58:07

+3

這是一個非常糟糕的設計決定。很難測試,也無法擴展使用OOP機制。如果你想要一個你的類的實例,查找Singleton模式,但是確實讓你的類成員實例。從你的問題,我認爲'靜態',你的意思是**只讀**? – Groo 2012-03-12 11:58:28

+1

您可以使用靜態構造函數。 – Steven 2012-03-12 11:58:32

回答

1

通常屬性使用私有字段來存儲數據。此行爲在編譯期間添加,並且在編碼時對開發人員隱藏。靜態方法/屬性不能訪問類中的私有變量/字段。

我建議使用單例模式。

public class UserDetails 
{ 
    private static UserDetails _instance; 

    public int ID { get; set; } 
    public string Name { get; set; } 
    public string Email { get; set; } 

    private UserDetails() {} 

    public static UserDetails Instance 
    { 
     get 
     { 
      if (_instance == null) 
      { 
       _instance = new UserDetails(); 
      } 
      return _instance; 
     } 
    } 
} 

而且你可以消耗這個樣子,

//Project Name: Administration 
public class Admin 
{ 
    public static UserDetails Details 
    { 
     get 
     { 
      return UserDetails.Instance; 
     } 
    } 
    public static int DepartmentID { get; set; } 
    public static string Phone { get; set; } 
    public static string Head { get; set; } 
} 
+0

謝謝,我的投票+1,請參閱以下http://stackoverflow.com/questions/9667931/static-class-property-getting-null-when-custom-validation- silverlight-4 – imdadhusen 2012-03-12 13:31:51

1

由Groo提到用一種單身的。

public class UserDetails 
{ 
    public static int ID { get; private set; } 
    public static string Name { get; private set; } 
    public static string Email { get; private set; } 

    private static UserDetails _userDetails; 

    private UserDetails(int id, string name, string email) 
    { 
    ID = id; 
    Name = name; 
    Email = email; 
    } 

    public static UserDetails CreateUserDetails(int id, string name, string email) 
    { 
    if (_userDetails != null) 
    { 
     throw new MyException("Second call to UserDetails.CreateUserDetails!"); 
    } 
    _userDetails = new UserDetails(id, name, email); 

    return _userDetails; 
    } 

    public static UserDetails GetUserDetails() 
    { 
    if (_userDetails == null) 
    { 
     throw new MyException("Not yet created by calling UserDetails.CreateUserDetails!"); 
    } 

    return _userDetails; 
    } 

}

後登錄你打電話UserDetails.CreateUserDetails(...)設置全局對象。
獲取詳細信息致電GetUserDetails()

+0

感謝您的寶貴答案,但我的問題在這裏http://stackoverflow.com/questions/9667931/static-class-property-getting-null-when-custom-validation-fired-in- silverlight-4 – imdadhusen 2012-03-12 13:30:50

+0

謝謝,我的投票+1 – imdadhusen 2012-03-12 13:31:16

+0

對不起,我不是Silverlight專家。 – brgerner 2012-03-12 14:36:24