0% found this document useful (0 votes)
142 views17 pages

Beginners Tutorial: Issue Status/Number - 1.0 Issue Date - 10 November, 2002

This document provides a tutorial on converting code from VB6 to VB.Net. It covers changes in variable declaration, data types, user defined types, option explicit/strict, string manipulation commands, and examples of code for common tasks like displaying message boxes, working with arrays and files. The tutorial is aimed at VB.Net beginners to help them understand major differences introduced in the new version.

Uploaded by

nathulalusa
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
142 views17 pages

Beginners Tutorial: Issue Status/Number - 1.0 Issue Date - 10 November, 2002

This document provides a tutorial on converting code from VB6 to VB.Net. It covers changes in variable declaration, data types, user defined types, option explicit/strict, string manipulation commands, and examples of code for common tasks like displaying message boxes, working with arrays and files. The tutorial is aimed at VB.Net beginners to help them understand major differences introduced in the new version.

Uploaded by

nathulalusa
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

VB6 to VB.

Net Tutorial

VB6 to [Link]
Beginners Tutorial

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

VB6 to [Link] Tutorial Version 1.0


Page 1

VB6 to [Link] Tutorial

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

VB6 to [Link] Tutorial Version 1.0


Page 2

VB6 to [Link] Tutorial

Introduction
Purpose
This tutorial is intended to show some of the differences [Link] has introduced over
VB6.

Scope
This tutorial is aimed at the [Link] beginner. It will show some of the major
differences [Link] 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 [Link] Tutorial Version 1.0


Page 3

VB6 to [Link] Tutorial

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

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

-2,147,483,648 -> 2,147,483,647

Char

16-Bit

0 -> 65,535

String

16-Bit

0 -> Approx 2 Billion Characters

Boolean

16-Bit

True or False

Date

64-Bit

Jan 1, 0001 -> Dec 31 9999

Object

32-Bit

All Types

The Variant data type is no longer supported in [Link]. 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 [Link] Tutorial Version 1.0


Page 4

VB6 to [Link] Tutorial

User Defined Types (UDTs)


UDTs 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 [Link]
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

[Link]

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 shouldnt) 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 [Link]. 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 [Link] Tutorial Version 1.0


Page 5

VB6 to [Link] Tutorial

String Manipulation Commands


The standard string manipulation commands have now changed in [Link]. Below is a
table mapping the older VB6 methods to the new [Link] methods.

VB6 Method

[Link]
Method

Description

UCase

ToUpper

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

&

& / Concat

Concatenate Strings

Not
Implemented

StrComp

Compare two string

Not
Implemented

Insert

Add characters in the


middle of a string

Not
Implemented

Remove

Remove characters from


the middle of a string

[Link] Example
Dim sString1, sString2 As String
sString1 = "this is a test"
sString2 = [Link]
Dim sString1, sString2 As String
sString1 = "this is a test"
sString2 = [Link]
Dim sString1, sString2 As String
sString1 = "this is a test"
sString2 = [Link](5, 2)
Dim sString1 As String
Dim iLength As Short
sString1 = "this is a test"
iLength = [Link]
Dim sString1 As String
Dim iPosition As Short
sString1 = "this is a test"
iPosition = [Link]("e")
Dim sString1 As String
sString1 = [Link](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,
[Link]))
Dim sString1, sString2 As String
sString1 = "this a test"
sString2 = [Link](5, "is ")
Dim sString1, sString2 As String
sString1 = "this is a test"
sString2 = [Link](5, 3)

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

VB6 to [Link] Tutorial Version 1.0


Page 6

VB6 to [Link] Tutorial

Coding Examples and Comparison


The following section will give examples of VB6 and the corresponding [Link] code
to perform the same or similar function.

Retrieving the Application Path


Retrieve the directory name the executable has been executed from.

[Link]

VB6

IO. [Link] ([Link] )

[Link]

Passing time back to the CPU


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

DoEvents

VB6

[Link]()

[Link]

VB6 to [Link] Tutorial Version 1.0


Page 7

VB6 to [Link] Tutorial

Displaying Message Boxs 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

[Link]
Select Case MsgBox("Please Press
Yes or No",
[Link] +
[Link], "Make a
Selection")
Case vbNo
MsgBox("No Pressed")
Case vbYes
MsgBox("Yes Pressed")

VB6 to [Link] Tutorial Version 1.0


Page 8

Note: The
[Link]
ow Function
can be used
instead of the
MsgBox
Function

VB6 to [Link] Tutorial

Working with Arrays


This section will cover some of the primary array manipulation techniques available in
[Link]. Many array techniques such as declaration and cycling through array
elements are exactly the same as VB6 and so wont 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

[Link]

VB6 to [Link] Tutorial Version 1.0


Page 9

VB6 to [Link] Tutorial

Basic array sorting


VB6
This example from [Link] (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 [Link] Tutorial Version 1.0


Page 10

VB6 to [Link] Tutorial

[Link]
Dim MyArray(4) As String
MyArray(0) = "A"
MyArray(1) = "D"
MyArray(2) = "E"
MyArray(3) = "B"
MyArray(4) = "C"
[Link](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"
[Link](MyArray)
[Link](MyArray)

VB6 to [Link] Tutorial Version 1.0


Page 11

Note: In the
above example
array is sorted EA

VB6 to [Link] Tutorial

Text File Manipulation


The following section will show the process of reading and writing to basic text files in
[Link]. 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 [Link] examples
require the Imports [Link] statement at the top of the form.

Reading Text Files


VB6 Read Line by Line
Dim sLine As String
Open "C:\[Link]" For Input As #1
Do Until EOF(1)
Line Input #1, sLine
Loop
Close #1

[Link] Read Line by Line


Dim sr As StreamReader =
[Link]("c:\[Link]")
Dim sLine As String
Do
sLine = [Link]()
Loop Until sLine = Nothing
[Link]()

Dim sr As StreamReader = [Link]("c:\[Link]")


Dim sAllText As String
sAllText = [Link]()
[Link]()

[Link] Read All

Dim sAllText As String


Open "C:\[Link]" For Input As #1
sAllText = Input(LOF(1), #1)
Close #1

VB6 Read All

VB6 to [Link] Tutorial Version 1.0


Page 12

VB6 to [Link] Tutorial

Writing/Creating Text Files


VB6
Open "C:\[Link]" For Output As #1
Print #1, "Line1"
Print #1, "Line2"
Print #1, "Line3"
Close #1

VB6 to [Link] Tutorial Version 1.0


Page 13

[Link]
Dim fs As FileStream =
[Link]("C: \[Link]",
[Link],
[Link])
Dim sr As New StreamWriter(fs)
[Link]("Line1")
[Link]("Line2")
[Link]("Lin e3")
[Link]()

VB6 to [Link] Tutorial

Checking if a File Exists


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

[Link]
If [Link]("C: \[Link]") Then
[Link]("File Found!")
Else
[Link]("File Not
Found!")
End If

catch errors and


when trying to load

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

VB
On Error GoT o ErrorHandler

[Link]

[Link] =
LoadPicture("c: \[Link]")
Try
ErrorHandler:
MsgBox("Error Loading File!")

VB6 to [Link] Tutorial Version 1.0


Page 14

[Link] = [Link]("c:\[Link]")
Catch
MsgBox("Error Loading File!")
End Try

VB6 to [Link] Tutorial

Basic Mathematic Functions


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

Dim dblResult As Double


dblResult = Sqrt(64)

VB6 to [Link] Tutorial Version 1.0


Page 15

VB6 to [Link] Tutorial

Returning Values from Functions


[Link] 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 [Link]
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

[Link]

VB6 to [Link] Tutorial Version 1.0


Page 16

VB6 to [Link] Tutorial

Working with the Windows Registry


[Link] still supports the GetSetting and SaveSetting commands from VB6, but also
allow unrestricted access to the registry via the [Link] class.
Below is an example of how to read and write to the registry in [Link]. VB examples
will not be included due to length.

Reading the Registry


[Link]
Dim oReg As [Link]
Dim oRegKey As [Link]
Dim sValue As String
oRegKey = [Link]("Software\Microsoft \Windows NT \CurrentVersion", False)
sValue = [Link]("CurrentVersion", vbNullString)

Writing to the Registry


[Link]
Dim oReg As [Link]
Dim oRegKey As [Link]
oRegKey = [Link]("Key \SubKey", True )
[Link]("Entry", "NewValue")

Creating New Registry Keys

Dim oReg As [Link]


Dim oRegKey As [Link]
oRegKey = [Link]("Key\Subkey\NewKey")

[Link]

VB6 to [Link] Tutorial Version 1.0


Page 17

You might also like