Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

default class constructor 2

Status
Not open for further replies.

mgriffith

MIS
Joined
Jul 3, 2001
Messages
177
Location
US
is there anything like a default contructor from c++ available in vb6? i thought i saw somethign like a sub called class_instantiate that automatically gets called whenever a class is created, but i can't find it.

thanks. mike griffith
----------------------------
mgriffith@lauren.com
mdg12@po.cwru.edu
 
Yeah - when playing with the cls file, set the left most combo to "Class", and you'll find an event in the right hand combo called "Initialize"

If you're from a C++ background, take note that VB does not support function overloading on the basis of function parameters.

mmilan


 
i can't find a way to use parameters with the class_initalize event..any suggestions? i think i'm just going to create a setup subroutine inside the class so you'd instantiate a class like

dim x as new c
x.setup(param1, param2)
... mike griffith
----------------------------
mgriffith@lauren.com
mdg12@po.cwru.edu
 
I do the following

In the class:

Friend Sub AssignPort(ByRef SerialPort As MSComm)
Set Me.SerialPort = SerialPort
Me.CommPort = SerialPort.CommPort
Me.Settings = SerialPort.Settings
Me.Handshaking = SerialPort.Handshaking
End Sub

Private Sub Class_Initialize()
m_strBaudRate = "1200"
m_strParity = "n"
m_strDataBits = "7"
m_strStopBits = "1"
End Sub

In the module:

'External Constructor Method For SerialPort Class
Public Function ReferenceMSComm(comPort As MSComm) As SerialPort
Set ReferenceMSComm = New SerialPort
ReferenceMSComm.AssignPort comPort
End Function


Not a true constructor but it is the best method I have come up with. Hope this helps.
If you choose to battle wits with the witless be prepared to lose.
[machinegun][hammer]
 
>can't find a way to use parameters with the
>class_initalize event

Quite so. It's one of the vaguely irritating limitations to VB classes

>any suggestions?

VB.NET addresses this issue.

With VB 6, I tend to use a factory class to instantiate and return objects that require parameters in their initialisation.

An example:
Factory Class:
[tt]
Option Explicit

Public Function BuildWidget(Width As Long, Height As Long) As Widget
Set BuildWidget = New Widget
BuildWidget.WidgetWidth = Width
BuildWidget.WidgetHeight = Height
End Function
[/tt]
Widget Class (generally I would set the instancing property of this to PublicNotCreatable):
[tt]
Option Explicit

Private mvarWidgetWidth As Long
Private mvarWidgetHeight As Long

Public Property Get WidgetArea() As Long
WidgetArea = mvarWidgetWidth * mvarWidgetHeight
End Property

Public Property Let WidgetHeight(ByVal vData As Long)
mvarWidgetHeight = vData
End Property

Public Property Get WidgetHeight() As Long
WidgetHeight = mvarWidgetHeight
End Property

Public Property Let WidgetWidth(ByVal vData As Long)
mvarWidgetWidth = vData
End Property

Public Property Get WidgetWidth() As Long
WidgetWidth = mvarWidgetWidth
End Property
[/tt]
And an example of it's use:
[tt]
Option Explicit

Private Sub Command1_Click()
Dim myFactory As Factory
Dim myWidget As Widget

Set myFactory = New Factory
Set myWidget = myFactory.BuildWidget(5, 10)

MsgBox myWidget.WidgetArea
End Sub
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top