blah blah blah is here! blah blah » Close

up0down
link

i have a static class
it has some boolean values that i can get outside the class
but there is also one string value that i cannot access outside the calss
i'm using the same definition method to all
here is my code:
the static class:
public static class UserData
{
public static string UserName;// can't get it outside the class

public static bool btnPayRull; //ok
public static bool btnUserManagement;//ok
public static double CheckUser(string User,string Password)
{
UserName= User;
if(User!="")
{
btnPayRull=True;
btnUserManagement=True;
}
}
}
i'm trying to access the data using
UserData.UserName;
but if returns null
:)

last answered 2 years ago

1 answers

up0down
link

If you try to access UserData.UserName without calling UserData.CheckUser first (which method, incidentally, has some problems), then the return value will be null because that's the default value of a string field.
Notice that null is not the same thing as the empty string "".
However, if you try to access UserData.btnPayRull or UserData.btnUserManagement without calling UserData.CheckUser first, then the return value will be false because this is the default value of a bool field.

up0down
link

thanks for your reply mr vulpes
i'm getting the proper values of both boolean datatypes , i'm getting null value just in UserData.UserName,
in the code that i past i tried to minimize the code of mine, it is the idea
i'm assigning the username value and the other boolean meembers from datadreader that retrieves the data from my database
as i said the boolean members giving proper values, but not the string one..
if(drUser.HasRows)
{
//Apply User Autherities
drUser.Read();
UserName = drUser["userId"].ToString();

btnPayRull =Convert.ToBoolean( drUser["PreparePAyRull"]);
btnUserManagement = Convert.ToBoolean(drUser["AllowAddUser"]);
}
:)

up0down
link

Well, in that case there are two possibilities:
1. The DataReader has no rows, so UserName is left as null and the bools as false.
2. drUser["userId"].ToString() is returning an empty string. You could check whether that's the case by doing a MessageBox.Show on that expression before you assign it to UserName.

up0down
link

i already checked the value of it inside the class
it gets the proper values, it works ok;
but when i try to get the value outside the class it returns nuthing!
there might be something extra that i should add...
or some other way to send the data to the rquired instance..

up0down
link

The only other thing I can think of is that you've inadvertently declared a local string variable called UserName in the method where you're setting the values which is hiding the field of the same name.

up0down
link

it's ok
it worked finaly
:D
i was using the same UserName for function parameter retrieved from the other form..
thanks alot
:)

This post was imported from csharpfriends, if you have a similiar question please ask it again.

All previous members have been migrated, hope you enjoy the new platform!

Feedback