Class Modules vs Std Modules - how to use?

C

Colleyville Alan

Ok, I'm fairly new to this, so go easy on me. I have downloaded an
application that has some class modules I'd like to use. When I imported
them into Access, it did not seem to find them. When I used the name of the
project and pressed the dot for the code completion, other subs in my
standard modules were showing up, but not the subs and functions of the
class modules.

How do I make them available to the program? Do I reimport them as standard
modules? I see nothing in the Tools|References selections. Is there some
declaration I need to make?

Thanks.
Alan
 
K

Karl E. Peterson

Colleyville Alan said:
Ok, I'm fairly new to this, so go easy on me. I have downloaded an
application that has some class modules I'd like to use. When I imported
them into Access, it did not seem to find them. When I used the name of the
project and pressed the dot for the code completion, other subs in my
standard modules were showing up, but not the subs and functions of the
class modules.

How do I make them available to the program? Do I reimport them as standard
modules? I see nothing in the Tools|References selections. Is there some
declaration I need to make?

In a nutshell, you create an instance of the class, *then* use it...

Dim cls As MyClass ' declare
Set cls = New MyClass ' instantiate
cls.ThisProperty = 1 ' set a property value or two
cls.ThatProperty = 2
cls.DoSomething ' invoke a method
Debug.Print cls.Result ' examine a property

I don't have but a few Office based examples on my site
(http://www.mvps.org/vb/samples.htm) but almost everything there uses a class in some
way. Wouldn't hurt to take a look at a few examples of things that interest you. I
think the ClipEx.zip file contains examples of using classes in Excel and PowerPoint.

Later... Karl
 
C

Colleyville Alan

Karl E. Peterson said:
In a nutshell, you create an instance of the class, *then* use it...

Dim cls As MyClass ' declare
Set cls = New MyClass ' instantiate
cls.ThisProperty = 1 ' set a property value or two
cls.ThatProperty = 2
cls.DoSomething ' invoke a method
Debug.Print cls.Result ' examine a property

I don't have but a few Office based examples on my site
(http://www.mvps.org/vb/samples.htm) but almost everything there uses a class in some
way. Wouldn't hurt to take a look at a few examples of things that interest you. I
think the ClipEx.zip file contains examples of using classes in Excel and PowerPoint.

Later... Karl
--

Thanks - especially helpful because they were your clipboard class modules!
 
M

Martin Seelhofer

Hi Alan

To access the functionality of class modules, you first have to
instantiate an object out of the class. You do this by declaring
a variable of this class using keyword "New":

Dim myObj As New clsStringTokenizer

.... where "clsStringTokenizer" is the name of the class module.
Note that the keyword New is required, here.

Afterwards, you can access the functionality of the class module
through the variable:

myObj.myFunction param1, param2, ...
Call myObj.myFunction(param1, param2, ...)
result = myObj.myProperty
....

Note that class modules allow you to use the same object
oriented programming style as with the built-in application-
defined objects. That's why they are pretty useful...


Cheers,

Martin
 
K

Karl E. Peterson

Hi Martin --
To access the functionality of class modules, you first have to
instantiate an object out of the class. You do this by declaring
a variable of this class using keyword "New":

Dim myObj As New clsStringTokenizer

... where "clsStringTokenizer" is the name of the class module.
Note that the keyword New is required, here.

Actually, use of New in this manner is considered very poor form, as you voluntarily
relinquish all control over creation and destruction of the object. Far better to
omit New from the declaration, and instantiate the object *on purpose* when it's
actually needed.

Later... Karl
 
M

Martin Seelhofer

Hi Karl

Can you please clarify your point about "instantiating on purpose"?
I thought that there was basically no difference between instantiating
on declaration and instantiation immediately after declaration? Since
you can place your dim anywhere in a sub (although this might not
be called "good programming style"), you pretty much are in control
of when your object is instantiated?! Or am I missing something, here?

So again, what's the (noteworthy) difference between...

Dim myObj As New clsStringTokenizer

.... and ...

Dim myObj As clsStringTokenizer
Set myObj = New clsStringTokenizer


Thanks for clarifying...

Martin
 
K

Karl E. Peterson

Hi Martin --
Can you please clarify your point about "instantiating on purpose"?
I thought that there was basically no difference between instantiating
on declaration and instantiation immediately after declaration? Since
you can place your dim anywhere in a sub (although this might not
be called "good programming style"), you pretty much are in control
of when your object is instantiated?! Or am I missing something, here?

So again, what's the (noteworthy) difference between...

Dim myObj As New clsStringTokenizer

... and ...

Dim myObj As clsStringTokenizer
Set myObj = New clsStringTokenizer

There really is no appreciable difference between those two, given you followed one
with the other in the second example. There are many cases when you wouldn't want an
object instantiated immediately. Perhaps it has an Initialize event that consumes
resources of some sort, and you only want to actually go through that in cases where
other conditions are met, for example.

Consider object destruction, as well. If you declare it using As New, what happens
when you subsequently set it to Nothing? How does that compare with using the two
step instantiation? (Answer: you get two instances in the first case, and just the
single intended instance in the second.) Again, if the object is consuming resources
of some type, even if just time, that's potentially costly.

In the simple cases, I agree with you. But I really think the better habit is taking
full charge of the situation yourself.

Later... Karl
 
T

Tushar Mehta

Try the following and you'll see the difference:

Option Explicit

Dim x As New Class1, y As Class1
Sub testit()
MsgBox x Is Nothing
MsgBox y Is Nothing
End Sub

and it will be made even more evident by:

Option Explicit

Dim x As New Class1, y As Class1
Sub testit()
MsgBox x Is Nothing
Set x = Nothing
MsgBox x Is Nothing
MsgBox y Is Nothing
End Sub

Effectively, *every* reference to x is preceded by a
'If x is Nothing then set x=new class1'

Some cost overhead. More significant is the capability penalty in
never being able to test x=nothing.

--
Regards,

Tushar Mehta, MS MVP -- Excel
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions

Hi Karl

Can you please clarify your point about "instantiating on purpose"?
I thought that there was basically no difference between instantiating
on declaration and instantiation immediately after declaration? Since
you can place your dim anywhere in a sub (although this might not
be called "good programming style"), you pretty much are in control
of when your object is instantiated?! Or am I missing something, here?

So again, what's the (noteworthy) difference between...

Dim myObj As New clsStringTokenizer

... and ...

Dim myObj As clsStringTokenizer
Set myObj = New clsStringTokenizer


Thanks for clarifying...

Martin
 
M

Malcolm Smith

Indeed, which is why I always advise people to *never* use
auto-instantiation in this manner.

Also since the test of the x object creates an instance; consider if there
is some code in the class' initialization method.

I don't like it at all and putting the New statement in the declaration
statement is lazy and could have, um, interesting side effects.

- Malc
www.dragondrop.com
 
C

Chip Orange

I too am having this problem, and am finding that the below example is not
working.

My problem is that the class module is defined in one template (which I have
in my startup dir so it's available globally), and I'm trying to use it from
a second template.

So, the question still, is there a special declaration I need to make,
either in the source of the class module, or in the client template of the
class module, to do this?

thanks.

Chip
 
K

Karl E. Peterson

Chip Orange said:
I too am having this problem, and am finding that the below example is not
working.

My problem is that the class module is defined in one template (which I have
in my startup dir so it's available globally), and I'm trying to use it from
a second template.

So, the question still, is there a special declaration I need to make,
either in the source of the class module, or in the client template of the
class module, to do this?

Ah! Horse of a different color, that. I think you need to go to Tools-References,
and select the other project there.

Later... Karl
 
C

Chip Orange

Yes, I did that, thanks for the response.

In module A I have a class definition.

In module B I have a reference to module A, and am trying to use the class
definition (and am getting a "user type not defined" error).

Both modules are in my Word startup.

I have tried declaring the variable in module B as both of type <class name>
and of type A.<class name> and no difference.

Am using MS Word XP.

Thanks for any ideas.

Chip
 
K

Karl E. Peterson

Hi Chip --

I'm sorry, but at this point we'll need to hope someone who's done more work in Word
than I have can respond. Wish I knew.

Sorry... Karl
 
C

Chip Orange

ok, I found the solution on someones web page, and appreciate all the
responses from everyone here. Here's what's necessary:

in the source project the "instancing" property for each class needs to be
changed from "private" to "public-non-creatable".
Also, a public function needs to be added to a standard code module in the
source project which returns a newly created instance of each class. This
is because you cannot use the "new" operator for a class declared in a
different project.

Once you've done these 2 items to the source project, you can then declare
variables in your project of the specified class, and use the special
instantiation functions to create them.


All of this is detailed at:

http://www.vbdesign.net/cadpages/features/twls/codereuse.htm

HTH,

Chip

Chip Orange said:
Yes, I did that, thanks for the response.

In module A I have a class definition.

In module B I have a reference to module A, and am trying to use the class
definition (and am getting a "user type not defined" error).

Both modules are in my Word startup.

I have tried declaring the variable in module B as both of type <class name>
and of type A.<class name> and no difference.

Am using MS Word XP.

Thanks for any ideas.

Chip

I
it
 

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