Beginners Tutorial: Issue Status/Number - 1.0 Issue Date - 10 November, 2002
Beginners Tutorial: Issue Status/Number - 1.0 Issue Date - 10 November, 2002
Net Tutorial
VB6 to VB.Net
Beginners Tutorial
Karl Durrance
Issue Status/Number 1.0
Issue date 10 th November, 2002
Table of Contents
TABLE OF CONTENTS....................................................................................................................................................2
INTRODUCTION................................................................................................................................................................3
PURPOSE............................................................................................................................................................................3
SCOPE................................................................................................................................................................................3
REFERENCES.....................................................................................................................................................................3
VARIABLE DECLARATION ...........................................................................................................................................4
USER DEFINED T YPES (UDTS).....................................................................................................................................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 BOXS 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
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.
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
Size
Boundaries
Byte
Short
Single
8-Bit
16-Bit
32-Bit F/P
Long
64-Bit
Double
64-Bit F/P
Decimal
128-Bit
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
Integer
32-Bit
Char
16-Bit
0 -> 65,535
String
16-Bit
Boolean
16-Bit
True or False
Date
64-Bit
Object
32-Bit
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.
Structure UserName
Dim LoginID As String
Dim FullName As String
Dim Address As String
Dim MaxLogins As Short
End Structure
VB.Net
VB6 Method
VB.Net
Method
Description
UCase
ToUpper
Convert to Uppercase
LCase
ToLower
Convert to Lowercase
Mid
SubString
Len
Length
Instr
IndexOf
&
& / Concat
Concatenate Strings
Not
Implemented
StrComp
Not
Implemented
Insert
Not
Implemented
Remove
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)
Note: Some Lines in the examples above have been truncated due to table width
App.Path
VB6
VB.Net
DoEvents
VB6
Application.DoEvents()
VB.Net
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
Searching an array
VB6
VB.Net
=
=
=
=
=
"1"
"5"
"2"
"4"
"3"
Call BubbleSortS(MyArray)
Sub BubbleSortS(arr() As Single,
Optional ByVal numEls _
As Variant, Optional ByVal
descending As Variant)
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 EA
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()
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.
VB
On Error GoT o ErrorHandler
VB.Net
Picture1.Picture =
LoadPicture("c: \file.bmp")
Try
ErrorHandler:
MsgBox("Error Loading File!")
PictureBox1.Image = System.Drawing.Bitmap.FromFile("c:\File.bmp")
Catch
MsgBox("Error Loading File!")
End Try
VB.Net
VB.Net