Simply looking at your problem, you could use ternary expressions.
Just like this:
usertype = obj.usertype; // this returns either "User" or "Admin"
int result = usertype.ToLower() == "User" ? 0 : 1;
Editdata(result);
Hope this can help you.
Best regards,
Xudong Peng
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
What is the parameter to Editdata? Why not make it a boolean? true=Admin, false = user.
Editdata(usertype.ToLower() == "admin");
Better still, what is the type of the horribly named variable called 'obj'? It hurts me to even type that name. I'll call it currentUser instead. It should have a boolean property called something like "IsAdmin" which hides the determination of user/admin
from clients. Now you get rid of the temp variable called 'usertype', there is no string conversion, there is no 'if'. All the code you have shown becomes the single statement.
Editdata( currentUser.IsAdmin );
Much better.
PS - part of the reason for handing off the determination to the client is so the code is only one place and you don't get errors like the original question which has 'admin actions' in the 'user' section and the post by XuDong Peng in this stream which
turns all users into admin - probably not a good idea!
Personally, I would probably just pass the currentUser to Editdata and let it work it out. What if, in the future, the call to Editdata is in several places and the test becomes more complicated - 'user', 'admin', 'manager' or 'service>5' or 'role=customerRep'
or ...? These sorts of actions should be hidden from clients. At the moment you have too much knowledge in the client. Without understanding what you are really doing it is hard to give good advice - it looks like there might be side-effects happening.
Editdata(currentUser);
PS - A really good reason for moving the test into a single place (inside Editdata) is to reduce the possibility of errors. Interestingly the original post has the comment "do admin stuff" in the user section and vice versa. The second post gives all users
Admin privileges! If mistakes can be made when the code is just written once then imagine what happens once the code is repeated in other places.
Member
281 Points
1002 Posts
How can i set a boolean depending on a string value without using if statement ?
Jun 26, 2020 08:52 AM|robby32|LINK
I have the following
usertype = obj.usertype; // this returns either "User" or "Admin"
I then do this check
if (usertype .ToLower() == "user")
{
Editdata(0);
//do admin stuff
}
else
{
Editdata(1);
//do user stuff
}
I want to try and not call Editdata twice . How can i do this?
thanks
Contributor
2110 Points
674 Posts
Re: How can i set a boolean depending on a string value without using if statement ?
Jun 26, 2020 10:24 AM|XuDong Peng|LINK
Hi robby32,
Simply looking at your problem, you could use ternary expressions.
Just like this:
Hope this can help you.
Best regards,
Xudong Peng
Participant
1620 Points
929 Posts
Re: How can i set a boolean depending on a string value without using if statement ?
Jun 26, 2020 11:51 AM|PaulTheSmith|LINK
What is the parameter to Editdata? Why not make it a boolean? true=Admin, false = user.
Editdata(usertype.ToLower() == "admin");
Better still, what is the type of the horribly named variable called 'obj'? It hurts me to even type that name. I'll call it currentUser instead. It should have a boolean property called something like "IsAdmin" which hides the determination of user/admin from clients. Now you get rid of the temp variable called 'usertype', there is no string conversion, there is no 'if'. All the code you have shown becomes the single statement.
Editdata( currentUser.IsAdmin );
Much better.
PS - part of the reason for handing off the determination to the client is so the code is only one place and you don't get errors like the original question which has 'admin actions' in the 'user' section and the post by XuDong Peng in this stream which turns all users into admin - probably not a good idea!
Personally, I would probably just pass the currentUser to Editdata and let it work it out. What if, in the future, the call to Editdata is in several places and the test becomes more complicated - 'user', 'admin', 'manager' or 'service>5' or 'role=customerRep' or ...? These sorts of actions should be hidden from clients. At the moment you have too much knowledge in the client. Without understanding what you are really doing it is hard to give good advice - it looks like there might be side-effects happening.
Editdata(currentUser);
PS - A really good reason for moving the test into a single place (inside Editdata) is to reduce the possibility of errors. Interestingly the original post has the comment "do admin stuff" in the user section and vice versa. The second post gives all users Admin privileges! If mistakes can be made when the code is just written once then imagine what happens once the code is repeated in other places.
Member
78 Points
56 Posts
Re: How can i set a boolean depending on a string value without using if statement ?
Jun 27, 2020 05:31 AM|Zoost|LINK
EditUserData()
{
userType = obj.userType;
switch(userType)
{
case userType.ToLower() = "admin":
//Do Admin stuff
default:
//Do User stuff
}
EditData();
}
All-Star
58224 Points
15671 Posts
Re: How can i set a boolean depending on a string value without using if statement ?
Jun 27, 2020 07:17 PM|bruce (sqlwork.com)|LINK
To set Boolean it’s just
bool b = usertype .ToLower() == "user";