Is VBA Class have Constructor like java ?

M

moonhk

Is VBA Class have Constructor like java ? I want New a clsStatement run
Init_ME() or other Init_xxx ? Is it possible ?


Option Explicit

Public StatementContent As New clsStatement

Sub Start()
With StatementContent
StatementContent.FileNameSource = "g:\ass\ass_statement.txt"
.Read_file
End With

End Sub


'~~ Class Modules

Option Explicit
Public FileNameSource As String ' Statement file name
Const TEMPLATE_DIRECTORY = "g:\ass\"
Const TEMPLATE_NAME = "eStatement_template.xls"
Const STATEMENT_DIR = "g:\ass\Statements"
Private Debtor_FN As String
Private Details(1024) As String
Private Debtor_nbr_pos As Long
Private Debtor_mail_pos As Long

Public Sub Init_ME()

End Sub

Private Sub Init_pos()
Debtor_nbr_pos = 0
Debtor_mail_pos = 0
Debtor_FN = ""
End Sub


Public Sub Build_file()
Dim cnt As Long
Dim Act As Long
Dim loSheet As Variant

'MsgBox VBA.Str(Debtor_nbr_pos)

'~~ Open template file
Workbooks.Open FileName:=TEMPLATE_DIRECTORY + "\" + TEMPLATE_NAME
Workbooks(TEMPLATE_NAME).Activate
Application.Workbooks(TEMPLATE_NAME).SaveAs STATEMENT_DIR + "\" +
Debtor_FN

' Workbooks("new.xls").Activate
Set loSheet = Application.Workbooks(Debtor_FN).Sheets("Statement")
Act = 6
For cnt = Debtor_nbr_pos To Debtor_mail_pos
loSheet.Cells(Act, 1).Value = Details(cnt)
Act = Act + 1
Next
Application.Workbooks(Debtor_FN).Save
Application.Workbooks(Debtor_FN).Close
Call Init_pos
End Sub

Public Sub Read_file()
Dim fileNumber As Long
Dim cnt As Long
Dim inputVal As String

Call Init_pos

fileNumber = FreeFile ' get unused file number
Application.StatusBar = "Reading ...." & FileNameSource


Open FileNameSource For Input As #fileNumber
Do While Not EOF(fileNumber)
Line Input #fileNumber, inputVal
cnt = cnt + 1
Details(cnt) = inputVal
If VBA.Left(inputVal, 10) = "Debtor nr:" Then
' MsgBox inputVal + " " + VBA.Str(cnt)
Debtor_nbr_pos = cnt
Debtor_FN = VBA.Trim(VBA.Mid(inputVal, 11,
VBA.Len(VBA.Trim(inputVal)) - 10) + ".xls")
'MsgBox Debtor_FN
End If
If VBA.Left(inputVal, 8) = "E-mail :" Then
'MsgBox inputVal + " " + VBA.Str(cnt)
Debtor_mail_pos = cnt
End If
If Debtor_mail_pos <> 0 Then
Call Build_file
End If
Loop
Close #fileNumber
MsgBox cnt

End Sub
 
J

Jim Thomlinson

In VBA there is an event generated when a class is initialized. This would be
similar to the constructor in Java. In the Class just above the code window
There is a drop down with (General) in it. Switch that to Class. It will by
default drop in a sub for the initialize event. other events are listed to
the right of that drop down...

One thing to not is that your declaration has the work new in it. That is a
very inefficient way of instantiating objects. check out this link (about 2/3
the way down)...

http://www.cpearson.com/excel/variables.htm
 
M

moonhk

Hi Jim
I want new class want define some constructor like java
I will change Public StatementContent As New clsStatement to

Public StatementContent As clsStatement
set StatementContent = new clsStatement

You provided link can not provide some information to me about
constructor like java.
 
N

NickHK

If I understand you correctly, then no, there is no such thing.
As Jim pointed, there is the "Class_Initialize()" routine that is run when
an instance is created, but you cannot call it and/or pass arguments
The closest you can get is write you own Init routine and call that after
instantiation. But it is the developer's responsibility to call before any
other properties/methods etc.

<MyClass>
Public Function Init (arg1 as string, arg2 as long) as long
'custom adjustment using the passed arguments
end function

Private Sub Class_Initialize()
'Any general creation code that is always the same
End Sub
</MyClass>

<Worksheet>
Dim Inst as myclass
set inst=new myclass 'Class_Initialize code executed
inst.Init "Somestring,1000 'Init code executed
</Worksheet>

NickHK
 
M

moonhk

Thank.
If I understand you correctly, then no, there is no such thing.
As Jim pointed, there is the "Class_Initialize()" routine that is run when
an instance is created, but you cannot call it and/or pass arguments
The closest you can get is write you own Init routine and call that after
instantiation. But it is the developer's responsibility to call before any
other properties/methods etc.

<MyClass>
Public Function Init (arg1 as string, arg2 as long) as long
'custom adjustment using the passed arguments
end function

Private Sub Class_Initialize()
'Any general creation code that is always the same
End Sub
</MyClass>

<Worksheet>
Dim Inst as myclass
set inst=new myclass 'Class_Initialize code executed
inst.Init "Somestring,1000 'Init code executed
</Worksheet>

NickHK
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top