Global Variable - Problem in diff. session.
-
Hi to all,
public class xyz {
public static int gUserID = 1;
}
When 1st user Login gUserID value is = 5 and second user login gUserID value is = 7 Problem is 1st user refresh or re-direct to another page gUserID is changing to 7. How to set Global variable for session or any solution to above problem. Thanks.
-
Hi to all,
public class xyz {
public static int gUserID = 1;
}
When 1st user Login gUserID value is = 5 and second user login gUserID value is = 7 Problem is 1st user refresh or re-direct to another page gUserID is changing to 7. How to set Global variable for session or any solution to above problem. Thanks.
How is the variable being updated? The single variable is used by every instance of the class. If you want each instance to have a
userId
then you need something like this:public class Test {
// note it's private! This stop anything else from changing it.
private static int nextUserId = 1;private int userId; public Test() { this.userId = Test.nextUserId++; } public int getUserId() { return this.userId; }
}
Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
-
How is the variable being updated? The single variable is used by every instance of the class. If you want each instance to have a
userId
then you need something like this:public class Test {
// note it's private! This stop anything else from changing it.
private static int nextUserId = 1;private int userId; public Test() { this.userId = Test.nextUserId++; } public int getUserId() { return this.userId; }
}
Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
Hi Nagy Vilmos, I m update gUserID from database. As soon as login to application. I m setting corresponding user id to that variable and I m utilizing that variable in complete project as global variable. here I m facing problem. As soon as 2nd person login The Value is reset (2nd user id is updating). I m trying your method now Thanks.
-
Hi Nagy Vilmos, I m update gUserID from database. As soon as login to application. I m setting corresponding user id to that variable and I m utilizing that variable in complete project as global variable. here I m facing problem. As soon as 2nd person login The Value is reset (2nd user id is updating). I m trying your method now Thanks.
Now I understand. You have an instance for each user, so the variable must not be
static
. If it is possible to set the value in the constructor, then that is the best thing because you can make the member variablefinal
and so it cannot be changed. It is strongly advised to not expose member variables outside the class, it is ALWAYS better to use in this case a method; as I used in my example. If the variable isfinal
, then you can to some extent relax the rule as there is no way it can be changed; but this is normally reserved for constants.
Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
-
Now I understand. You have an instance for each user, so the variable must not be
static
. If it is possible to set the value in the constructor, then that is the best thing because you can make the member variablefinal
and so it cannot be changed. It is strongly advised to not expose member variables outside the class, it is ALWAYS better to use in this case a method; as I used in my example. If the variable isfinal
, then you can to some extent relax the rule as there is no way it can be changed; but this is normally reserved for constants.
Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
my +5. I would like to point out that the UserID should also be an automatically created number that the user object should not modify - it's hard to identify the data otherwise.
regards Torsten When I'm not working
-
Hi to all,
public class xyz {
public static int gUserID = 1;
}
When 1st user Login gUserID value is = 5 and second user login gUserID value is = 7 Problem is 1st user refresh or re-direct to another page gUserID is changing to 7. How to set Global variable for session or any solution to above problem. Thanks.