use a single equals sign in C# unless youre assigning a value to a variable; other-
wise your code will have a very different meaning than you expect!
Breaking Long Lines of Code
Since the message string in the above example was too long to fit on one line in
this book, we used the string concatenation operator to combine two shorter
strings on separate lines to form the complete message. In VB, we also had to
break one line of code into two using the line continuation symbol (_, an under-
score at the end of the line to be continued). Since C# marks the end of each
command with a semicolon (;), you can split a single command over two lines
in this language without having to do anything special.
Well use these techniques throughout this book to present long lines of code
within a limited page width. Feel free to recombine the lines in your own code
if you likethere's no length limit on lines of VB and C# code.
Conditional Logic
As you develop ASP.NET applications, there will be many instances in which
youll need to perform an action only if a certain condition is met, for instance,
if the user has checked a certain checkbox, selected a certain item from a
DropDownList control, or typed a certain string into a TextBox control. We check
for such occurrences using conditionals, the simplest of which is probably the If
statement. This statement is often used in conjunction with an Else statement,
which specifies what should happen if the condition is not met. So, for instance,
we may wish to check whether or not the name entered in a text box is Zak,
redirecting the user to a welcome page if it is, or to an error page if its not:
Visual Basic
If (userName.Text = "Zak") Then
Response.Redirect("ZaksPage.aspx")
Else
Response.Redirect("ErrorPage.aspx")
End If
C#
if (userName.Text == "Zak")
{
Response.Redirect("ZaksPage.aspx");
}
else
{
70
Chapter 3: VB and C# Programming Basics
Response.Redirect("ErrorPage.aspx");
}
Take Care with Case Sensitivity
Instructions are case-sensitive in both C# and VB, so be sure to use if in
C# code, and If in VB code. On the other hand, variable and function
names are case-sensitive only in C#. As such, in C# you could have two
variables called x and X, which would be considered to be different; in VB,
they would be considered to be the same variable.
Often, we want to check for many possibilities, and specify our application to
perform a particular action in each case. To achieve this, we use the Select Case
(VB) or switch (C#) construct:
Visual Basic
Select Case userName
Case "Zak"
Response.Redirect("ZaksPage.aspx")
Case "Mark"
Response.Redirect("MarksPage.aspx")
Case "Fred"
Response.Redirect("FredsPage.aspx")
Case Else
Response.Redirect("ErrorPage.aspx")
End Select
C#
switch (userName)
{
case "Zak":
Response.Redirect("ZaksPage.aspx");
break;
case "Mark":
Response.Redirect("MarksPage.aspx");
break;
case "Fred":
Response.Redirect("FredsPage.aspx");
break;
default:
Response.Redirect("ErrorPage.aspx");
break;
}
71
Conditional Logic

Get Build Your Own ASP.NET 2.0 Web Site Using C# & VB, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.