Results 1 to 4 of 4
  1. #1
    5 Star Lounger
    Join Date
    Mar 2001
    Posts
    989
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hello.
    I'm creating a class (in Excel 2003) to represent an employee.

    How can I create a method that returns a value? For example, I have a SalaryChange(value) method but I would like it to return
    the new salary.
    Is it possible to create a 'hierarchy' - that is a sub-class such as an Account? Thank you, Andy.

  2. #2
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You could do it like this:

    Class clsEmployee
    Code:
    Option Explicit
    
    Private mdSalary As Double
    
    Public Function SalaryChange(dAddition As Double) As Double
        Salary = Salary + dAddition
        SalaryChange = Salary
    End Function
    
    Public Property Get Salary() As Double
        Salary = mdSalary
    End Property
    
    Public Property Let Salary(ByVal dSalary As Double)
        mdSalary = dSalary
    End Property
    In a normal module (or anywhere else you might use the employee class)

    Code:
    Option Explicit
    
    Sub Demo()
        Dim CEmployee As clsEmployee
        Set CEmployee = New clsEmployee
        With CEmployee
            .Salary = 100
            MsgBox "New salary equals: " & .SalaryChange(20)
        End With
    End Sub
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

  3. #3
    5 Star Lounger
    Join Date
    Mar 2001
    Posts
    989
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you very much.

    So a Public Sub is a method, but for a method to return a value it must be a Public Function? Is there any point in using Public Sub
    to represent a method, when I could use a function and just not return a value?

    My second question was - is it possible (with VBA) for a class to have another class as a member? Andy.

  4. #4
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts
    Equally valid question - is there any point in using a function and not returning a value? Makes no odds, really.
    And yes, you can simply declare a variable in one class as another class.
    Regards,
    Rory

    Microsoft MVP - Excel

Posting Permissions

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