03.11.2016 Views

Beginning ASP.NET 4.5 in CSharp and VB Opsylum

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Data Types <strong>and</strong> Variables x 147<br />

NOTE The .<strong>NET</strong> Framework used by <strong>ASP</strong>.<strong>NET</strong> is huge <strong>and</strong> conta<strong>in</strong>s thous<strong>and</strong>s<br />

of types with hundreds of thous<strong>and</strong>s of members. Clearly, you cannot memorize<br />

all the types <strong>in</strong> the framework, so you need to make good use of resources like<br />

IntelliSense <strong>and</strong> the onl<strong>in</strong>e help. Navigat<strong>in</strong>g the MSDN site (http://msdn<br />

.microsoft.com/en-us/library/) can sometimes be a daunt<strong>in</strong>g task. However,<br />

I often f<strong>in</strong>d that search<strong>in</strong>g for someth<strong>in</strong>g like typeName type .<strong>NET</strong> MSDN br<strong>in</strong>gs up<br />

exactly what I need. So, if I wanted to learn more about the str<strong>in</strong>g class, I’d type<br />

str<strong>in</strong>g class .<strong>NET</strong> MSDN <strong>in</strong> my favorite search eng<strong>in</strong>e. N<strong>in</strong>e out of ten times the<br />

first result is a l<strong>in</strong>k to the relevant page on the MSDN website, where I can learn<br />

more about the class — where it’s def<strong>in</strong>ed <strong>and</strong> located <strong>and</strong> how to use it.<br />

DATA TYPES AND VARIABLES<br />

At first when you th<strong>in</strong>k about data that is used <strong>in</strong> some programm<strong>in</strong>g environment, you may not<br />

realize that each piece of data has a data type. You may th<strong>in</strong>k that a computer would store the text<br />

Hello World <strong>in</strong> exactly the same way as today’s date or the number 26; as a series of characters, for<br />

example. However, to be able to effectively work with data, many programm<strong>in</strong>g languages have different<br />

data types, <strong>and</strong> each data type is constra<strong>in</strong>ed to a specific type of <strong>in</strong>formation. Out of the box,<br />

the .<strong>NET</strong> Framework comes with a long list of data types that enable you to work with numbers<br />

(such as Int32, Int16, <strong>and</strong> Double), text str<strong>in</strong>gs (Char <strong>and</strong> Str<strong>in</strong>g), dates (DateTime), true/false<br />

constructs (the Boolean), <strong>and</strong> more. A list of the most common types is supplied later <strong>in</strong> this section.<br />

For each major type of data there is a special data type. To work with that data, you can store it<br />

<strong>in</strong> a variable that you need to declare first us<strong>in</strong>g the required data type. In <strong>VB</strong>.<strong>NET</strong> you use Dim<br />

myVariable As DataType, whereas <strong>in</strong> C# you use DataType myVariable to declare a variable. A<br />

valid variable name typically consists of letters, numbers, <strong>and</strong> underscores, <strong>and</strong> cannot start with a<br />

number. These rules apply to other identifiers as well, such as classes <strong>and</strong> methods, which you see<br />

later. The follow<strong>in</strong>g example shows you how to declare two variables: an Integer (<strong>in</strong>t <strong>in</strong> C#) to<br />

hold a number <strong>and</strong> a Str<strong>in</strong>g (str<strong>in</strong>g <strong>in</strong> C#) to hold a piece of text:<br />

<strong>VB</strong>.<strong>NET</strong><br />

' Declare a variable of type Integer to hold medium sized whole numbers.<br />

Dim distanceInMiles As Integer<br />

' Declare a variable to hold some text like a first name.<br />

Dim firstName As Str<strong>in</strong>g<br />

C#<br />

// Declare a variable of type <strong>in</strong>t to hold medium sized whole numbers.<br />

<strong>in</strong>t distanceInMiles;<br />

// Declare a variable to hold some text like a first name.<br />

str<strong>in</strong>g firstName;<br />

These two code examples also conta<strong>in</strong> comments, prefixed with a tick (') <strong>in</strong> <strong>VB</strong>.<strong>NET</strong> or two forward<br />

slashes (//) <strong>in</strong> C#. You learn more about comment<strong>in</strong>g your code later <strong>in</strong> this chapter.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!