I've been using variables throughout the first two chapters, and you may have a loose idea of what a variable is and does. Remember a variable is nothing more than a container.
If you come from a traditional ASP background, variables created sloppy coding behavior because variables weren't really variables in the truest sense of the word. They were variants and could basically hold anything without ever indicating what type of data the variable was holding. Now variables must be told what they are holding, otherwise…BOOM!!! It won't work.
We are going to explore variables and their data types in both languages. There are some differences in names, the way you address variables, and how you manipulate them. First I must show you what data types are available and what their names are.
The language-specific data type names in the following table aren't really the data types themselves. They are pointers. Do you know what they point to? You guessed it objects! These objects handle the data types and their properties and methods. These can also be explored in the .NET Framework Class Library explorer mentioned at the end of Chapter 2.
Table 3.1 shows the pointer names for both languages, their corresponding .NET object, and an explanation of what that data type can hold.
Visual Basic .NET Type | C# Type | .NET Framework Type | Range |
---|---|---|---|
Boolean | bool | System.Boolean | True or False |
Byte | byte | System.Byte | 0-255 |
Char | char | System.Char | 0-65535 Unicode Character |
Date | DateTime | System.Datetime | January 1, 0001 to December 31, 9999 |
Decimal | decimal | System.Decimal | Approx. 1.0_10-28 to 7.9_1028 |
Double | double | System.Double | Approx. ±5.0_10-324 to ±1.7_10308 |
Integer | int | System.Int32 | -2,147,483,648 to 2,147,483,647 |
Long | long | System.Int64 | -9,223,372,036, 854,775,808 to 9,223,372,036, 854,775,807 |
N/S | sbyte | System.SByte | -128 to 127 |
Short | short | System.Int16 | -32,768 to 32,767 |
Single | float | System.Single | Approx. ±1.5_10-45 to ±3.4_1038 |
String | string | System.String | 0 to approximately 2 billion Unicode characters |
N/S | ushort | System.UInt16 | 0 to 65,535 |
N/S | uint | System.UInt32 | 0 to 4,294,967,295 |
N/S | ulong | System.UInt64 | 0 to 18,446,744,073, 709,551,615 |
N/S = Not Supported |
Don't be intimidated by all the different data types and numbers. I really only put this in the book to impress you with my knowledge of the vast and seemingly infinite range of data one can manipulate with these data types and their corresponding objects.
YEAH…RIGHT!!!
Seriously, this is best way I can explain data types to you: The raisin between my ears has boiled it down to the descriptions in the sections that follow.
Boolean, which is shown in Table 3.2, is about as simple a data type as there can be. True or false. That's it.
Visual Basic .NET Type | C# Type | .NET Framework Type | Range |
---|---|---|---|
Boolean | bool | System.Boolean | True or False |
There are numbers with decimals, numbers without decimals that can have negative numbers, and numbers without decimals that start at 0 and go up. (The latter are available only in C#.) Big, Bigger, and Super Size, for all three as seen in Table 3.3 and Table 3.4.
Visual Basic .NET Type | C# Type | .NET Framework Type | Range |
---|---|---|---|
Byte | byte | System.Byte | 0-255 |
N/S | sbyte | System.SByte | -128 to 127 |
Short | short | System.Int16 | Big number, no decimal, can start below 0 |
Integer | int | System.Int32 | Bigger number, no decimal, can start below 0 |
Long | long | System.Int64 | Super Size number, no decimal, can start below 0 |
N/S | ushort | System.UInt16 | Big number, no decimal, that starts at 0 |
N/S | uint | System.UInt32 | Bigger number, no decimal, that starts at 0 |
N/S | ulong | System.UInt64 | Super Size number, no decimal, that starts at 0 |
Visual Basic .NET Type | C# Type | .NET Framework Type | Range |
---|---|---|---|
Single | float | System.Single | Big number with decimal |
Double | double | System.Double | Bigger number with decimal |
Decimal | decimal | System.Decimal | Super Size number with decimal |
To give you an idea how big a Super Size number is, its largest possible value is
+/-79,228,162,514,264,337,593,543,950,335
That's like 9 trillion or zillion or something like that. Like I said…SUPER SIZED!!!
Note |
The size differences of the variables in the Table 3.3 and 3.4 bring up an issue that I'd like to address. The larger the variable you use, the more server memory you use, so it is good practice to use variables that are sufficient enough in capability to contain what you will be placing in them but to not use anything larger. Using larger variables puts undue requirements on the server and allocates memory that you will never use. That said, I can tell you that in all my years of programming I've NEVER needed to use a Long or Super Size integer before. |
This data type includes any type of character such as letters, numbers, and special characters. There are only two kinds of character data types. One is very small and the other is very large, as shown in Table 3.5.
Visual Basic .NET Type | C# Type | .NET Framework Type | Range |
---|---|---|---|
Char | char | System.Char | Any single one of the 65535 Unicode characters |
String | string | System.String | 0 to approximately 2 billion Unicode characters |
If you need to use more than one character, you've go no choice but to use a string not a difficult choice to make.
This data type is pretty self-explanatory. It's used to store dates and times, as shown in Table 3.6.
Visual Basic .NET Type | C# Type | .NET Framework Type | Range |
---|---|---|---|
Date | DateTime | System.Datetime | January 1, 0001 to December 31, 9999 |
In the previous chapters, you've seen a bit of variable declaration and initialization, but let's make it official and discuss some different techniques and shortcuts.
Remember, we declare variables like this:
dim var1 as integer
int var1;
And variables are initialized like this:
var1 = 1
var1 = 1;
It's also possible to declare multiple variables of the same type at the same time. They simply need to be separated by commas:
dim var1,var2,var3 as integer
int var1,var2,var3;
You can declare and initialize variables on the same line, as well, for a shortcut of sorts.
dim var1 as integer = 1
int var1 = 1;
Now here's a place where C# has some advantage over Visual Basic .NET. C# allows you to declare and initialize multiple variables of the same type on the same line.
int var1 = 1, var2 = 2, var3 = 3;
You cannot use this type of technique in Visual Basic .NET.
Top |