If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Excel > my 1st class what am i doing wrong?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-19-09, 12:52
mikezx10 mikezx10 is offline
Registered User
 
Join Date: Oct 2003
Posts: 226
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
Reply With Quote
  #2 (permalink)  
Old 05-19-09, 14:24
Colin Legg Colin Legg is offline
Registered User
 
Join Date: Sep 2008
Location: London, UK
Posts: 495
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 14:34.
Reply With Quote
  #3 (permalink)  
Old 05-19-09, 15:42
mikezx10 mikezx10 is offline
Registered User
 
Join Date: Oct 2003
Posts: 226
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
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On