0

i have created Solution which contain 3 Projects like.

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

This above class should be set once, When user logged in and after that i would like to access these details across entire solution.

Like Administration, SalesInfo projects.

//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; }
    }

Any Answer, Comments or Suggestions would be highly appreciated

M.Babcock
  • 18,753
  • 6
  • 54
  • 84
imdadhusen
  • 2,493
  • 7
  • 40
  • 75
  • Is this a web app? Win forms? WPF? Should the information be accessible to everyone or is this "per user"? Have you looked at design patterns? –  Mar 12 '12 at 11:58
  • 3
    This is a very bad design decision. Hard to test and impossible to extend using OOP mechanisms. If you want a single instance of your class, look up Singleton pattern, but do make your class members instance. And from your question I presume by `static`, you actually mean **read-only**? – vgru Mar 12 '12 at 11:58
  • 1
    You can use a static constructor. – Steven Mar 12 '12 at 11:58
  • 2
    1. You have non-static classes with entirely static members. 2. You don't need to expose `UserDetails` as a static property on these other classes -- it's already globally accessible, just access it where needed as `UserDetails.Name` &c. – Paul Ruane Mar 12 '12 at 12:16
  • This is a terrible terrible idea... – Marc Gravell Mar 12 '12 at 12:34

2 Answers2

1

Normally property uses private field to store the data. This behavior added during compile time and is hidden for the developer while coding. Static method/property can't access private variables/fields in a class.

I recommend to use singleton pattern.

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;
        }
    }
}

And you can consume like this,

//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; }
}
vinodpthmn
  • 1,062
  • 14
  • 28
  • Thanks, my vote of +1, Please see following http://stackoverflow.com/questions/9667931/static-class-property-getting-null-when-custom-validation-fired-in-silverlight-4 – imdadhusen Mar 12 '12 at 13:31
1

Use a kind of singleton as mentioned by 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;
  }

}

After log-in you call UserDetails.CreateUserDetails(...) to set the global object.
To get details call GetUserDetails().

brgerner
  • 4,287
  • 4
  • 23
  • 38
  • Thanks for your valuable answer but my problem is here http://stackoverflow.com/questions/9667931/static-class-property-getting-null-when-custom-validation-fired-in-silverlight-4 – imdadhusen Mar 12 '12 at 13:30
  • Sorry I'm not a Silverlight expert. – brgerner Mar 12 '12 at 14:36