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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Class_Initialize Problem!

Status
Not open for further replies.

cmlimnet

Programmer
Mar 7, 2002
29
IN
Dear All,

I have wrote a class and I want to pass in parameters in the class_initialize() like below:
Private Sub Class_Initialize(ByVal Name As String)
strUser = Name
End Sub
When I compile it to dll, visual basic prompt error.
Can we actually pass in parameters into class_initialize?

Thank you.

Best Regards,
cmlimnet
 
You can not "pass-in" arguments to Class_Initialize because it is an event, not a method. You have to define Name as a property or define a method for setting it. VB.Net however does have an overloaded New method for which you may define parameters. Forms/Controls Resizing/Tabbing Control
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
One way to work around this is to create a construtor method for the class, something like this.

In a module

'External Constructor Method For CEmployeeRecordsets Class
Public Function EmployeeDatabase() As CEmployeeRecordsets
Set EmployeeDatabase = New CEmployeeRecordsets
EmployeeDatabase.StationName = g_strStationName 'This acts like a passed variable to the class Initialize
If Not EmployeeDatabase.ConnectDatabase(c_sConnection) Then
MsgBox "Database connection failed at the EmployeeDatabase Constructor.", vbInformation, _
"CTI Office Management System Failure !"
End If
End Function


In the class

Private Sub Class_Initialize()
Set m_hDB = New ADODB.Connection
Set m_rsEmployees = New ADODB.Recordset
Set m_rs = New ADODB.Recordset
End Sub

Friend Function ConnectDatabase(ByVal sConnectionString As String) As Boolean

On Error GoTo ErrHnd

m_hDB.ConnectionString = sConnectionString 'Open The Connection
m_hDB.ConnectionTimeout = 5
m_hDB.Mode = adModeRead
m_hDB.Open
m_hDB.Close
GetPeriodData

ConnectDatabase = True
Exit Function

ErrHnd:
MsgBox Err.Number & " " & Err.Description & " Error Generated By " & Err.Source, vbCritical, "Class Error Trap !"
ConnectDatabase = False
End Function

Property Get StationName() As String
StationName = m_strStationName
End Property

Property Let StationName(ByVal sNew As String)
m_strStationName = sNew
End Property


From the form call the constructor

Private Sub Form_Load()

On Error GoTo ErrHnd

Set m_EmpDB = EmployeeDatabase 'Establish DB connection w/Class Initializtion

Exit Sub

ErrHnd:
Form_ErrorHandler "Form Load Event"
Resume Next
End Sub

Hope this helps. If you choose to battle wits with the witless be prepared to lose.
[machinegun][hammer]

I don't work for Stars, but I can't even get a "That a Boy" from my boss [cheers]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top