Embed
Email

VB6 to VB Net Beginners Tutorial

Document Sample
VB6 to VB Net Beginners Tutorial
Description

VB6 to VB Net Beginners Tutorial,This tutorial is intended to show some of the differences VB.Net has introduced over VB6. 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. Table of Contents : Introduction, Purpose, Scope, References, Variable Declaration, User-defined Types (UDT'S), Option Explicit/Option Strict, String Manipulation Commands,

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 Size Boundaries

Byte 8-Bit 0-255

Short 16-Bit -32,768 -> 32767

Single 32-Bit F/P -3.4028235E38 -> 3.4028235E38

-9,223,372,036,854,775,808 ->

Long 64-Bit

9,223,372,036,854,775,807

-1.79769313486231E308 ->

Double 64-Bit F/P

1.79769313486231E308

Decimal 128-Bit +/- 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 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.





VB.Net

VB6 Method Description VB.Net Example

Method

Dim sString1, sString2 As String

UCase ToUpper Convert to Uppercase sString1 = "this is a test"

sString2 = sString1.ToUpper



Dim sString1, sString2 As String

LCase ToLower Convert to Lowercase sString1 = "this is a test"

sString2 = sString1.ToLower

Dim sString1, sString2 As String

Mid SubString Return a part of a string sString1 = "this is a test"

sString2 = sString1.SubString(5, 2)

Dim sString1 As String

Dim iLength As Short

Len Length Get the length of a string

sString1 = "this is a test"

iLength = sString1.Length

Dim sString1 As String

Give the index of a Dim iPosition As Short

Instr IndexOf

substring sString1 = "this is a test"

iPosition = sString1.IndexOf("e")

Dim sString1 As String

& & / Concat Concatenate Strings

sString1 = String.Concat(“This”, “ is a “, “test”)

Dim sString1, sString2 As String

Dim bMatch As Boolean

Not sString1 = "this is a test"

StrComp Compare two string

Implemented sString2 = "this is A test"

bMatch = Not CBool(StrComp(sString1, sString2,

CompareMethod.Binary))

Dim sString1, sString2 As String

Not Add characters in the

Insert sString1 = "this a test"

Implemented middle of a string

sString2 = sString1.Insert(5, "is ")

Dim sString1, sString2 As String

Not Remove characters from

Remove sString1 = "this is a test"

Implemented the middle of a string

sString2 = sString1.Remove(5, 3)

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") VB.Net

Case vbNo Select Case MsgBox("Please Press Note: The

Yes or No", MessageBox.Sh

MsgBox "No Pressed" MsgBoxStyle.Information +

MsgBoxStyle.YesNo, "Make a ow Function

Case vbYes Selection") can be used

MsgBox "Yes Pressed" instead of the

Case vbNo

MsgBox

End Select MsgBox("No Pressed") Function

Case vbYes



MsgBox("Yes Pressed")









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) = "1"

MyArray(1) = "5"

MyArray(2) = "2"

MyArray(3) = "4"

MyArray(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 Note: In the above example array is sorted A-E

MyArray(0) = "A" Dim MyArray(4) As String Note: In the

MyArray(1) = "D"

MyArray(2) = "E"

above example

MyArray(0) = "A"

MyArray(3) = "B" MyArray(1) = "D" array is sorted E-

MyArray(4) = "C" MyArray(2) = "E" A

MyArray(3) = "B"

Array.Sort(MyArray) MyArray(4) = "C"



Array.Sort(MyArray)

Array.Reverse(MyArray)









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) VB.Net Read Line by Line

Line Input #1, sLine

Loop Dim sr As StreamReader =

Close #1 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" VB.Net

Print #1, "Line2"

Print #1, "Line3" Dim fs As FileStream =

Close #1 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!") VB.Net

Else

MsgBox("File Not Found!") If File.Exists("C: \File.txt") Then

End If MessageBox.Show("File Found!")

Else

MessageBox.Show("File Not

Error Handling

Found!")

End If The following

section shows

how to use the

VB.Net Try

command to

catch errors and display a prompt to the user

when trying to load a picture in a picture box.

VB



On Error GoT o ErrorHandler



Picture1.Picture = VB.Net

LoadPicture("c: \file.bmp")

Try

ErrorHandler: PictureBox1.Image = System.Drawing.Bitmap.FromFile("c:\File.bmp")

Catch

MsgBox("Error Loading File!") 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


Related docs
Other docs by sehrish ishaq ...
Make Money With Google AdSense
Views: 48  |  Downloads: 4
VB6 to VB Net Beginners Tutorial
Views: 14  |  Downloads: 0
D. Hanson - Android 3D TTT
Views: 5  |  Downloads: 0
Forex Trading Guide and Tutorial
Views: 19  |  Downloads: 0
Sewing Machine Maintenance How To
Views: 12  |  Downloads: 0
Epson Printer Print Quality Guide
Views: 6  |  Downloads: 0
Basic Skills For HTML
Views: 5  |  Downloads: 0
Automotive Guide Opel
Views: 3  |  Downloads: 0
NEAMB_Educational_Travel_Guide.
Views: 0  |  Downloads: 0
By registering with docstoc.com you agree to our
privacy policy

You are almost ready to download!

You are almost ready to download!