Results 1 to 3 of 3

Thread: [RESOLVED] Excel VBA Class module

  1. #1

    Thread Starter
    Fanatic Member VBAhack's Avatar
    Join Date
    Dec 2004
    Location
    Sector 000
    Posts
    617

    Resolved [RESOLVED] Excel VBA Class module

    Can someone tell me why the variable i is not set in the below example? Are Class methods unable to modify the values of input variables?

    In normal code module:
    Code:
    Sub test_SimpleClass()
        Dim i As Long
        Dim s As SimpleClass
        
        Set s = New SimpleClass
        s.testx (i)
        Debug.Print "test_SimpleClass:  i=" & i
        Set s = Nothing
        
        'output:
        'SimpleClass: constructor does nothing...
        'SimpleClass: testx()  a=345
        'test_SimpleClass:  i = 0
    End Sub
    In a Class Module called SimpleClass:
    Code:
    Option Explicit
    Private Const ID As String = "SimpleClass"
    
    Private Sub Class_Initialize()
        Debug.Print ID & ": constructor does nothing..."
    End Sub
    
    Public Sub testx(ByRef a As Long)
        a = 345
        Debug.Print ID & ": testx()  a=" & a
    End Sub

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel VBA Class module

    you are not calling correctly

    Code:
    s.testx i
    ' or
    call s.textx(i)
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Fanatic Member VBAhack's Avatar
    Join Date
    Dec 2004
    Location
    Sector 000
    Posts
    617

    Re: Excel VBA Class module

    Thanks! I figured it must be something simple...

Posting Permissions

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



Click Here to Expand Forum to Full Width