I have a Class-module with lots of Procedures & Functions etc.
Is that OK to declare this class-module in a module as public? So that i can get it any time any where in the program
Any suggestion?
Printable View
I have a Class-module with lots of Procedures & Functions etc.
Is that OK to declare this class-module in a module as public? So that i can get it any time any where in the program
Any suggestion?
Yes, of course you can do that:
Don't forget to destroy it when exiting program. (set myclass = nothing)Code:'in the module
Option Explicit
Public myClass As Class1
'elsewhere in your code - perhaps main form load event
Set myClass = New Class1 '<<< do it only once when program starts
Yes you can declare it as Public in a Bas Module and use it
anywhere in your project. However there is a caveat: VB
does not allow Public WithEvents in a module. So if the class
has Events you will need to declare the class WithEvents in
each module where you need it. Or you could declare it
as Public WithEvents in your main form and refer to that
instance in the other modules.
RhinoBull, VBClassicRocks Thanks a lot people
VBClassicRocks, thanks for the info....Actually there is no events in this class
"'in the module
Option Explicit
Public myClass As Class1
'elsewhere in your code - perhaps main form load event
Set myClass = New Class1 '<<< do it only once when program starts"
What about declaring the class-module in a module like this:
Public myClass As New Class1
and when exiting program. (set myclass = nothing)
By doing so i can avoid "Set myClass = New Class1" every time when i am calling this class...
Is that ok?
RhinoBull, Thanks for the info...
1 More question:
In a bas module i am declaring:
Public myClass As Class1
------------------------------
My startup object is Sub Main()
So In submain() of bas module
Set myClass = New Class1
and when exiting program. (set myclass = nothing)
what about this?
In general that would be the correct approach.
Agreed... and here is a link to relevant article: Why shouldn't I use "Dim .. As New .."? from our Classic VB FAQs (in the FAQ forum)
I would go back to the original post and ask "Why do you want to do this?"
Normally it doesn't make any sense to write a Class to do what you propose. I'm not sure I see the value it offers over a static module if you merely want to create a Big Bag O' Procedures.
Members of static modules can even be qualified as required, example where a Left() function is defined in BigBag:
Static modules can even have Property Let/Get/Set procedures in them.Code:Dim S As String
S = Left$("1234567890X", 10)
Do While Len(S) > 0
Text1.SelText = S & vbNewLine
S = BigBag.Left(S)
Loop
Now that doesn't mean it never makes sense to create a "bag Class," but usually only if you're trying to put such a bag of procs into a DLL.
If you really need to do this one workaround is to save and close the Project, then open your .CLS module in a text editor and change PredeclaredId to True and save. When you re-open the Project in VB6.exe you will find you have a predeclared instance of your Class, much as you get with something like a Form module.
Note that you will still get "Dim ... As New" semantics just as you have with the default instance of any Form. There is nothing at all wrong with "As New" despite the comments above, though I agree you need to be aware of its implications.
Deprecating "As New" is like saying you should never use an axe because you might cut your leg off, so always use a butter knife. But just like an axe, "As New" should be used with proper respect for potential hazards.
Indiscriminate use of DoEvents() is far, far more hazardous yet people recommend that here all day long.