Beginning ASP.NET 2.0 With CSharp (2006) [eng]
.pdf
Chapter 9
What Are All Those Curly Brackets and Semicolons For?
These are covered in more detail later in the chapter, but you’ll see a brief explanation now so you understand what you are seeing. The first thing to note is that in C#, a line of code only ends when the semicolon character (;) is reached. This means that you can spread the code line across multiple physical lines to make it easier to read, or so it doesn’t scroll off the end of the window.
The next thing to know is that the code blocks are surrounded by curly brackets ({}), which define the start and end of the code block. There will be lots more on these later in the chapter, explaining when these are required, but for now just know that they are part of the language. The term curly brackets is often used to differentiate them from parentheses of square brackets.
The other really important thing to know about C# is that it is case-sensitive. In Visual Basic .NET it doesn’t matter what case you use, and IntelliSense will correct the language keywords. In C#, however, what you type is what you get, so a common cause of errors is simply mistyping — you know, when you mistakenly hit the Caps Lock key instead of the Shift or Tab key.
Declaring Variables
There is a specific syntax for creating variables, which requires the data type and name. The name is up to you, but it’s always best to use a sensible name, something that represents what the variable holds. To define a variable you use the data type followed by the variable name, like so:
DataType VariableName;
Replace the VariableName and DataType with your requirements:
string FirstName; string LastName;
int Age;
DateTime Birthday;
Here you see sensible names for the variables: there are two string types to hold the first and last names, an int to hold the age, and a DateTime to hold the birthday. In some documents and books, you might see a prefix on the variable name, giving an indication of its data type. For example, the names and ages might be declared like so:
string strFirstName; string sLastName;
int iAge;
DateTime dBirthday;
Here str or s has been used as a prefix for string types, i for int types, and d for DateTime data types. You may find this technique useful when learning how to use variables and data types, although it isn’t generally recommended and has dropped out of favor. A correctly named variable should give a good indication of its data type. Having said that, there are times when some form of prefix is useful, and you’ll see this when you look at classes later in the chapter.
288
Code
Assigning Values
After you declare a variable, you can assign values to it, and this is simply a matter of setting the variable equal to the required value. The syntax for this is as follows:
variable = value;
This says give the variable named on the left of the equals sign the value on the right of the equals sign. For example:
FirstName = “Arthur”;
LastName = “Arbuthnot”;
Age = 24;
As you can see, string values are enclosed in quotation marks, whereas numeric values aren’t.
Variables can also be initialized when they are declared, like so:
string FirstName = “Arthur”; string LastName = “Arbuthnot”;
int Age = 24;
DateTime Birthday = new DateTime(1980, 22, 10);
These examples show the assignment using literal values, but you can also assign values to other variables or objects. For example, in ASP.NET pages, it’s very common to assign values from user-entered data, perhaps from a text box:
FirstName = FirstNameTextBox.Text;
LastName = LastNameTextBox.Text;
Or vice versa:
FirstNameTextBox.Text = FirstName;
LastNameTextBox.Text = LastName;
The TextBox control has a property called Text that contains the text value, so you are simply copying that value into the string variable, or copying the string value into the property. This is fine for string types, but extra work needs to be done if the variable isn’t a string type. For example, allowing a user to enter his age would require a text box and an int type variable. However, you can’t assign the values of differing types — you need to convert them.
Data Conversion
Visual Basic .NET provides some automatic conversion of data types, but C# doesn’t provide this facility, and you have to explicitly convert variables between data types. Explicit conversion makes it clear exactly what your code does, a useful point when you (or others) look at your code later. There isn’t a single syntax for converting between types, but there is a lot of commonality in the different methods.
289
Chapter 9
Converting to string values is the simplest, because every data type has a ToString method. For example, to convert age to a TextBox you could use the following:
AgeTextBox.Text = Age.ToString();
For the bool type the conversion method is the same, but the string value will be either true or false.
Converting from a string value to another type is slightly different, because there aren’t methods on the string type to do this automatically. Instead you have to use a separate class to perform the conversion.
Converting Data Types Using the Framework Classes
There are two ways to convert from string values to other types using the framework classes, and it’s worth mentioning both in case you see them in code. The first is to use the Parse method that most data types supply. For example, to convert a number held in a TextBox control into an int data type, you could do this:
int Age;
Age = int.Parse(AgeTextBox.Text);
What’s happening here is that the Parse method parses the value passed to it — that is, it reads the value, checks that it is an integer value, and converts it into an integer. The value to be converted is the value from the Text property of the AgeTextBox control, which is a string. So the string is passed into the Parse method, which converts it into an integer and returns the integer, which in turn is assigned to the Age variable.
All of the data types, apart from Object, support the Parse method, which means that even though you may be using a different data type, the syntax is the same. For example:
double ANumber;
ANumber = Double.Parse(NumberTextBox.Text);
The second way to perform data conversion is to use the Convert class, which converts between types. It’s a very flexible class because it can convert between all types, but it requires knowledge of the CLR type. For example, the preceding example using an integer could be written as follows:
int Age;
Age = Convert.ToInt32(AgeTextBox.Text);
For the double example, this would be:
double ANumber;
ANumber = Convert.ToDouble(NumberTextBox.Text);
In use there is no difference between the Convert class and the data type class when converting types. The only reason for using the Convert class is that it makes code easier to convert to another language. This was important when writing the Wrox United web site, but may not be important to you if you intend to stick with one language.
290
