Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2003
    Posts
    233

    Unanswered: my 1st class what am i doing wrong?

    calling code:
    Sub test()
    Dim j As New JournalEntry ' how do u do a class constructor?
    j = j.Init(100, -11, "10000", Asset)

    End Sub


    Class JournalEntry


    Private mDebit As Integer
    Private mCredit As Integer
    Private mAcctNo As String
    Private mAcctType As AcctType

    Public Enum AcctType
    Asset = 1
    Liability = 2
    Equity = 3
    Income = 4
    Exp = 5
    End Enum

    Public Function Init(dr As Integer, cr As Integer, AcctNo As String, AccountType As AcctType) As JournalEntry
    Set je = New JournalEntry
    With je
    .mDebit = dr 'HERE i get errr "Object doesnt support this prop or method"
    .mCredit = cr
    .mAcctNo = AcctNo
    .mAcctType = AccountType
    End With

    End Function

  2. #2
    Join Date
    Sep 2008
    Location
    London, UK
    Posts
    511
    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...
    Last edited by Colin Legg; 05-19-09 at 15:34.

  3. #3
    Join Date
    Oct 2003
    Posts
    233
    Thanks for the help,, I have some what of a C# back ground, but simple macro experiance, i have never done much with classes or even enums in vba

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •