VB6 - VB.NET Syntax Conversions
(Part 1)
If you're one of those guys/girls who have that old VB6 code just lying around and would like to migrate over to vb.net without rewriting everything from scratch... then this article should (could) help...
Well written code converts easy
First thing in any conversion (even in converting borland C to VC++ or similiar) is to clean up and make your code as functional and "simple" as possble"
Before converting , you should use a "clean up" concept and be sure to
- Notate the important functionality s of your code using 'Your notes(this will allow you to find code that may have experienced a significant change)
- Declare every single variable.
- make sure all variables have the explicit type (as opposed to variants.)
- Check timers, be sure to set timer1.enabled = false (as opposed to the alternative in vb6 which allowed you to set it to 0 )
- Convert DAO and RDO to ADO.
Consider the following a reference guide, somewhere you can come when you need to migrate your
VB6 project to
VB.NET
Errors
(check out my errors article)
Handle Your Errors
In
VB6
On Error Goto errhandler
__________(code..yada...yada...)
errhandler:
MsgBox(err.Description)
In
VB.NET
Try
Throw New Exception("error description")
Catch e as Exception
MsgBox(e.Description)
End Try
Events
Handling an event
In
VB.NET,you can us the new keyword AddHandler.
AddHandler object.event, AddressOf procedure
DoEvents
VB6
DoEvents
VB.NET
System.Windows.Forms.Application.DoEvents
App Object
Full Filepath
VB6
App.Path & App.EXEName
VB.NET
System.Reflection.Assembly.GetExecutingAssembly.Lo cation.ToString
App instance
VB6
App.hInstance
VB.NET
System.Runtime.InteropServices.Marshal.GetHINSTANC E _(System.Reflection.Assembly.GetExecutingAssembly. GetModules() _(0)).ToInt32()
Previous Application Instance
VB6
App.PrevInstance
VB.NET
Function PrevInstance() As Boolean
If Ubound(Diagnostics.Process.GetProcessesByName(Diag nostics.Process.GetCurrentProcess).ProcessName)) > 0 Then
Return True
Else
Return False
End If
End Sub
Graphics
Load Picture
VB6
Picture1.Picture = LoadPicture(path)
VB.NET
Dim imgf As Image = Image.FromFile(path)
Picture1.Image = imgf
Load icons
VB6
Me.Icon = LoadPicture(path)
VB.NET
Dim icof As New Icon(path)
Me.Icon = icof
File I/0
Read File
VB6
Open path For Input As #1
Line Input #1, buffer
Close #1
VB.NET
Dim fst As FileStream = File.Open(path, FileMode.OpenOrCreate, _ FileAccess.Read)
Dim srf As New StreamReader(fst)
Buffer = srf.ReadLine
srf.Close
Write in a file
VB6
Open path For Output As #1
Write #1, buffer
Close #1
VB.NET
Dim fst As FileStream = File.Open(path, FileMode.OpenOrCreate, _
FileAccess.Write)
Dim srw As New StreamWriter(fst)
srw.Write(buffer)
srw.Close
Note: This again is a reference guide, so if your new to vb.net and used vb6 in the past and are looking to make the transistion, start here.
Hope this helps