By Wei-Meng Lee
Price: $14.95 USD
£9.95 GBP
Cover | Table of Contents | Colophon
My namespace—gives you the means by which to perform many common tasks without having to work your way through the complex types of the .NET class libraries.
ToolStripContainer control onto the form. The ToolStripContainer control allows other controls (such as the ToolStrip control) to anchor in the four positions available (left, right, top, and bottom).
ToolStripContainer Tasks menu, click on the "Dock Fill in Form" link (see Figure 1-5) to dock the ToolStripContainer control onto the form. This will cause the ToolStripContainer control to fill up the entire form and automatically resize itself when the form is resized.
MenuStrip control in the Toolbox to add it to the form. The MenuStrip control displays a standard list of drop-down menus at the top of a window. In the MenuStrip Tasks menu, click on the Insert Standard Items link to add a list of commonly used menu items to the control (see Figure 1-6).
WindowsApplication1, and then selecting Add → New Item…. In the Add New Item dialog, select Dialog and use the default name of Dialog1.vb as shown in Figure 1-19.
OK_Button) and Cancel (Cancel_Button).Label control shown in Figure 1-20 by dragging and dropping it from the Toolbox. Also, resize the dialog window.
ToolStrip control by entering the code shown in bold in Example 1-2 on the code-behind page.
Private Sub exitToolStripMenuItem_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles exitToolStripMenuItem.Click
DialogResult returned by the Exit dialog box (Dialog1).
FormClosingEventArgs object. To see this result, simply position your cursor over the name of the variable or object that you are interested in while the program is stopped at a breakpoint. Not only can you view the values of variables and objects, you can also edit and change their values during debug time.
WindowsApplication1) in Solution Explorer and select Add New Item…. Select the About Box template and accept the default name of AboutBox1.vb provided by Visual Studio 2005. Click Add (see Figure 1-27).
ToolStrip control as you wish while you are using the application. However, you may have noticed that its position is lost each time you exit the application. This is because you need to manually save its positions each time you exit the application, or else the information will be lost. Fortunately, this can be done easily with the new Application Settings feature in VB 2005. In this section, you'll see how.
ToolStrip control. Select the ToolStrip control and, in its Properties window, select the
PropertyBinding property (under the "(ApplicationSettings)" property; see Figure 1-35). Click the "…" button.
ToolStripLocation (see Figure 1-36). Be sure to set the scope to User. Click OK. Be sure to set the Location property to the newly created application setting.Currency type (which takes up 8 bytes) in VB 6 is replaced in VB 2005 by the Decimal type. The old Decimal type (which takes up 12 bytes in VB 6), is now 16 bytes. Integer is now 4 bytes, instead of its 2 bytes in VB 6. Likewise, the Long data type is now 8 bytes, instead of its 4 bytes in VB 6.
Variant data type in VB 6 is no longer supported in VB 2005; you should use the Object type instead. Object and the types that derive from it are discussed at greater length in Chapter 3.Currency type (which takes up 8 bytes) in VB 6 is replaced in VB 2005 by the Decimal type. The old Decimal type (which takes up 12 bytes in VB 6), is now 16 bytes. Integer is now 4 bytes, instead of its 2 bytes in VB 6. Likewise, the Long data type is now 8 bytes, instead of its 4 bytes in VB 6.
Variant data type in VB 6 is no longer supported in VB 2005; you should use the Object type instead. Object and the types that derive from it are discussed at greater length in Chapter 3.|
VB 2005 type
|
Size (bytes)
|
VB 6 type (size in bytes)
|
|---|---|---|
Boolean
|
Depends on implementing platform
|
Dim (dimension) keyword and you specify its type using the As keyword:
Dim num1
Dim num2 As Integer
num1, by default, to be an Object type. The Object type is the base class of all the classes in the .NET Framework. You can think of the Object type as equivalent to the Variant type in VB 6.num2 to be an Integer variable.num1 as a Short type and then assign a value to it:
'---range: -32768 <--> 32767
Dim num1 As Short
num1 = 32767
Option Strict On statement at the top of your code file. All variables must now be declared with a type.
Option Explicit Off statement. VB 2005 turns on Option Explicit On by default.Option Explicit Off by default. In both VB 6 and VB 2005, it is advisable for you to turn Option Explicit on, because using variables without first declaring them can easily inject potential bugs into your program.Const keyword. The following definition assigns the value 3.14 to a constant whose name is pi and then uses it in calculating the area of a circle:Const pi As Double = 3.14 Dim radius as Double = 5 Dim area as Double = pi * radius ^ 2
Integer types, such as 32, is an Integer type by default.A to ch1 and ch2, both of which are Char types:'---assign the character "A" to ch1 and ch2 Dim ch1 As Char = "A"c Dim ch2 As Char = Chr(65) Dim longValue as Long = 100L
Dim str As String ' assigns str to "He said: "VB is so cool!"" str = "He said: ""VB is so cool!"""
DateTime variable, use the # character:
Dim timeNow As DateTime
timeNow = #3/22/2005 10:01:19 AM#String types are used to represent text and are a good example of a reference type, as you saw in "Variables," earlier in this chapter. Strings in .NET are immutable, which means that once you've assigned a value to a string variable, it cannot be changed. If the value of a string variable is changed, another string object is created during runtime. Consider this example:Dim st As String st = "Hello" st &= " World!" MsgBox(st) ' prints "Hello World!"
Dim i As Integer, str As String = ""
For i = 0 To 10000
str &= i.ToString
Next
StringBuilder class, located in the System.Text namespace:
Dim i As Integer, str As New _
System.Text.StringBuilder()
For i = 0 To 10000
str.Append(i.ToString)
Next
num1 as an array by adding open and closed parentheses to its name:Dim num1() As Integer
num1 to be an array; the actual size of the array is not known yet. To get num1 to point to an actual array, use the New keyword:
num1 = New Integer() {1, 2, 3}
num1 is now an array containing three members of Integer data type with values 1, 2, and 3.Dim num2(2) As Integer num2(0) = 1 num2(1) = 2 num2(2) = 3
Dim num2(2) As Integer = New Integer '---Not allowed since size is ' already indicated
Dim num3() As Integer = _
New Integer() {1, 2, 3}
Dim num3() As Integer = New Integer()
'---Not allowed; missing {}
Dim num3() As Integer = New Integer(3)
'---Not allowed; missing {}
Dim num3() As New Integer
'---Not allowed, arrays cannot use New
Dim num3() as New Integer() {1,2,3}
'---Syntax error
ReDim keyword:
Dim num4() As Integer() = New Integer() {1, 2, 3}
ReDim num4(5)
ReDim an array if the array is initially declared as a variable length array, as the following shows:' array is fixed length Dim num1(3) As Integer ReDim num1(5) '---error ' array is variable length Dim num2() As Integer ReDim num2(5) '---OK
Dim num1 as Short = 25 Dim num2 as Long num2 = num1
Short type to the Long type. Since all the values that could be stored by the Short type can fit into a Long type, this conversion is known as a
widening conversion and is a safe operation. The reverse of widening is a
narrowing conversion, which is a conversion from a data type that has a larger range to one with a lower range. Consider the following:Dim num1 As Long = 25 Dim num2 As Short num2 = num1
num1 may potentially contain a value that will cause an overflow in num2 if the assignment takes place. In VB 2005, you can restrict automatic data type conversion by using the Option Strict statement. By default, in VB 2005, the Option Strict statement is set to Off.Option Strict statement. Hence, the design decision of VB 2005 was to turn Option Strict Off by default so that VB 6 code can be migrated easily.
Option Strict On, you will need to perform an
explicit conversion (or else the compiler will complain):
'---if option strict on
num2 = CShort(num1)
'--OR--
num2 = CType(num1, Short)
Option Strict On, so that any narrowing operations you are doing are flagged. This will allow you to take action to catch potential errors that might result from a narrowing conversion. Note that in VB 6, performing a narrowing conversion will not set off a warning since the Option Strict statement is not supported.CInt, CStr, and CSng|
Type
|
Language element
|
Description
|
|---|---|---|
|
Arithmetic
|
^
|
Raises to the power of
|
|
–
|
Subtraction
| |
|
*
|
Multiplication
| |
|
/
|
Division
| |
|
\
|
Integer Division
| |
|
Mod
|
Modulus (remainder)
| |
|
+
|
Addition
| |
|
Assignment
|
=
|
Assigns a value to a variable or property
|
|
^=
|
Raises the value of a variable to the power of an expression and assigns the result back to the variable (new in VB 2005)
| |
|
*=
|
Multiplies the value of a variable by the value of an expression and assigns the result to the variable (new in VB 2005)
| |
|
/=
|
Divides the value of a variable by the value of an expression and assigns the result to the variable (new in VB 2005) |
If-Then-Else
Select-Case
If-Then-Else construct.If <condition> Then <statement(s)> Else <statement(s)> End if
Dim day As Short = 4 Dim dayofWeek As String If day = 1 Then dayofWeek = "Monday" End If
day is equal to 1, the string "Monday" is then assigned to the dayofWeek variable. For a one-line statement, you can shorten the above code to:If day = 1 Then dayofWeek = "Monday"
End If statement is mandatory. VB 2005 lets you pack a block of conditional code into a single line. For example, the following block of code:
If day = 1 Then
dayofWeek = "Monday"
currentTime = Now
End If
If day = 1 Then dayofWeek = "Monday" : currentTime = Now
Return statement. VB 2005 programmers have three choices: they can write their own functions, continue using most of the VB 6 functions they have come to know and love, or tap into the rich functionality of the .NET Framework Class Library through the new My namespace (see "My Namespace," later in this chapter).Area takes in two input parameters, computes the area, and then returns the result:
Public Function Area(ByVal length As Single, _
ByVal breadth As Single) As Single
Dim result As Single
result = length * breadth
Return result
End Function
Dim areaOfRect As Single = Area(4, 5)
On Error and On Error Resume Next
statements. The specific information about an error that occurred can be retrieved from the Err object.Try… Catch…Finally construct. Basically, you place any code that could possibly trigger a runtime error, such as a disk access, into a Try block. Any errors that happen within the Try block will be caught and serviced by the Catch block(s) that follow. This is where you can take actions to correct the error or clean up any resources that you've allocated. The Finally block is executed whether an error occur in the Try block or not. The Finally block is a good place to perform housekeeping chores such as closing a database or file connection.My namespace, which encapsulates some of the most common functionalities that developers need in their daily work.Microsoft. VisualBasic namespace (which is automatically referenced by default in all VB 2005 projects) and so you can continue to use your favorite VB 6 functions without doing anything extra.My namespace exposes several areas of functionality, as shown in the IntelliSense pop-up in Figure 2-4.
My namespace is to provide direct access to commonly used libraries (in the .NET Framework) like Resources that were previously difficult to access. The intuitive hierarchy of the My namespace provides a mechanism that VB 2005 developers can use to easily navigate the .NET Framework class libraries and locate the classes required to perform a particular task. For example, suppose you want to play an audio file in your application. Which class should you use? Using the My namespace, it is easy to locate the right class to use. As it turns out, the class to use can be found in My.Computer.Audio.Play!My namespace are:My.Application
My.Computer
My namespace is another productive feature that Microsoft has built into the language.