none
About class and static ?

    Question

  • Hello

    If i use below code at winform and aspx, what happened ?

    User use different computer. Database at one server.

    Userid have last userid that user entered software ? Or per user have different userid ?

    namespace application
    {
        public class classname
        {
    public static class user
    {
                public static int userid;
                
    }
    }
    //at entered software.
    classname.user.userid = ; // somecodes for get userid.
    //at insert codes
    parameter.add("userid",classname.user.userid);


    • Edited by MSDN.CSharp Wednesday, May 09, 2012 7:38 PM
    Wednesday, May 09, 2012 7:37 PM

Answers

  • At winform class static value be different for per user. At website all class static value be same for all user.
    • Marked as answer by MSDN.CSharp Friday, May 11, 2012 9:42 AM
    Friday, May 11, 2012 9:42 AM

All replies

  • In this code, the "user" class is an internal class of classname, so you must use "classname.user" to refer to it, as you have. Making the class "user" static means that you cannot instantiate an instance of that class; it does not have any effect on the "classname" class itself directly.

    The classname.user.userid field is also static, but marking a field static infers that this field is shared between instances of the "classname.user" class. However, as I've discussed, because the "classname.user" class is static, it will only have one instance.

    So. One instance of "classname.user", and one instance of "classname.user.userid". Every user of your system will share this userid as it is now.

    Wednesday, May 09, 2012 8:11 PM
  • At aspx, i have to use session or viewstate if i want that per user have different userid.

    What should i do for winform ? Can i use class ? Can you explain with code sample ?

    Wednesday, May 09, 2012 8:28 PM
  • static class are static, by memory it's like an struct in C++,
    if you change it it change for all, i mean you cant have instance of those variable and classes, there's only one, and if it change it change for all, you cant access them from object of classname, but you need to access them directly (though in java, i see one compiler which didnt generate any error about calling it through instance, but in C# you can call it only from the class)
    Thursday, May 10, 2012 4:05 AM
  • i didnt write a class in aspx, cause the time i worked over that i was unaware of object - oriented, but i'm pretty much sure that the static variable remain static, and once for all...
    Thursday, May 10, 2012 4:06 AM
  • At winform class static value be different for per user. At website all class static value be same for all user.
    • Marked as answer by MSDN.CSharp Friday, May 11, 2012 9:42 AM
    Friday, May 11, 2012 9:42 AM