imdadhusen
imdadhusen

Reputation: 2494

how to create static class List property inside static class

I have created following class:

       public static class Current
        {
            public static class User
            {
                public static int UserID { get; set; }
                public static string UserName { get; set; }
                public static List<UserRole> Role { get; set; }
            }
            public static class UserRole
            {
                public static int RoleID { get; set; }
                public static string RoleName { get; set; }
            }
       }

But it will gives me an error: in this line

public static List<UserRole> Role { get; set; }

Error 1 'Framework.Security.Current.UserRole': static types cannot be used as type arguments

Upvotes: 1

Views: 8928

Answers (3)

Jeff Galvao
Jeff Galvao

Reputation: 21

I understand what you are trying to do. (although you're kind of going the wrong way about it.) The only Static Class should be User and they should not be Nested. There are a few other issues with your design but in the end everyone works differently.

This is one way you could have a List within a Static Class.

        public static class CurrentUser           
        {              
            public static int UserID { get; set; }              
            public static string UserName { get; set;   }


            public static List<UserRole> Role()
            {
                //Call another class with method to fill the list
                AnotherClass _anotherClass = new AnotherClass();
                return _anotherClass.Roles();

            }
        }  

Then have a non-static UserRole

public  class UserRole         
        {              
        public  int RoleID { get; set; }     
        public  string RoleName { get; set; }      
        }

And the class where you retrieve your data from should contain a method to fill the List

            public class AnotherClass
        {
            public List<UserRole> Roles()
            {
                 List<UserRole> outList = new List<UserRole>();
                 //Fill the List with your UserRoles
                 foreach (object MyData in MyDataList)
                 {
                     UserRole myRole = new UserRole();
                     outList.Add(myRole);
                 }

                 return outList;
            }

        }

Upvotes: 2

Teudimundo
Teudimundo

Reputation: 2670

The problem here is that you cannot use UserRole as a type for the List. Since it is a static class it would not make really sense. A list is made of objects that are instances of a class. There is no such thing as an instance for static class.

You should reconsider your design, you have some serious flaw in it.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500055

This has nothing to do with where you're trying to use the class - it has everything to do with the fact that you can't use a static class as a type argument. Given that you can't create an instance of a static class, how could a List<UserRole> ever be useful?

I strongly suspect that those classes shouldn't be static classes to start with - why on earth would you want them to be?

(It's also not clear why they should be nested classes, but that's a different matter.)

Upvotes: 8

Related Questions