Getting Started with Classes

D

Donald Nova

I'm trying to get started with Classes, I understand the concepts but I just
cannot seem to get them to work. Here is some very basic demo code to
highlight the problems I'm getting:

=====================
' Code attached to ThisDocument
Public Sub AutoOpen()
Dim c1 As New Class1
Dim c2 As New Class2

c1.SayHello
End Sub
=====================

=====================
' Code attached to Class1
Public Sub SayHello()
Debug.Print "Class1 says hello"
c2.SayHelloAlso
End Sub
=====================

=====================
' Code attached to Class2
Public Sub SayHelloAlso()
Debug.Print "Class2 also says hello"
End Sub
=====================

When Run, I get a error dialog box stating "Object required".

Now I am sure the problem is because the initialisation of the classes are
only temporary within the AutoOpen() procedure, however I'm not entirely
sure how to make the classes persistant and permanantly accessable by other
classes and functions?

This has been driving me mad!!

Cheers
Donald
 
P

Perry

Try

'<<module general declaration section
Dim c1 As Class1

Public Sub AutoOpen()
Set c1 = New Class1
c1.SayHello
End Sub

While the above will work,
buy/read the book advised by Steve
to get the grips of using classes.

Particularly the subjects on scope of classes.

Krgrds,
Perry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top