Docstoc

VB6 to VB.Net Tutorial

Document Sample
VB6 to VB.Net Tutorial Powered By Docstoc
					VB6 to VB.Net Tutorial

VB6 to VB.Net
Beginners Tutorial

Karl Durrance Issue Status/Number – 1.0 Issue date – 10 th November, 2002

VB6 to VB.Net Tutorial – Version 1.0 Page 1

VB6 to VB.Net Tutorial

Table of Contents
TABLE OF CONTENTS....................................................................................................................................................2 INTRODUCTION................................................................................................................................................................3 PURPOSE............................................................................................................................................................................3 SCOPE................................................................................................................................................................................3 REFERENCES.....................................................................................................................................................................3 VARIABLE DECLARATION ...........................................................................................................................................4 USER DEFINED T YPES (UDT’S).....................................................................................................................................5 OPTION EXPLICIT / OP TION STRICT ..........................................................................................................................5 STRING MANIPULATION COMMANDS.....................................................................................................................6 CODING EXAMPLES AND COMPARISON................................................................................................................7 RETRIEVING THE APPLICATION PATH .......................................................................................................................7 PASSING TIME BACK TO THE CPU................................................................................................................................7 DISPLAYING MESSAGE BOX’S AND RETRIEVING A RESPONSE WITH A CASE STATEMENT ..............................8 W ORKING WITH ARRAYS ..............................................................................................................................................9 Searching an array ...................................................................................................................................................9 Basic array sorting .................................................................................................................................................10 TEXT FILE MANIPULATION .......................................................................................................................................12 Reading Text Files ..................................................................................................................................................12 Writing/Creating Text Files ..................................................................................................................................13 CHECKING IF A FILE EXISTS........................................................................................................................................14 ERROR HANDLING.........................................................................................................................................................14 BASIC M ATHEMATIC FUNCTIONS..............................................................................................................................15 RETURNING VALUES FROM FUNCTIONS....................................................................................................................16 W ORKING WITH THE W INDOWS REGISTRY.............................................................................................................17 Reading the Registry ..............................................................................................................................................17 Writing to the Registry ...........................................................................................................................................17 Creating New Registry Keys .................................................................................................................................17

VB6 to VB.Net Tutorial – Version 1.0 Page 2

VB6 to VB.Net Tutorial

Introduction
Purpose
This tutorial is intended to show some of the differences VB.Net has introduced over VB6.

Scope
This tutorial is aimed at the VB.Net beginner. It will show some of the major differences VB.Net has introduced over VB6. This tutorial does not cover changes in the IDE, only basic code changes.

References
There are no references outside of this document.

VB6 to VB.Net Tutorial – Version 1.0 Page 3

VB6 to VB.Net Tutorial

Variable Declaration
Variable types in VB.Net have altered when compared to VB6. Below is a table listing the variable types and the associated value boundaries for VB.Net.

Variable Type Byte Short Single Long Double Decimal Integer Char String Boolean Date Object

Size 8-Bit 16-Bit 32-Bit F/P 64-Bit 64-Bit F/P 128-Bit 32-Bit 16-Bit 16-Bit 16-Bit 64-Bit 32-Bit

Boundaries 0-255 -32,768 -> 32767 -3.4028235E38 -> 3.4028235E38 -9,223,372,036,854,775,808 -> 9,223,372,036,854,775,807 -1.79769313486231E308 -> 1.79769313486231E308 +/- 79,228 x 1024 -2,147,483,648 -> 2,147,483,647 0 -> 65,535 0 -> Approx 2 Billion Characters True or False Jan 1, 0001 -> Dec 31 9999 All Types

The Variant data type is no longer supported in VB.Net. The Object type can be used in cases where the data type is unknown like Variants were used in VB6. As with VB6 and Variants, the Object type should be avoided.

VB6 to VB.Net Tutorial – Version 1.0 Page 4

VB6 to VB.Net Tutorial

User Defined Types (UDT’s)
UDT’s are no longer declared with the Type keyword as in VB6. Below is an example of a UDT in VB6 and the corresponding UDT in VB.Net VB6
Type UserName LoginName As String FullName As String Address As String MaxLogins As Integer End Type

Structure UserName Dim LoginID As String Dim FullName As String Dim Address As String Dim MaxLogins As Short End Structure

VB.Net

Option Explicit / Option Strict
Option explicit is now defaulted to on instead of off as in VB6. To turn Option Explicit off (even though you shouldn’t) type the following at the top of the form. Option Explicit Off If a variable is not declared in your form it will be created as an Object if the above statement is used. Option Strict is a new command in VB.Net. This statement forces you to convert variable types to the same type before they can be compared. To convert variable types use commands such as CInt, CLng, CStr, CBool and CType functions.

VB6 to VB.Net Tutorial – Version 1.0 Page 5

VB6 to VB.Net Tutorial

String Manipulation Commands
The standard string manipulation commands have now changed in VB.Net. Below is a table mapping the older VB6 methods to the new VB.Net methods.

VB6 Method

VB.Net Method ToUpper

Description

VB.Net Example Dim sString1, sString2 As String sString1 = "this is a test" sString2 = sString1.ToUpper Dim sString1, sString2 As String sString1 = "this is a test" sString2 = sString1.ToLower Dim sString1, sString2 As String sString1 = "this is a test" sString2 = sString1.SubString(5, 2) Dim sString1 As String Dim iLength As Short sString1 = "this is a test" iLength = sString1.Length Dim sString1 As String Dim iPosition As Short sString1 = "this is a test" iPosition = sString1.IndexOf("e") Dim sString1 As String sString1 = String.Concat(“This”, “ is a “, “test”) Dim sString1, sString2 As String Dim bMatch As Boolean sString1 = "this is a test" sString2 = "this is A test" bMatch = Not CBool(StrComp(sString1, sString2, CompareMethod.Binary)) Dim sString1, sString2 As String sString1 = "this a test" sString2 = sString1.Insert(5, "is ") Dim sString1, sString2 As String sString1 = "this is a test" sString2 = sString1.Remove(5, 3)

UCase

Convert to Uppercase

LCase

ToLower

Convert to Lowercase

Mid

SubString

Return a part of a string

Len

Length

Get the length of a string

Instr

IndexOf

Give the index of a substring Concatenate Strings

&

& / Concat

Not Implemented

StrComp

Compare two string

Not Implemented Not Implemented

Insert

Add characters in the middle of a string Remove characters from the middle of a string

Remove

Note: Some Lines in the examples above have been truncated due to table width

VB6 to VB.Net Tutorial – Version 1.0 Page 6

VB6 to VB.Net Tutorial

Coding Examples and Comparison
The following section will give examples of VB6 and the corresponding VB.Net code to perform the same or similar function.

Retrieving the Application Path
Retrieve the directory name the executable has been executed from.

App.Path

VB6

IO. Path.GetDirectoryName (Application.ExecutablePath )

VB.Net

Passing time back to the CPU
Passing time back to the CPU is generally necessary while inside loops.

DoEvents

VB6

Application.DoEvents()

VB.Net

VB6 to VB.Net Tutorial – Version 1.0 Page 7

VB6 to VB.Net Tutorial

Displaying Message Box’s and Retrieving a Response with a Case Statement
Display a popup message to the user during application execution asking a question VB6
Select Case MsgBox("Please Press Yes or No", vbInformation + vbYesNo, "Make a Selection") Case vbNo MsgBox "No Pressed" Case vbYes MsgBox "Yes Pressed" End Select

VB.Net
Select Case MsgBox("Please Press Yes or No", MsgBoxStyle.Information + MsgBoxStyle.YesNo, "Make a Selection") Case vbNo MsgBox("No Pressed") Case vbYes MsgBox("Yes Pressed")

Note: The MessageBox.Sh ow Function can be used instead of the MsgBox Function

VB6 to VB.Net Tutorial – Version 1.0 Page 8

VB6 to VB.Net Tutorial

Working with Arrays
This section will cover some of the primary array manipulation techniques available in VB.Net. Many array techniques such as declaration and cycling through array elements are exactly the same as VB6 and so won’t be covered in this section.

Searching an array

Dim MyArray(4) As String Dim iIndex As Integer

VB6

Dim MyArray(4) As String Dim iIndex As Short

VB.Net

VB6 to VB.Net Tutorial – Version 1.0 Page 9

VB6 to VB.Net Tutorial

Basic array sorting
VB6 This example from VB2TheMax.com (Bubblesort)
Dim MyArray(4) As Single MyArray(0) MyArray(1) MyArray(2) MyArray(3) MyArray(4) = = = = = "1" "5" "2" "4" "3"

Call BubbleSortS(MyArray) Sub BubbleSortS(arr() As Single, Optional ByVal numEls _ As Variant, Optional ByVal descending As Variant)

VB6 to VB.Net Tutorial – Version 1.0 Page 10

VB6 to VB.Net Tutorial

VB.Net
Dim MyArray(4) As String MyArray(0) = "A" MyArray(1) = "D" MyArray(2) = "E" MyArray(3) = "B" MyArray(4) = "C" Array.Sort(MyArray)

Note: In the above example array is sorted A-E
Dim MyArray(4) As String MyArray(0) = "A" MyArray(1) = "D" MyArray(2) = "E" MyArray(3) = "B" MyArray(4) = "C" Array.Sort(MyArray) Array.Reverse(MyArray)

Note: In the above example array is sorted EA

VB6 to VB.Net Tutorial – Version 1.0 Page 11

VB6 to VB.Net Tutorial

Text File Manipulation
The following section will show the process of reading and writing to basic text files in VB.Net. There are a number of different ways to do this, this section will only show the process utilising the streaming file access methods. The below VB.Net examples require the Imports System.IO statement at the top of the form.

Reading Text Files
VB6 – Read Line by Line
Dim sLine As String Open "C:\File.txt" For Input As #1 Do Until EOF(1) Line Input #1, sLine Loop Close #1

VB.Net Read Line by Line
Dim sr As StreamReader = File.OpenText("c:\file.txt") Dim sLine As String Do sLine = sr.ReadLine() Loop Until sLine = Nothing sr.Close()

Dim sr As StreamReader = File.OpenText("c:\file.txt") Dim sAllText As String sAllText = sr.ReadToEnd() sr.Close()

VB.Net Read All

Dim sAllText As String Open "C:\File.txt" For Input As #1 sAllText = Input(LOF(1), #1) Close #1

VB6 Read All

VB6 to VB.Net Tutorial – Version 1.0 Page 12

VB6 to VB.Net Tutorial

Writing/Creating Text Files
VB6
Open "C:\File.txt" For Output As #1 Print #1, "Line1" Print #1, "Line2" Print #1, "Line3" Close #1

VB.Net
Dim fs As FileStream = File.Open("C: \File.txt", FileMode.OpenOrCreate, FileAccess.Write) Dim sr As New StreamWriter(fs) sr.WriteLine("Line1") sr.WriteLine("Line2") sr.WriteLine("Lin e3") sr.Close()

VB6 to VB.Net Tutorial – Version 1.0 Page 13

VB6 to VB.Net Tutorial

Checking if a File Exists
The following section will show the use of the VB.Net File class and how it can be used to detect for the existence of a file. The VB.Net example below requires the Imports System.IO statement at the top of the form VB
If Dir("c:\file.txt") <> "" Then MsgBox("File Found!") Else MsgBox("File Not Found!") End If

VB.Net
If File.Exists("C: \File.txt") Then MessageBox.Show("File Found!") Else MessageBox.Show("File Not Found!") End If

Error Handling
The following section shows how to use the VB.Net Try command to display a prompt to the user a picture in a picture box.

catch errors and when trying to load VB
On Error GoT o ErrorHandler Picture1.Picture = LoadPicture("c: \file.bmp") Try ErrorHandler: MsgBox("Error Loading File!")

VB.Net
PictureBox1.Image = System.Drawing.Bitmap.FromFile("c:\File.bmp") Catch MsgBox("Error Loading File!") End Try

VB6 to VB.Net Tutorial – Version 1.0 Page 14

VB6 to VB.Net Tutorial

Basic Mathematic Functions
VB.Net supports all the mathematic functions that VB6 supports, but some of these functions now exist in the System.Math class VB.Net internally supports the following mathematic functions Addition(+), Subtraction(-), Multiplication(*), Division(/), Integer Division(\), Exponentiation(^). The following functions exist in the System.Math class Abs(), Atan(), Cos(), Exp(), Sign(), Sin(), Sqrt(), Tan() Below is an example of how to get the square root of a number in VB.Net
Imports System.Math

Dim dblResult As Double dblResult = Sqrt(64)

VB6 to VB.Net Tutorial – Version 1.0 Page 15

VB6 to VB.Net Tutorial

Returning Values from Functions
VB.Net now supports a new keyword specifically used for functions. The Return keyword performs the same as setting the function name to a value then and Exit Function. Below is an example of the same function in VB6 and VB.Net VB6
Public Function GetAgePhrase(ByVal Age As Integer) As String If Age > 60 Then GetAgePhrase = "Senior" ElseIf Age > 40 Then GetAgePhrase = "Middle-aged" ElseIf Age > 20 Then GetAgePhrase = "Adult" ElseIf Age > 12 Then GetAgePhrase = "Teen -aged" ElseIf Age > 4 Then GetAgePhrase = "School- aged" ElseIf Age > 1 Then GetAgePhrase = "Toddler" Else GetAgePhrase = "Infant" End If End Function

Public Function GetAgePhrase(ByVal Age As Integer) As String If Age > 60 Then Return "Senior" If Age > 40 Then Return "Middle-aged" If Age > 20 Then Return "Adult" If Age > 12 Then Return "Teen-aged" If Age > 4 Then Return "School-aged" If Age > 1 Then Return "Toddler" Return "Infant" End Function

VB.Net

VB6 to VB.Net Tutorial – Version 1.0 Page 16

VB6 to VB.Net Tutorial

Working with the Windows Registry
VB.Net still supports the GetSetting and SaveSetting commands from VB6, but also allow unrestricted access to the registry via the Microsoft.Win32.Registry class. Below is an example of how to read and write to the registry in VB.Net. VB examples will not be included due to length.

Reading the Registry
VB.Net
Dim oReg As Microsoft.Win32.Registry Dim oRegKey As Microsoft.Win32.RegistryKey Dim sValue As String oRegKey = oReg.LocalMachine.OpenSubKey("Software\Microsoft \Windows NT \CurrentVersion", False) sValue = oRegKey.GetValue("CurrentVersion", vbNullString)

Writing to the Registry
VB.Net
Dim oReg As Microsoft.Win32.Registry Dim oRegKey As Microsoft.Win32.RegistryKey oRegKey = oReg.LocalMachine.OpenSubKey("Key \SubKey", True ) oRegKey.SetValue("Entry", "NewValue")

Creating New Registry Keys

Dim oReg As Microsoft.Win32.Registry Dim oRegKey As Microsoft.Win32.RegistryKey oRegKey = oReg.LocalMachine.CreateSubKey("Key\Subkey\NewKey")

VB.Net

VB6 to VB.Net Tutorial – Version 1.0 Page 17


				
DOCUMENT INFO
Shared By:
Categories:
Stats:
views:258
posted:12/31/2009
language:English
pages:17
Description: VB6 to VB.Net Tutorial