Hi Mike,
Unlike say
VB.Net, VBA doesn't support constructors. The closest you can get to a constructor is using the class's initialize event handler:
Code:
Private Sub Class_Initialize()
End Sub
However, you cannot pass arguments into the event handler so it's use is limited in comparison to constuctors. Since I'm thinking you are coming from a .Net background (?) I'll also point out that VBA does not support overloading either.
Another important point is on this line here:
Code:
Dim j As New JournalEntry
Whilst in
VB .Net (2005 and later) this would be perfectly acceptable, in VBA it is considered a bad practice to auto-instantiate your JournalEntry object.
I think, all in all, you are looking for something along the lines of this:
Standard code module
Code:
Option Explicit
Sub test()
Dim j As JournalEntry
Set j = New JournalEntry
j.Init 100, -11, "10000", Asset
Debug.Print j.Debit
Debug.Print j.Credit
Debug.Print j.AcctNo
Debug.Print j.AccountType
End Sub
JournalEntry class module
Code:
Option Explicit
'class fields
Private mDebit As Integer
Private mCredit As Integer
Private mAcctNo As String
Private mAcctType As AcctType
'''''''''''''''''''''''''''''''''''''''''
'class interface
Public Enum AcctType
Asset = 1
Liability = 2
Equity = 3
Income = 4
Exp = 5
End Enum
'methods
Public Sub Init(dr As Integer, cr As Integer, AcctNo As String, AccountType As AcctType)
mDebit = dr
mCredit = cr
mAcctNo = AcctNo
mAcctType = AccountType
End Sub
'read-only properties
Public Property Get Debit() As Integer
Debit = mDebit
End Property
Public Property Get Credit() As Integer
Credit = mCredit
End Property
Public Property Get AcctNo() As String
AcctNo = mAcctNo
End Property
Public Property Get AccountType() As AcctType
AccountType = mAcctType
End Property
MS Excel MVP Chip Pearson has put together a nice introduction to classes in VBA on his website; well worth a read:
http://www.cpearson.com/excel/Classes.aspx
Hope that helps...