Learning the Visual Basic .NET Language

This article presents an overview of the Visual Basic .NET language. It 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).

Contributed by
Rating: 4 stars4 stars4 stars4 stars4 stars / 26
November 17, 2005
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

BEFORE YOU CAN CREATE an ASP.NET application, you need to choose a .NET language to program it. If you’re an ASP or Visual Basic developer, the natural choice is VB .NET. If you’re a longtime Java programmer or old-hand C++ coder, C# will probably suit you best. VBScript isn’t an option—it just doesn’t have the range, flexibility, and elegance a modern language demands.

This chapter presents an overview of the Visual Basic .NET language. You’ll learn about the data types you can use, the operations you can perform, and the code you’ll need to define functions, loops, and conditional logic. This chapter assumes that you’ve programmed before and that you’re already familiar with most of these concepts—you just need to see how they’re implemented in VB .NET.

If you’ve programmed with an earlier version of Visual Basic, you might find that the most beneficial way to use this chapter is to browse through it without reading every section. This approach will give you a general overview of the VB .NET language. You can then return to this chapter later as a reference when needed. But remember, though you can program an ASP.NET application without mastering all the language details, this deep knowledge is often what separates the casual programmer from the legendary programming guru.


NOTE  
The examples in this chapter show individual lines and code snippets. You won’t actually be able to use these code snippets in an application until you’ve learned about objects and .NET types. But don’t despair—the next chapter builds on this information, fills in the gaps, and presents an ASP.NET example for you try out.

The .NET Languages

The .NET Framework 1.1 ships with four languages: VB .NET, C#, JScript .NET, and J#. These languages are, to a large degree, functionally equivalent. Microsoft has worked hard to eliminate language conflicts in the .NET Framework. These battles slow down adoption, distract from the core framework features, and make it difficult for the developer community to solve problems together and share solutions. According to Microsoft, choosing to program in VB .NET instead of C# is just a lifestyle choice and won’t affect the performance, interoperability, feature set, or development time of your applications. Surprisingly, this ambitious claim is essentially true.

.NET also allows other third-party developers to release languages that are just as feature rich as VB .NET or C#. These languages (which already include Eiffel, Perl, Python, and even COBOL) “snap in” to the .NET Framework effortlessly. The secret is the special machine.config configuration file that is installed with the .NET Framework. In one of its sections, the machine.config file lists the language compilers that are currently installed.

<!-- Other configuration settings omitted.  -->
<compilers>
   <compiler language="c#;cs;csharp" extension=".cs" 
   type= "Microsoft.CSharp.CSharpCodeProvider, System, Version=1.0.5000.0"/>
   
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"  
   type=
"Microsoft.VisualBasic.VBCodeProvider, System, Version=1.0.5000.0"/>
   
<compiler language="js;jscript;javascript" extension=".js" 
   type= "Microsoft.JScript.JScriptCodeProvider, Microsoft.JScript,
   Version=7.0.5000.0"/>
   
<compiler language="VJ#;VJS;VJSharp" extension=".jsl" 
   type= "Microsoft.VJSharp.VJSharpCodeProvider, VJSharpCodeProvider,
   Version=7.0.5000.0"/>
</compilers>


TIP  
To view the machine.config file, look for the directory [WinDir]\ Microsoft.NET\Framework\[Version]\Config. For example, after installing .NET 1.1 on a Windows XP computer, the machine.config file is placed in the directory C:\Windows\Microsoft.NET\Framework\v1.1.4322\Config.

To install another .NET language, all you need to do is copy the compiler to your computer and add a line to the machine.config file. (Typically, a setup program will perform these steps for you automatically.) The new compiler will then transform your code creations into a sequence of IL (Intermediate Language) instructions.

IL is the only “language” that the CLR recognizes. When you create the code for an ASP.NET web form, it’s changed into IL using the VB .NET compiler (vbc.exe), the C# compiler (csc.exe), the JScript compiler (jsc.exe), or the J# compiler (vjc.exe). You can perform the compilation manually or let ASP.NET handle it automatically when a web page is requested. We’ll discuss the difference between these two techniques in Chapter 5.

The Evolution of VB .NET

Traditional ASP development was restricted to the VBScript programming language, which was first developed as a basic scripting language for writing macros and other simple code that would be used by another application. VBScript was never intended for sophisticated, interactive web applications, hence expert programmers had to strain the language to its limit to create first-rate ASP pages. To get around many limitations in VBScript, advanced pages needed to rely on separate components written in other languages, which generally had to be installed and configured separately on the web server. In the end, even though VBScript was intended to be easier to use than ordinary Visual Basic, writing advanced ASP pages actually became much more complicated because of the additional effort needed to circumvent VBScript’s limitations.

Just replacing VBScript with Visual Basic would have been a significant advantage. Some of the features Visual Basic 6 offers that VBScript lacks include the following:

  1. Access to the platform services. VBScript, on the other hand, is automatically isolated by the scripting host and has many security-related restrictions.
  2. Typed programming. VBScript doesn’t allow you strict control over data types, and works with special “variant” variables instead, which are supposed to be easier to use. Unfortunately, they also introduce data type conversion problems and difficult-to-detect errors.
  3. Event-driven programming. Unlike Visual Basic, VBScript is notoriously disorganized and has little flexibility to group or organize code so that code can be easily debugged and reused.
  4. Support for objects. Visual Basic doesn’t have perfect object-oriented features, but they are still light years over what VBScript can accomplish.

However, ASP.NET has completely skipped over this stage in evolution and moved directly to the advanced capabilities of Visual Basic .NET. This latest version of Visual Basic is a complete redesign that answers years of unmet complaints and extends the VB language into new territory. Some of the new features include the following:

  • Structured error handling. The end of the aggravating “On Error Goto” construct has finally arrived. VB .NET introduces .NET’s new standard: clean, concise, structured exception handling. You’ll see it in Chapter 11.
  • Language refinements. Every aspect of the VB language has been tweaked and refined. You can now overload functions, declare and assign variables on the same line, and use shortened assignment syntax.
  • Strong typing. Even Visual Basic 6 performed some automatic variable conversions that could cause unusual bugs. VB .NET allows you to rein in your program and prevent possible errors with strict type checking.
  • True object-oriented programming. Inheritance, interfaces, polymorphism, constructors, shared members and abstract classes…the list goes on, and Visual Basic .NET integrates them all into the language.

These are only some of the changes, but they’re enough to show you that VB .NET is separated from VBScript by two major evolutionary leaps. All of these features are available in other .NET languages such as C#, but Visual Basic and VBScript developers will have to adjust the most.

Variables and Data Types

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.

...........................................................................................................................

Arrays

Arrays allow you to store a series of values that have the same data type. Each individual value in the array is accessed using one or more index numbers. It’s often convenient to picture arrays as lists of data (if the array has one dimension), or grids of data (if the array has two dimensions). Typically, arrays are laid out contiguously in memory.

Visual Basic programmers will find that arrays are one of the most profoundly changed language elements. In seeking to harmonize .NET languages, Microsoft decided that all arrays must start at a fixed lower bound of 0. There are no exceptions. When you create an array in VB .NET, you simply specify the upper bound.

' Create an array with four strings (from index 0 to index 3).
Dim StringArray(3) As String
' Create a 2 x 4 grid array (with a total of eight integers).
Dim IntArray(1, 3) As Integer

By default, if your array includes simple data types they are all initialized to default values (0, False, or "", depending on whether you are using some type of number, a Boolean variable, or a string). You can also fill an array with data at the same time that you create it. In this case, you don’t need to explicitly specify the number of elements, because .NET can determine it automatically.

' Create an array with four strings, one for each number from 1 to 4.
Dim StringArray() As String = {"1", "2", "3", "4"}

The same technique works for multidimensional arrays, except that two sets of curly brackets are required:

' Create a 4 x 2 array (a grid with four rows and two columns).
Dim IntArray() As Integer = {{1, 2}, {3, 4}, {5, 6}, {7, 8}}

Figure 3-1 shows what this array looks like in memory.

Figure 3-1.  A sample array of integers

To access an element in an array, you specify the corresponding index number in parentheses. Array indices are always zero-based. That means that MyArray(0) accesses the first cell in a one-dimensional array, MyArray(1) accesses the second cell, and so on.

' Access the value in row 0 (first row), column 1 (second column).
Dim Element As Integer
Element = intArray(0, 1)   ' Element is now set to 2.

One nice feature that VB .NET offers is array redimensioning. In VB .NET, all arrays start with an initial size, and any array can be resized. To resize an array, you use the ReDim keyword.

Dim MyArray(10, 10) As Integer
ReDim MyArray(20, 20)

In this example, all the contents in the array will be erased when it’s resized. To preserve the contents, you can use the optional Preserve keyword when redimensioning the array. However, if you’re using a multidimensional array you’ll only be able to change the last dimension, or a runtime error will occur.

Dim MyArray(10, 10) As Integer
ReDim Preserve MyArray(10, 20)   ' Allowed, and the contents will remain.
ReDim Preserve MyArray(20, 20)   ' Not allowed. A runtime error will occur.


TIP  
In many cases, it’s easier to dodge counting issues and use a full-fledged collection rather than an array. Collections are generally better suited to modern object-oriented programming and are used extensively in ASP.NET. The .NET class library provides many types of collection classes, including simple collections, sorted lists, key-indexed lists (dictionaries), and queues. You’ll see examples of collections throughout this book.

Enumerations

An enumeration is a group of related constants, each of which is given a descriptive name. Every enumerated value corresponds to a preset integer. In your code, however, you can refer to an enumerated value by name, which makes your code clearer and helps to prevent errors. For example, it’s much more straightforward to set the border of a label to the enumerated value BoderStyle.Dashed rather than the obscure numeric constant 3. In this case, Dashed is a value in the BorderStyle enumeration, and it represents the number 3.

Here’s an example of an enumeration that defines different types of users:

' Define an enumeration called UserType with three possible values.
Enum UserType
    Admin
    Guest
    Invalid
End Enum

Now you can use the UserType enumeration as a special data type that is restricted to one of three possible values. You assign or compare the enumerated value using the dot notation shown in the following example.

' Create a new value and set it equal to the UserType.Admin constant.
Dim NewUserType As UserType
NewUserType = UserType.Admin

Internally, enumerations are maintained as numbers. In the preceding example, 0 is automatically assigned to Admin, 1 to Guest, and 2 to Invalid. You can set a number directly into an enumeration variable, although this can lead to an undetected error if you use a number that doesn’t correspond to one of the defined values.

In some scenarios, you might want to control what numbers are used for various values in an enumeration. This technique is typically used when the number has some specific meaning or corresponds to some other piece of information. For example, the following code defines an enumeration that represents the error code returned by a legacy component.

Enum ErrorCode
    NoResponse = 166
    TooBusy = 167
    Pass = 0
End Enum

Now you can use the ErrorCode enumeration, which was defined earlier, with a function that returns an integer representing an error condition, as shown here:

Dim Err As ErrorCode
Err = DoSomething()
If Err = ErrorCode.Pass
   
' Operation succeeded.
End If


TIP 
Enumerations are widely used in .NET. You won’t need to create your own enumerations to use in ASP.NET applications, unless you’re designing your own components. However, the concept of enumerated values is extremely important, because the .NET class library uses it extensively. For example, you set colors, border styles, alignment, and various other web control styles using enumerations provided in the .NET class library.

Variable Operations

You can use all the standard type of variable operations in VB .NET. When working with numbers, you can use various math symbols, as listed in Table 3-2. Visual Basic .NET follows the conventional order of operations, performing exponentiation first, followed by multiplication and division, and then addition and subtraction. You can also control order by grouping subexpressions with parentheses.

Dim Number As Integer
Number = 4 + 2 * 3
' Number will be 10.
Number = (4 + 2) * 3
' Number will be 18.

Table 3-2. Arithmetic Operations

Operator

Description

 

+

Addition.

1 +1 = 2.

-

Subtraction (and to indicate negative numbers).

5 - 2 = 3.

*

Multiplication.

2 * 5 = 10.

/

Division.

5 / 2 = 2.5.

^

Exponentiation.

3 ^ 2 = 9.

\

Integer division (any remainder is discarded).

7 \ 3 = 2.

Mod

Gets the remainder left after integer division.

7 Mod 3 = 1.

When dealing with strings, you can use the concatenation operator (&), which joins together two strings.

' Join two strings together. Could also use the + operator.
MyName = FirstName & " " & LastName

The addition operator (+) can also be used to join strings, but it’s generally clearer to use the concatenation operator. The concatenation operator automatically attempts to convert both variables in the expression to the string data type, if they are not already strings.

In addition, Visual Basic .NET now provides special shorthand assignment operators. A few examples are shown here:

' Add 10 to MyValue (the same as MyValue = MyValue + 10).
MyValue += 10
' Multiple MyValue by 3 (the same as MyValue = MyValue * 3).
MyValue *= 3
' Divide MyValue by 12 (the same as MyValue = MyValue / 12).
MyValue /= 12

............................................................................................................................

Line Termination

Sometimes, code statements are too long to efficiently fit on a single line. In Visual Basic, you can break a code statement over multiple lines by adding a space followed by the line-continuation character (an underscore) to the end of the line. Here’s an example:

' A long line of code.
MyValue = MyValue1 + MyValue2 + MyValue3
' A code statement split over several lines in VB.
MyValue = MyValue1 + MyValue2 + _
          MyValue3

.............................................................................................................................

Advanced Math

In the past, every language has had its own set of keywords for common math operations such as rounding and trigonometry. In .NET languages, many of these keywords remain. However, you can also use a centralized Math class that’s part of the .NET Framework. This has the pleasant side effect of ensuring that the code you use to perform mathematical operations can easily be translated into equivalent statements in any .NET language with minimal fuss.

To use the math operations, you invoke the methods of the System.Math class. These methods are shared, which means that they are always available and ready to use. (The next chapter explores the difference between shared and instance members in more detail.)

The following code snippet shows some sample calculations that you can perform with the Math class.

Dim MyValue As Integer
MyValue = Math.Sqrt(81)          ' MyValue = 9
MyValue = Math.Round(42.889, 2)  ' MyValue = 42.89
MyValue = Math.Abs(-10)          ' MyValue = 10
MyValue = Math.Log(24.212)       ' MyValue = 3.18.. (and so on)
MyValue = Math.PI                ' MyValue = 3.14159265358979323846

The features of the Math class are too numerous to list here in their entirety. The preceding examples show some common numeric operations. For more information about the trigonometric and logarithmic functions that are available, refer to the MSDN reference for the Math class.

Type Conversions

Converting information from one data type to another is a fairly common programming task. For example, you might retrieve text input for a user that contains the number you want to use for a calculation. Or, you might need take a calculated value and transform it into text you can display in a web page.

The Visual Basic .NET rules for type conversion are slightly less strict than some other languages, like C#. For example, VB .NET will automatically allow the conversions shown here:

Dim BigValue As Integer = 100
Dim SmallValue As Short
Dim MyText As String = "100"
' Convert your 32-bit BigValue number into a 16-bit number.
SmallValue = BigValue
' Convert your MyText into a number. BigValue = MyText

The problem with code like this, is that it isn’t guaranteed to work. Conversions are of two types: narrowing and widening. Widening conversions always succeed. For example, you can always convert a number into a string, or a 16-bit integer into a 32-bit integer. On the other hand, narrowing conversions may or may not succeed, depending on the data. If you’re converting a 32-bit integer to a 16-bit integer (as in the previous code snippet), you’ll encounter an error if the 32-bit number is larger than the maximum value that can be stored in a 16-bit data type. Similarly, some strings can’t be converted to numbers. A failed narrowing conversion will lead to an unexpected runtime error.

It’s possible to tighten up Visual Basic’s type conversion habits by adding an Option Strict instruction to the beginning of your code files.

Option Strict On

In this case, VB .NET will not allow automatic or implicit data type conversions if they could cause an error or lose data. Instead, you’ll need to explicitly indicate that you want to perform a conversion.

To perform an explicit data type conversion in VB .NET, you use the CType() function. This function takes two arguments. The first specifies the variable you want to convert, and the second specifies the data type you’re converting it to. Here’s how you would rewrite the earlier example with explicit conversions:

 Dim BigValue As Integer = 100
Dim SmallValue As Short
Dim MyText As String = "100"
' Explicitly convert your 32-bit number into a 16-bit number.
SmallValue = CType(BigValue, Short)
' Explicitly convert your string into a number.
BigValue = CType(MyText, Integer)

Just like implicit conversions, explicit conversions can still fail and produce a runtime error. The difference is that when you add the CType() function, you clearly indicate that you’re aware that a conversion is taking place. At the same time that you use CType(), you should add code that either validates your data before you attempt to convert it, or catches any errors using the error-handling techniques described in Chapter 11.

You can also use the classic Visual Basic keywords such as Val(), CStr(), CInt(), CBool(), and so on to perform data type conversions with the standard data types. However, the CType() function is a nice generic solution that works for all scenarios. The examples in this book almost always use explicit conversion with the CType() function. A few exceptions apply. For example, Visual Basic’s built-in Val() function is more convenient than CType() in some scenarios because it just returns a zero if it fails to convert a string to a number.

Dim TextString As String = "Hello"
Dim Number As Integer
Number = Val(TextString)
' Number is now 0, because TextString contains no numeric information.

You’ll also find that you can use object methods to perform some conversions a little more elegantly. The next section demonstrates this approach.

Object-Based Manipulation

.NET is object-oriented to the core. In fact, even ordinary variables are really full-fledged objects in disguise. That means that common data types have the built-in smarts to handle basic operations (like counting the number of letters in a string). Even better, it means that you can manipulate strings, dates, and numbers in exactly the same way in C# and in VB .NET. This wouldn’t be true if developers used special keywords that were built into the C# or VB .NET language.

As an example, every type in the .NET class library includes a ToString() method. The default implementation of this method returns the class name. In simple variables, a more useful result is returned: the string representation of the given variable. The following code snippet demonstrates the use of the ToString() method with an integer.

Dim MyString As String
Dim MyInteger As Integer = 100
' Convert a number to a string. MyString will have the contents "100".
MyString = MyInteger.ToString()

To understand this example, you need to remember that all integers are based on the Int32 class in the .NET class library. The ToString() method is built in to the Int32 class, so it’s available when you use an integer in any language. You should also notice that the line of code that uses the ToString() method is virtually identical in both languages.

The next few sections explore the object-oriented underpinnings of the .NET data types in more detail.


NOTE  
Object-based code isn’t just easier to translate from one language to another, it’s also more elegant. From an object-oriented perspective, it makes more sense when the functionality you use to manipulate a data type originates from the data type itself.

The String Class

One of the best examples of how class members can replace built-in functions is found with strings. You might be familiar with the standard Visual Basic string functions such as Len(), Left(), Right(), and Instr(). These functions have been a part of Visual Basic and VBScript for years, and they’re still available in VB .NET. However, a better solution is to use the methods of the String class, which provides greater consistency between .NET languages.

The following code snippet shows several ways to manipulate a string using its object nature.

Dim MyString As String = "This is a test string   "
MyString = MyString.Trim()          ' = "This is a test string"
MyString = MyString.Substring(0, 4) ' = "This"
MyString = MyString.ToUpper()       ' = "THIS"
MyString = MyString.Replace("IS", "AT") ' = "THAT"
Dim Length As Integer = MyString.Length ' = 4

The first few statements use built-in methods, like Trim(), Substring(), ToUpper(), and Replace(). These methods generate new strings, and each of these statements replaces the current MyString with the new string object. The final statement uses a built-in Length property, which returns an integer that represents the number of letters in the string.


TIP 
A method is just a function or procedure that’s hard-wired into an object. A property is similar to a variable—it’s a piece of data that’s associated with an object. You’ll learn more about methods and properties in the next chapter.

Note that the Substring() method requires a starting offset and a character length. Strings use zero-based counting. That means that the first letter is in position  0, the second letter is in position 1, and so on. This marks a sharp change from the traditional Visual Basic functions, which used one-based counting. This change is found throughout the .NET Framework for the sake of consistency.

You can even use these methods in succession in a single (rather ugly) line:

MyString = MyString.Trim.SubString(0, 4).ToUpper().Replace("IS", "AT")

Or, to make life more interesting, you can use the string methods on string literals just as easily as string variables:

MyString = "hello".ToUpper()  ' Sets MyString to "HELLO"

Table 3-3 lists some useful members of the System.String class.

Table 3-3. Useful String Members

 

 

Member

Description

Length

Returns the number of characters in the string  (as an integer).

ToUpper() and ToLower()

Changes the string to contain all uppercase or all lowercase characters. 

Trim(), TrimEnd(), and TrimStart()

Removes spaces or some other characters from either (or both) ends of a string.

PadLeft() and PadRight()

Adds the specified character to either side of a string, the number of times you indicate. For example, PadLeft(3, " ") adds three spaces to the left side.

Insert()

Puts another string inside a string at a specified (zero-based) index position. For example, Insert(1, "pre") adds the string "pre" after the first character of the current string.

Remove()

Removes a specified number of strings from a specified position. For example, Remove(0, 1) removes the first character.

Replace()Replaces a specified substring with another string. For example, Replace("a", "b") changes all "a" characters in a string into "b" characters.

Substring()

Extracts a portion of a string of the specified length at the specified location. For example, Substring(0, 2) retrieves the first two characters.

StartsWith() and EndsWith()

Determines whether a string ends or starts with a specified substring. For example, StartsWith("pre") will return either True or False, depending on whether the string begins with the letters "pre" in lowercase. 

 

Table 3-3. Useful String Members (Continued)

 

 

Member

Description

IndexOf() and LastIndexOf()

Finds the zero-based position of a substring in a string. This returns only the first match, and can start at the end or beginning. YOu can also use overloaded versions of these methods that accept a parameter that specifies the position to start the search.

Split()

Divides a string into an array of substrings delimited by a specific substring. For example, with Split(".") you could chop a paragraph into an array of sentence strings.

Join()

Joins an array of strings together. You can also specify a separator that will be inserted between each element.
 

The DateTime and TimeSpan Classes

The DateTime and TimeSpan data types also have built-in methods and properties. These class members allow you to perform the following three useful tasks:

  • Extract a part of a DateTime (for example, just the year), or convert a TimeSpan to a specific representation (such as the total number of days or total number of minutes).
  • Easily perform date calculations.
  • Determine the current date and time and other information (such as the day of the week or whether or not the date occurs in a leap year).

For example, the following block of code creates a DateTime object, sets it to the current date and time, and adds a number of days. It then creates a string that indicates the year that the new date falls in (“for example, “2003”).

Dim MyDate As DateTime = DateTime.Now
MyDate = MyDate.AddDays(365)
Dim DateString As String = MyDate.Year.ToString()

The next example shows how you can use a TimeSpan object to find the total number of minutes between two DateTime objects.

Dim MyDate1 As DateTime = DateTime.Now
Dim MyDate2 As DateTime = DateTime.Now.AddHours(3000)
Dim Difference As TimeSpan
Difference = MyDate2.Subtract(MyDate1)
Dim NumberOfMinutes As Double NumberOfMinutes = Difference.TotalMinutes

These examples give you an idea of the flexibility .NET provides for manipulating date and time data. Tables 3-4 and 3-5 list some of the more useful built-in features of the DateTime and TimeSpan objects.

Table 3-4. Useful DateTime Members

 

 

Member

Description

Now

Gets the current date and time.

Today

Gets the current date, and leaves time set to 00:00:00.

Year, Date, Day, Hour, Minute, Second, and Millisecond

Returns one part of the DateTime object as an integer. For example, Month will return 12 for any day in December.

DayOfWeek

Returns an enumerated value that indicates the day of the week for this DateTime, using the DayOfWeek enumeration. For example, if the date falls on Sunday, this will return DayOfWeek.Sunday.
Add() and Subtract()

Adds or subtracts a TimeSpan from the DateTime.

AddYears(), AddMonths(), AddDays(), AddHours(), AddMinutes(), AddSeconds(), AddMilliseconds()

Adds an integer that represents a number of years, months, and so on, and returns a new DateTime. You can use a negative integer to perform a date subtraction.

 DaysInMonth()

Returns the number of days in the month represented by the current DateTime.

IsLeapYear()

Returns True or False depending on whether the current DateTime is in a leap year.

ToString()

 Changes the current DateTime to its string representation. You can also use an over-loaded version of this method that allows you to specify a parameter with a format string.

 

Table 3-5. Useful TimeSpan Members

Member

Description

Days, Hours, Minutes, Seconds, Milliseconds

Returns one component of the current TimeSpan. For example, the Hours property can return an integer from 0 to 23. 

TotalDays, TotalHours, TotalMinutes, TotalSeconds, TotalMilliseconds

Returns teh total value of the current TimeSpan, indicated as a number of days, hours, minutes, and so on. For example, the TotalDays property might return a number like 234.342.

Add() and Subtract()

Combines TimeSpan objects together.

FromDays(), FromHours(), FromMinutes(), FromSeconds(), FromMilliseconds()

Allows you to quickly specify a new TimeSpan. For example, you can use TimeSpan.FromHours(24) to define a TimeSpan object exactly 24 hours long.

ToString()

Changes the current TimeSpan to its string representation. You can also use an over-loaded version of this method that allows you to specify a parameter with a format string.

 

The Array Class

Arrays also behave like objects in the new world of .NET. For example, if you want to find out the size of an array, you won’t use the old-fashioned UBound() function that’s built into the Visual Basic language. Instead, you can use the Array.GetUpperBound() method in any language. The following code snippet shows this technique in action.

Dim MyArray() As Integer = {1, 2, 3, 4, 5} Dim Bound As Integer
' Zero represents the first dimension of an array.
Bound = MyArray.GetUpperBound(0)    ' Bound = 4

Arrays also provide a few other useful methods, which allow you to sort them, reverse them, and search them for a specified element. Table 3-6 lists some useful members of the System.Array class.

Table 3-6. Useful Array Members

Member

Description

Length

Returns an integer that represents the total number of elements in all dimensions of an array. For example, a 3 × 3 array has a length of 9.

GetLowerBound() and GetUpperBound()

Determines the dimensions of an array. As with just about everything in .NET, you start counting at zero (which represents the first dimension).

Clear()

Empties an array’s contents.

IndexOf() and LastIndexOf()

Searches a one-dimensional array for a specified value and returns the index number. You cannot use this with multidimensional arrays.

Sort()

Sorts a one-dimensional array made up of comparable data such as strings or numbers.

Reverse()

Reverses a one-dimensional array so that its elements are backward, from last to first.

Conditional Structures

In many ways, conditional logic—deciding which action to take based on user input, external conditions, or other information—is the heart of programming.

All conditional logic starts with a condition: a simple expression that can be evaluated to True or False. Your code can then make a decision to execute different logic depending on the outcome of the condition. To build a condition, you can use any combination of literal values or variables, along with logical operators. Table 3-7 lists the basic logical operators.

Table 3-7. Logical Operators

Operator

Description

=

Equal to

<>

Not equal to

<

Less than

>

Greater than

<=

Less than or equal to

>=

Greater than or equal to

And

And (evaluates to True only if both expressions are True)

Or

Or (evaluates to True if either expression is True)

You can use the comparison operators (<, >, <=, >=) with numeric types and strings. A string is deemed to be “less than” another string if it occurs earlier in an alphabetic sort. Thus “apple” is less than “attach.”

The If End If Block

The If … End If block is the powerhouse of conditional logic, able to evaluate any combination of conditions and deal with multiple and different pieces of data. Here’s an example with an If … End If block that features two conditions:

If MyNumber > 10 Then
    ' Do something.
ElseIf MyString = "hello" Then
    ' Do something.
Else
   
' Do something.
End If

Keep in mind that the If … End If block matches one condition at most. For example, if MyNumber is greater than 10, the first condition will be met. That means the code in the first conditional block will run, and no other conditions will be evaluated. Whether or not MyString contains the text “hello” becomes irrelevant, because that condition will not be evaluated.

The Select Case Block

VB .NET also provides a Select Case structure that you can use to evaluate a single variable or expression for multiple possible values. In the following code, each case examines the MyNumber variable, and tests if it’s equal to a specific integer.

Select Case MyNumber
    Case 1
        ' Do something if MyNumber = 1.
    Case 2
        ' Do something if MyNumber = 2.
    Case Else
        ' Do something if MyNumber is anything else.
End Select

If desired, you can handle multiple cases with one segment of code by including a list of comma-separated values in the Case statement.

Select Case MyNumber
    Case 1, 2
        ' Do something if MyNumber = 1 Or MyNumber = 2.
    Case Else
        ' Do something if MyNumber is anything else.
End Select

Unlike the If … End If block, Select Case is limited to evaluating a single piece of information at a time. However, it provides a leaner, clearer syntax than the If … End If block for situations in which you need to test a single variable.

Loop Structures

Loop structures allow you to repeat a segment of code multiple times. There are three basic types of loops in Visual Basic .NET. You choose the type of loop based on the type of task you need to perform. Your choices are as follows:

  • You can loop a set number of times with a For … Next loop.
  • You can loop through all the items in a collection of data using a For Each loop.
  • You can loop until a certain condition is met, using a Do … Loop.

    For … Next and For Each loops are ideal for chewing through sets of data that have a known, fixed size. Do … Loop is a more flexible construct that allows you to continue processing until a complex condition is met. Do … Loop is often used with repetitive tasks or calculations that don’t have a set number of iterations.
The For Next Block

The For … Next block is a basic ingredient in many programs. It allows you to repeat a block of code a set number of times, using a built-in counter. To create a For … Next loop, you need to specify a starting value, an ending value, and the amount to increment with each pass. Here’s one example:

Dim i As Integer
For i = 1 To 10 Step 1
   
' This code executes 10 times.
    Debug.Write(i)
Next

In this example, the counter you’re using is a variable named i. The loop begins at 1 and ends at 10. The “Step 1” clause specifies that i will be increased by 1 each time the code passes through the loop. You can omit this part of the code, because 1 is the default increment. Once i reaches 10 the final pass is made through the loop.

If you run this code using a tool like Visual Studio .NET, it will write the following numbers in the Debug window:

1 2 3 4 5 6 7 8 9 10

It often makes sense to set the counter variable based on the number of items you’re processing. For example, you can use a For … Next loop to step through the elements in an array by checking the size of the array before you begin. Here’s the code you would use:

Dim StringArray() As String = {"one", "two", "three"}
Dim i As Integer
For i = 0 To StringArray.GetUpperBound(0)
    Debug.Write(StringArray(i) & " ")
Next

This code produces the following output:

one two three

...............................................................................................................................

Scope Changes from Visual Basic 6

In traditional Visual Basic, there were only two types of scope for a variable: form level and procedure level. VB .NET tightens these rules a notch by introducing block scope. Block scope means that any variables created inside a block structure (such as a conditional If… End If or a For… Next or a Do… Loop) are only accessible inside that block of code.

For i = 0 To 10
    Dim TempVariable As Integer
   
' (Do some calculation with TempVariable.)
Next
' You cannot access TempVariable here.

This change won’t affect many programs. It’s really designed to catch a few more accidental errors. If you need to access a variable inside and outside of some type of block structure, just define the variable before the block starts.

.............................................................................................................................

The For Each Block

VB .NET also provides a For Each block that allows you to loop through the items in a set of data. With a For Each block, you don’t need to create an explicit counter variable. Instead, you create a variable that represents the type of data you’re looking for. Your code will then loop until you’ve had a chance to process each piece of data in the set.

For Each is particularly useful for traversing the data in collections and arrays. For example, the next code segment loops through the items in an array using For Each. This code is identical to the previous example, but a little simpler.

Dim StringArray() As String = {"one", "two", "three"}
Dim Element As String
For Each Element In StringArray
   
' This code loops three times, with the Element variable set to
   
' "one", then "two", and then "three".
    Debug.Write(Element & " ")
Next

In this case, the For Each loop is looking for string in the array. Thus, if defines a string variable named Element. If you used code like this, you wouldn’t retrieve any information at all:

Dim StringArray() As String = {"one", "two", "three"}
Dim Element As Integer
For Each Element In StringArray
   
' This code never executes, because there are
   
' no integers in this array.
Next

For Each iteration has one key limitation: It’s read-only. For example, if you wanted to loop through an array and change the values in that array at the same time, For Each code wouldn’t work. Instead, you would need to fall back on a For … Next block with a counter.

The Do Loop Block

Finally, Visual Basic supports a Do … Loop structure that tests a specific condition after each pass through the loop. To build your condition, you use the While or Until keyword. These two keywords are opposites. While means  “as long as this is true,” and Until means “as long as this is not true.”

Here’s an example that loops ten times. After each pass, the code evaluates whether the counter (i) has exceeded a set value.

Dim i As Integer = 1
Do
   
i += 1
   
' This code executes 10 times.
Loop While i < 10 

You can also place the condition at the beginning of the loop. In this case, the condition is tested before the loop is started. The following code is equivalent to the previous example, unless the condition you’re testing is False to begin with. In that case, none of the code in the loop will execute.

Dim i As Integer = 1
Do While i < 10
   i += 1
   ' This code executes 10 times.
Loop

On the other hand, if you place the condition at the end of the loop, the code will always be executed at least once.


TIP  
Sometimes you need to exit a loop in a hurry. To do so, you’ll need to use the corresponding Exit statement. For example, you can jump out of a For … Next or For Each loop using the Exit For statement, and you can escape from a Do … Loop with Exit Do.

Functions and Subroutines

Functions and subroutines are the most basic building block you can use to organize your code. Ideally, each function or subroutine will perform a distinct, logical task. By breaking your code down into procedures, you not only simplify your life, but you also make it easier to organize your code into classes and step into the world of object-oriented programming.

Visual Basic distinguishes between two types of procedures: ordinary subroutines and functions. The only difference between the two is that functions return a value. To set the return value of a function, you can assign a value to the function name, just as in previous versions of Visual Basic. VB .NET also introduces the useful Return keyword, which allows you to exit a function and return a result in one quick, clear step.

Subroutines are declared with the Sub keyword, and functions are declared with the Function keyword. Here is an example of each one:

Private Sub MySub()
    ' Code goes here.
End Sub
Private Function MyFunc() As Integer 
    
' As an example, return the number 10.
    Return 10
End Function

You’ll notice that both of these procedures are preceded with the accessibility keyword Private. This indicates that these procedures won’t be accessible to code in a different class or module. The next chapter considers classes and accessibility in more detail.

Calling your procedures is straightforward—you simply enter the name of procedure, followed by parentheses. If you call a function, you have the option of using the data it returns, or just ignoring it.

' This call is allowed.
MySub()
' This call is allowed.
MyFunc()
' This call is allowed.
Dim MyNumber As Integer
MyNumber = MyFunc()
' This call isn't allowed.
' MySub does not return any information. MyNumber = MySub()

Parameters

Procedures can also accept information through parameters. Parameters are declared in a similar way to variables. By convention, parameter names always begin with a lowercase first letter in any language.

Here’s how you might create a function that accepts two parameters and returns their sum.

Private Function AddNumbers(number1 As Integer, number2 As Integer) As Integer
    Return (number1 + number2)
End Sub

When calling a procedure, you specify any required parameters in parentheses, or use an empty set of parentheses if no parameters are required.

' Call a subroutine with no parameters. MySub()
' Call a subroutine with two Integer parameters.
MySub2(10, 20)
' Call a function with two Integer parameters and an Integer return value.
Dim ReturnValue As Integer = AddNumbers(10, 10)

Procedure Overloading

VB .NET supports overloading, which allows you to create more than one function or subroutine with the same name, but with a different set of parameters. When you call the procedure, the CLR automatically chooses the correct version by examining the parameters you supply.

This is old hat to C programmers, but a valuable new introduction to the Visual Basic world. It allows you to collect different versions of several functions together. For example, you might allow a database search that returns an author name. Rather than create three functions with different names depending on the criteria, such as GetNameFromID(), GetNameFromSSN(), GetNameFromBookTitle(), you could create three versions of the GetCustomerName() function. Each function would have the same name, but a different signature, meaning it would require different parameters.

To overload a procedure, you use the Overloads keyword. Here’s an example that provides two overloaded versions for the GetProductPrice() method.

Private Overloads Function GetProductPrice(ID As Integer) As Decimal
    ' Code here.
End Function
Private Overloads Function GetProductPrice(name As String) As Decimal
    ' Code here.
End Function
' And so on...

You can look up product prices based on the unique product ID, or the full product name, depending on whether you supply an integer or string argument:

Dim Price As Decimal
' Get price by product ID (the first version).
Price = GetProductPrice(1001)
' Get price by product name (the second version).
Price = GetProductPrice("DVD Player")

You cannot overload a function with versions that have the same signature—that is, the same number of parameters and parameter data types—because the CLR will not be able to distinguish them from each other. When you call an overloaded function, the version that matches the parameter list you supply is used. If no version matches, an error occurs.


NOTE  
.NET uses overloaded methods in most of its classes. This approach allows you to use a flexible range of different parameters, while centralizing functionality under common names. Even the methods we’ve looked at so far (like the String methods for padding or replacing text) have multiple versions that provide similar features with various different options.

Delegates

Delegates are a new feature of the .NET languages. They allow you to create a variable that “points” to a procedure. You can use this variable at any time to invoke the procedure. Delegates help you write flexible code that can be reused in many different situations.

The first step when using a delegate is to define its signature. A delegate variable can only point to a function or procedure that matches its specific signature (list of parameters). For example, if you have a subroutine that accepts a single string parameter, and another subroutine that accepts two string parameters, you’ll need to use a separate delegate type for each subroutine. This enforces a level of type safety that isn’t found with traditional function pointers in languages like C++.

Here’s the code you use to define a delegate and its signature:

' Define a delegate that can represent any function that accepts a single
' string parameter and returns a string. Private Delegate Function StringFunction(in As String) As String

Once you’ve defined a type of delegate, you can create and assign a delegate variable. For example, assume your program has the following function:

Private Function TranslateEnglishToFrench(english As String) As String
    ' Code goes here.
End Function

This function requires one string parameter and returns a string. Thus, it matches the signature used in the definition of the StringFunction delegate. That means you can create a StringFunction delegate variable, and use it to store a reference to the TranslateEnglishToFrench() function. To actually store the reference to the TranslateEnglishToFrench() function, you use the AddressOf statement.

The following code demonstrates this technique:

' Create a delegate variable.
Dim FunctionReference As StringFunction
' Store a reference to a matching procedure. FunctionReference = AddressOf TranslateEnglishToFrench

Once you have a delegate variable that references a function, you can invoke the function through the delegate. To do this, you just use the delegate name as though it were the function name:

Dim FrenchString As String
' Run the method that functionReference points to.
' In this case, it will be TranslateEnglishToFrench().
FrenchString = FunctionReference("Hello")

In the previous code example, the procedure that the FunctionReference delegate points to will be invoked with the parameter "Hello" and the return value will be stored in the FrenchString variable.

..........................................................................................................................

Delegates Are the Basis of Events

Wouldn’t it be nice to have a delegate that could refer to more than one function at once, and invoke them simultaneously? That would allow the client application to have multiple “listeners,” and notify the listeners all at once when something happens.

In fact, delegates do have this functionality, but you’re more likely to see it in use with .NET events. Events, which are described in the next chapter, are based on delegates, but work at a slightly higher level. In a typical ASP.NET program, you’ll use events extensively, but you’ll probably never work directly with delegates.

................................................................................................................................

The Last Word

It’s impossible to do justice to an entire language in a single chapter. However, if you’ve programmed before, you’ll find that this chapter provides all the information you need to get started with the .NET languages. As you work through the full ASP.NET examples in the following chapters, you can refer to this chapter to clear up any language issues. In the next chapter, you’ll learn about more important language concepts, and the object-oriented nature of .NET.

blog comments powered by Disqus
VISUAL BASIC.NET ARTICLES

- Basic Form Properties and Modality in VB.NET
- Multiple Document Interfaces in Visual Basic
- Visual Basic for Beginners
- ASP.NET Image to PDF with VB.Net
- MySQL in ASP.NET: Mono using VB.NET
- AsyncFileUpload File Type and File Size Vali...
- Visual Studio: Adding Functionality and Style
- Clocks and Countdowns
- User-defined Functions using Visual Basic Ap...
- Understanding Object Binding in VBA
- Mastering the Message Box
- Testing a Windows Forms Application
- Using Visual Basic.NET Features to Code a Wi...
- Correcting Code in a Windows Forms Applicati...
- Write Readable Code and Comments for Windows...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 3 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials