Build Your Own ASP.NET 3.5 Website Using C# & VB (15 page)

Read Build Your Own ASP.NET 3.5 Website Using C# & VB Online

Authors: Cristian Darie,Zak Ruvalcaba,Wyatt Barnett

Tags: #C♯ (Computer program language), #Active server pages, #Programming Languages, #C#, #Web Page Design, #Computers, #Web site development, #internet programming, #General, #C? (Computer program language), #Internet, #Visual BASIC, #Microsoft Visual BASIC, #Application Development, #Microsoft .NET Framework

BOOK: Build Your Own ASP.NET 3.5 Website Using C# & VB
10.98Mb size Format: txt, pdf, ePub

“dimension,” while in C#, we simply precede the variable name with the appropriate

data type.

Sometimes, we want to set an initial value for variables that we declare; we can do

this using a process known as
initialization
, which simply involves declaring a

variable and setting its initial value:

Visual Basic

Dim carType As String = "BMW"

C#

string carType = "BMW";

Licensed to [email protected]

56

Build Your Own ASP.NET 3.5 Web Site Using C# & VB

We can declare and/or initialize a group of variables of the same type simultaneously

using a comma-delimited list. This practice isn’t recommended, though, as it makes

the code more difficult to read. I know you’re curious, so here’s how it would look:

Visual Basic

Dim carType As String, carColor As String = "blue"

C#

string carType, carColor = "blue";

Table 3.1
lists the most useful data types available in VB and C#.
Table 3.1. Commonly Used Data Types

VB

C#

Description

Integer

int

whole numbers in the range -2,147,483,648 to

2,147,483,647

Decimal

decimal

numbers up to 28 decimal places; this command is used

most often when dealing with costs of items

String

string

any text value

Char

char

a single character (letter, number, or symbol)

Boolean

bool

true or false

Object

object

a generic type that can be used to refer to objects of

any type

You’ll encounter many other data types as you progress, but this list provides an

overview of the ones you’ll use most often.

Many Aliases Are Available

These data types are the VB-and C#-specific aliases for types of the .NET Framework. For example, instead of Integer or int, you could use System.Int32 in any

.NET language; likewise, instead of Boolean or bool, you could use

System.Boolean, and so on.

Licensed to [email protected]

VB and C# Programming Basics

57

To sum up, once you’ve declared a variable as a given type, it can only hold data

of that type: you can’t put a string into an integer variable, for instance. However,

there are frequently times when you’ll need to convert one data type to another.

Have a look at this code:

Visual Basic

Dim intX As Integer

Dim strY As String = "35"

intX = strY + 6

C#

int intX;

string strY = "35";

intX = strY + 6;

Now, you’d be forgiven for assuming that this could make sense—after all, the string

strY contains a number, so we may wish to add it to another number. Well, this

isn’t so simple for a computer!

VB performs some conversions for us. The VB version of the code will execute

without a hitch, because the string will be converted to a number before the mathematical operation is applied. C#, on the other hand, will throw an error, as it’s more strict than VB about conversions.

As a rule of thumb, it’s better to stay on the safe side and avoid mixing types

wherever possible.

VB and C#: Strongly Typed Languages

Both VB and C# are
strongly typed
languages, which means that they’re very strict

about data types. Many other languages—mostly scripting languages such as

JavaScript—are loosely typed, which means that they’re more flexible when it

comes to dealing with data types, but can cause unintended behaviour if you’re

not careful. For example, if you try to calculate the sum of a number and a string,

as we did in the previous code snippet, the JavaScript interpreter would make

the conversion for you automatically … but what does it convert? It would convert

the integer 6 into a string and join it with the string "35" to make “356”—not

Licensed to [email protected]

58

Build Your Own ASP.NET 3.5 Web Site Using C# & VB

what you intended at all! At times, despite being a strongly typed language at

heart, VB does a bit of background work for you, which makes it slightly easier

to work with.

In .NET, you can (and sometimes need to) explicitly convert the string into an integer

before you’re able to add them up:

Visual Basic

Dim intX As Integer

Dim strY As String = "35"

intX = Int32.Parse(strY) + 6

C#

int intX;

string strY = "35";

intX = Convert.ToInt32(strY) + 6;

Now, both of these examples can be executed successfully—the server ends up

adding two numbers, rather than a number and a string, which we tried initially,

because the string value is converted to a number value before the addition occurs.

This principle holds true whenever we’re mixing types in a single expression.

Arrays

Arrays are a special kind of variable that’s tailored for storing related items of the

same data type. Any one item in an array can be accessed using the array’s name,

followed by that item’s position in the array (its offset). Let’s create a sample page

to see how it’s done:

Visual Basic

LearningASP\VB\Arrays.aspx

<%@ Page Language="VB" %>

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">




Arrays










C#

LearningASP\CS\Arrays.aspx
(excerpt)

<%@ Page Language="C#" %>




The results of this code are shown in Figure 3.3
.

Licensed to [email protected]

60

Build Your Own ASP.NET 3.5 Web Site Using C# & VB

Figure 3.3. Reading an element from an array

There are some important points to pick up from this code. First, notice how we

declare an array. In VB, it looks like a regular declaration for a string, except that

the number of items we want the array to contain is provided in parentheses after

the name:

Visual Basic

LearningASP\VB\Arrays.aspx
(excerpt)

Dim
drinkList(4)
As String

In C#, it’s a little different. First, we declare that drinkList is an array by following

the data type with two empty square brackets. We then use the new keyword to

specify that this is an array of four items:

C#

LearningASP\CS\Arrays.aspx
(excerpt)

string[]
drinkList = new
string[4]
;

A crucial point to realize here is that, in both C# and VB, these arrays are known

as
zero-based
arrays. In a zero-based array, the first item has position 0, the second has position 1, and so on through to the last item, which has a position that’s one

less than the size of the array (3, in this case). So, we specify each item in our array

like this:

Visual Basic

LearningASP\VB\Arrays.aspx
(excerpt)

drinkList(0) = "Water"

drinkList(1) = "Juice"

drinkList(2) = "Soda"

drinkList(3) = "Milk"

Licensed to [email protected]

VB and C# Programming Basics

61

C#

LearningASP\CS\Arrays.aspx
(excerpt)

drinkList[0] = "Water";

drinkList[1] = "Juice";

drinkList[2] = "Soda";

drinkList[3] = "Milk";

Note that C# uses square brackets for arrays, while VB uses standard parentheses.

We have to remember that arrays are zero-based when we set the label text to the

value of the second array item, as shown here:

Visual Basic

Other books

More Than a Memory by Marie James
Paradise Lane by Ruth Hamilton
Rumor Central by ReShonda Tate Billingsley
The Raven Boys by Maggie Stiefvater
The Scent of Jasmine by Jude Deveraux
Vampire in Denial by Mayer, Dale