I think a lot of developers zero in on Objects and Classes in C# because it is an Object Orientated development language, and they don’t bother getting to really know the If Statement. This is a real shame as I consider Conditional Logic to be one of the fundamental building blocks of any application.
Setting a Boolean value
Take a look at this code;
if (WebUtil.isDebug == true)
{
btnMerge.Visible = true;
}
else
{
btnMerge.Visible = false;
}
When setting a Boolean value based on the value of another Boolean, you don’t need to use the full If Statement. You can use the following shorthand to get the same result;
btnMerge.Visible = (WebUtil.isDebug);
Obviously, if the Boolean values are opposite, you can just use the ! operator as follows;
btnMerge.Visible = (!WebUtil.isDebug);
You can set your Boolean in this way with any If Statement because If Statements always resolve to either true or false;
btnMerge.Visible = ((x<=2)||(y>=6));
Nested If Statements
When you write an If Statement inside another If Statement, it’s called a nested If Statement. There are very few instances that this is actually required and it makes your code unnecessarily bloated and hard to read.
If you spend some time learning to use your C# Operators and apply yourself to the logic you will be amazed how much unnecessary code you can eliminate.
Here is an example of unnecessary nested If Statements. In the following chunk of Conditional Logic only employees can login impersonating end users. When somebody logs in, the app will send a notification only if they are not in the new Database and not impersonating another user and the notification has not already been sent;
if (Employee)
{
if (AsSelf)//Employee logging in as self
{
if (!obj.InNewDB())
{
if (obj.CheckforSendNotify())
{
NewDBNotify(loginID);
}
}
}
else
{
//Don’t notify because Employee impersonating user
}
}
else // notify because it’s an end user
{
if (!obj.InNewDB())
{
if (obj.CheckforSendNotify())
{
NewDBNotify(loginID);
}
}
}
if ((Employee && AsSelf) || (!Employee))
{
if ((!obj.InNewDB()) && (obj.CheckforSendNotify()))
{
NewDBNotify(loginID);
}
}
Setting a default for a null-able value
You can use the ?? operator to set a default for a null-able value or reference. Don’t worry if that didn’t make sense, just keep reading and it should become clear to you.
In the code below, btnMerge.Visible will get it’s TRUE or FALSE value from MyObject.Boolfield, but if MyObject.Boolfield is NULL, btnMerge.Visible will default to FALSE;
btnMerge.Visible = MyObject.Boolfield ?? false;
If you enjoyed this post, please let me know by leaving a comment. Even if you disagree with anything I have said here, I value your feedback.
Hey Carl,
ReplyDeleteReally great article, it was just what I needed this morning. It's lovely looking at some well written code examples. Thanks for the encouragement.
:)
Thanks Peter
DeleteI'm Glad you enjoyed it!
Nice Mr Bartlett
ReplyDeleteThanks Ismail :)
DeleteThank you good article
ReplyDeleteGlad you Enjoyed it
Deletegreat
ReplyDelete