[VB6] VB6 vs VB.net

VB6 vs VB.net
Data Type

VB6 VB.NET Comments
Integer Short 16 bits
Long Integer 32 bits
N/A Long 64 bits
Variant N/A Use the new ‘Object’ data type
Currency N/A Use Decimal in VB6 or Decimal or Long in VB.NET
N/A Decimal Available* in VB6. Native in VB.NET
String String VB.NET doesn’t support fixed length strings

– VB.net does not support Variant and Currency data type so use Object data type
– For currency, VB.net uses decimal data type and VB can also use the decimal data type. VB can’t declare a variable as decimal but can declare it as variant and then use cDec function to make its subtype decimal

Passing Parameters
– VB is using 2 methods: pass by reference and pass by value
– VB.net is using pass by value

Variable Declaration
– Always declare a variable in separate lines.
– Eg:
Dim lngOne As Long
Dim lngTwo As Long

If declares like this,
Dim lngOne, lngTwo As Long

declares lngOne as a Variant (which is not supported in .NET) and lngTwo as a Long.

New Commands and Keywords
– VB.NET introduces many new keywords

Array
– VB, array(5) gives index 0 to index 5
– VB.net, array(5) gives index 0 to index 4

Default Properties
– VB allows referencing default properties without specifying the name of property
– VB.net does not allow it

Let and Get Properties
– VB.Net no longer has Let properties

Data Access Method
– VB.net, all bound controls work with ADO only

Windows Forms
– VB.net is using windows forms replacing the VB6 Forms

More details, http://www.thescarms.com/vbasic/VB6vsVBNet.aspx

[VB6] COM

Component Object Model
– Self-contained units of code that provide functionality
– It is widely accepted interoperability standard which explain the ways the memory should be organised for all object
– .NET components provide a programmable interface that is accessed by client application. Component interface consists a number of properties, events and methods that provide functionality.

All objects expose to important attributes:
– properties
– methods
– events

[VB6] Use of Property

Here’s an example of using property to pass a value. It’s written using VB 6.0. The value is taken from a class and passes to a form.

Declare a variable at the form.
> Private mTableName As Variant

Then, add a Get and a Let property in the same form.
> Public Property Get baseTable() As Variant
baseTable = mTableName
End Property

Public Property Let baseTable(ByVal vNewValue As Variant)
mTableName = vNewValue
End Property

To pass a value to a class. At class, I dim an object first,
Dim oOrderForm As SqMeterPriceFrm
Then,
oOrderForm.baseTable = sTableName
and set the object to the class,
Set oOrderForm = New SqMeterPriceFrm

Notes:
3 requirements to create a property in a code module:
1) private variable to hold the property value
2) Property Let procedure for setting property value (write-only).  Property Set procedure if variable will hold an object reference
3) Property Get retrieves property value (read-only)

The Get and Let property must have same name
Eg: baseTable (as shown above)


[VB6] Class

After learning a bit from the previous post on the topic of Class. I give a try to call a class from another class through Function call.

From my trials and errors, I get the following working codes.

Form1
Option Explicit
Private msFormat As String

Public Property Let DateFormat(ByVal value As String)
msFormat = value
End Property

Private Sub Command1_Click()
Dim oSpecDate1 As cSpecialDate
Set oSpecDate1 = New cSpecialDate

Call oSpecDate1.GetfromClass
End Sub

1st Class – cSpecialDate
Public Function GetfromClass() As String
Dim oGetClass As Class1
Set oGetClass = New Class1

MsgBox “Message 1: ” & oGetClass.GetSpecialDate
MsgBox “Message 2: ” & oGetClass.GetSpecialDate
End Function

2nd Class – Class1
Public Function GetSpecialDate() As String
Dim msFormat As String

msFormat = “yy-mm-dd”
GetSpecialDate = Format(Now, msFormat)
End Function

[VB6] Different Between Class and Module

As per title, I get this from the following source: http://en.allexperts.com/q/Visual-Basic-1048/class-module.htm

– A class requires you to create an instance of it before it’s use and module does not.
– Technically they are both classes.
– When you create a module, you are in essence creating a class. When your code runs, VB creates an instance of the module and makes it globally available. So, once your project is running, any public methods and/or variables in the module are available to your application.
– You can declare a variable as an instance of your class and then the public methods and variables of that class are available.

Examples:
Create a function that returns the date in a format custom for your application. Can be done in 2 methods:

1) Create a module and add a function.
Public Function GetSpecialDate() As String
GetSpecialDate = Format(Now, “yy-mm-dd”)
End Function

Call GetSpecialDate from anywhere in your application to get your date.
Private Sub Command1_Click()
MsgBox GetSpecialDate
End Sub

2) If you want to add this function into a class called cSpecialDate,
Private Sub Command1_Click()
Dim oSpecDate As cSpecialDate
Set oSpecDate = New cSpecialDate

MsgBox oSpecDate.GetSpecialDate
End Sub

where oSpecDate is an object for the cSpecialDate class and set the object.

A class allows you to create multiple instances of it. To show you how this may be used, let’s add property to our class that will affect the format of the date.

Option Explicit
Private msFormat As String

Private Sub Class_Initialize()
msFormat = “yy-mm-dd”
End Sub

Public Function GetSpecialDate() As String
GetSpecialDate = Format(Now, msFormat)
End Function

Public Property Let DateFormat(ByVal value As String) //write-only
msFormat = value
End Property

Private Sub Command1_Click()
Dim oSpecDate1 As cSpecialDate
Dim oSpecDate2 As cSpecialDate
Set oSpecDate1 = New cSpecialDate
Set oSpecDate2 = New cSpecialDate

MsgBox oSpecDate1.GetSpecialDate

//oSpecDate2.DateFormat = “yyyy-mmm-dd” //Don’t have DateFormat in VB6
MsgBox oSpecDate2.GetSpecialDate
End Sub