2012-03-12 76 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