Learning the Visual Basic .NET Language - Variables and Data Types (Page 3 of 9 ) As with all programming languages, you keep track of data in VB .NET using variables. Variables can store numbers, text, dates, and times, and can even point to full-fledged objects. When you declare a variable, you give it a name, and you specify the type of data it will store. To declare a local variable, you use the Dim statement, as shown here: ' Create an integer variable named ErrorCode. Dim ErrorCode As Integer ' Create a string variable named MyName. Dim MyName As String NOTE This example shows one other ingredient in VB .NET programming: comments. Comments are descriptive text that is ignored by the compiler. VB .NET comments always start with an apostrophe (') and continue for the entire line.
Every .NET language uses the same variable data types. Different languages may provide slightly different names (for example, a VB .NET Integer is the same as a C# int), but the CLR makes no distinction. This allows for deep language integration. Because languages share the same data types, you can easily use objects written in one .NET language in an application written in another .NET language. No data type conversions are required. In order to create this common data type system, Microsoft needed to iron out many of the inconsistencies that existed between VBScript, Visual Basic 6, C++, and other languages. Their solution was to create a set of basic data types, which are provided in the .NET class library. These core data types are shown in Table 3-1. Table 3-1. Common Data Types Class Library Name | VB .NET Name | C# Name | Contains | Byte | Byte | byte | An integer from 0 to 255. | Int16 | Short | short | An integer from –32768 to 32767. | Int32 | Integer | int | An integer from –2,147,483,648 to 2,147,483,647. | Int64 | Long | long | An integer from about –9.2e18 to 9.2e18. |
Table 3-1. Common Data Types (Continued) Class Library Name | VB .NET Name | C# Name | C# Name | Single | Single | float | A single-precision floating point number from approx. -3.4e38 to 3.4e38. | Double | Double | double | A double-precision floating point number from approx. -1.8e308 to 1.8e308. | Decimal | Decimal | decimal | A 128-bit fixed-point fractional number that supports up to 28 significant digits. | Char | Char | char | A single 16-bit Unicode character. | String | String | string | A variable-length series of Unicode characters. | Boolean | Boolean | bool | A True or False value. | DateTime | Date | N/A* | Represents any date and time from 12:00:00 AM, January 1 of the year 1 in the Gregorian calendar to 11:59:59 PM, December 31, year 9999. Time values can resolve values to 100 nanosecond increments. Internally, this data type is stored as a 64-bit integer. | TimeSpan | N/A* | N/A* | Represents a period of time, as in 10 seconds or 3 days. The smallest possible interval is 1 "tick" (100 nanoseconds). | Object | Object | object | The ultimate class of all .NET types. Can contain any data type or object. |
* If the language does not provide an alias for a given type, you can just use the .NET class name. |
You can also define a variable by using the type name from the .NET class library. This approach produces identical variables. It’s also a requirement when the data type doesn’t have an alias built into the language. For example, you can rewrite the earlier example that used VB .NET data type names with this code snippet that uses the class library names: Dim ErrorCode As System.Int32 Dim MyName As System.String This code snippet uses fully qualified type names that indicate that the Int32 type is found in the System namespace (along with all of the most fundamental types). In Chapter 4, we’ll discuss types and namespaces in more detail. .......................................................................................................................... Data Type Changes from Visual Basic 6 Luckily for VB developers, the list of data types in .NET is quite similar to the data types from earlier versions. Some changes, however, were unavoidable: - Visual Basic defined an Integer as a 16-bit number with a maximum value of 32,767. VB .NET, on the other hand, defines an Integer as a 32-bit number. Long now refers to a 64-bit integer, while Short refers to a 16-bit integer.
- The Currency data type has been replaced by Decimal, which supports more decimal places (and thus greater accuracy).
- Fixed-length strings are no longer supported.
- Variants are no longer supported and have been replaced with the generic Object type, which can hold any object or simple data type.
The Date data type has increased range and precision and works slightly differently. If you’re migrating legacy code that manipulates dates from a previous version of Visual Basic, you may need to rewrite it.
.................................................................................................. Assignment and Initializers Once you’ve created your variable you can freely assign values to them, as long as these values have the correct data type. Here’s the code that shows this two step process: ' Define variables. Dim ErrorCode As Integer Dim MyName As String ' Assign values. ErrorCode = 10 MyName = "Matthew" You can also assign a value to a variable in the same line that you create it. This feature is familiar to most C++ programmers, but it’s a new addition to VB .NET. Here’s an example that compresses four lines of code into two: Dim ErrorCode As Integer = 10 Dim MyName As String = "Matthew" Visual Basic .NET is kind enough to let you use simple data types without initializing them. Numbers are automatically initialized to 0 and strings are initialized to an empty string (""). That means the following code will succeed in VB .NET: Dim Number As Integer ' Number now contains 0. Number = Number + 1 ' Number now contains 1. ........................................................................................................................ What’s in a Name? Not the Data Type. You’ll notice that the preceding examples don’t use variable prefixes. Most Visual Basic and C++ programmers are in the habit of adding a few characters to the start of a variable name to indicate its data type. In .NET, this practice is discouraged, because data types can be used in a much more flexible range of ways without any problem, and most variables hold references to full objects anyway. In this book, variable prefixes aren’t used, except for web controls, in which it helps to distinguish lists, text boxes, buttons, and other common user interface elements. In your own programs, you should follow a consistent (typically companywide) standard that may or may not adopt a system of variable prefixes. ........................................................................................................................... Next: Arrays >>
More Visual Basic.NET Articles More By Apress Publishing
| This article is excerpted from chapter three of the book Beginning ASP.NET in VB.NET: From Novice to Professional, written by Matthew MacDonald (Apress, 2004; ISBN: 1590592786). Check it out at your favorite bookstore today. Buy this book now.
|
| |