Coding Tips and Tricks
topic What are regions and why would I use one?
issue As I use Visual Studio .NET, I notice that it puts region markers (#Region) in my code. What are they and why would I want to use them?
applies to VB and C#

Region markers allow you to define sections of text that can be hidden or shown as desired. You add region markers by typing the Region directive along with a region name as follows:

In VB:

#Region "Properties"

#End Region
			

In C#:

#region Properties
#endregion
			

When the region is closed, the region name appears in the IDE. When the region is open, the Region directive and all information within the region is displayed. These are great for organizing your code.

NOTE: Region directives have no impact on the execution of your application.

topic How do I execute one AND or OR condition but not the other?
issue I want to check for a null in a field and then check the field value. However, if the field is null the second check generates an error. What am I missing?
applies to VB and C#

When you execute a statement with multiple logical AND or OR conditions, each expression is evaluated. For example:

In VB:

If sUserName = "" or sPassword = "" then
    Messagebox.Show("You must enter the Username and Password")
End If
			

In C#:

if (sUserName == "" | sPassword == "")
{
    Messagebox.Show("You must enter the Username and Password");
}
			

In this example, both the sUserName field and sPassword field are checked. If you want to skip evaluation of the second expression when it is not necessary, use short-circuiting. For example:

In VB:

If sUserName = "" orElse sPassword = "" then
    Messagebox.Show("You must enter the Username and Password")
End If
			

In C#:

if (sUserName == "" || sPassword == "")
{
    Messagebox.Show("You must enter the Username and Password");
}
			

By using short-circuiting, the second expression is not evaluated unless necessary.

Short-circuiting operators conditionally evaluate the second expression as follows:

All contents © 2004 InStep Technologies, Inc. All rights reserved.