|
-
Jun 16th, 2011, 11:31 PM
#1
Thread Starter
Member
Declaring ClassModule as public
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?
----------------------------------------------------------------------------------------------------------------------------------------------------
"Pasting somebody's Code or Link is easy; Spending some time and addressing the real issue & putting our own ideas and codes is difficult"
----------------------------------------------------------------------------------------------------------------------------------------------------
-
Jun 17th, 2011, 07:18 AM
#2
Re: Declaring ClassModule as public
Yes, of course you can do that:
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
Don't forget to destroy it when exiting program. (set myclass = nothing)
-
Jun 17th, 2011, 07:24 AM
#3
Re: Declaring ClassModule as public
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.
-
Jun 17th, 2011, 02:30 PM
#4
Thread Starter
Member
Re: Declaring ClassModule as public
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?
----------------------------------------------------------------------------------------------------------------------------------------------------
"Pasting somebody's Code or Link is easy; Spending some time and addressing the real issue & putting our own ideas and codes is difficult"
----------------------------------------------------------------------------------------------------------------------------------------------------
-
Jun 17th, 2011, 03:56 PM
#5
Re: Declaring ClassModule as public
 Originally Posted by WeeBee
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?
Not really, it may create you more troubles than you can wish for.
Forum has few discussions on the subject so if interested try searching for something like "why dim as new is a bad idea".
-
Jun 17th, 2011, 05:30 PM
#6
Thread Starter
Member
Re: Declaring ClassModule as public
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?
----------------------------------------------------------------------------------------------------------------------------------------------------
"Pasting somebody's Code or Link is easy; Spending some time and addressing the real issue & putting our own ideas and codes is difficult"
----------------------------------------------------------------------------------------------------------------------------------------------------
-
Jun 17th, 2011, 07:58 PM
#7
Re: Declaring ClassModule as public
In general that would be the correct approach.
-
Jun 18th, 2011, 11:09 AM
#8
Re: Declaring ClassModule as public
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)
-
Jun 18th, 2011, 11:55 AM
#9
Re: Declaring ClassModule as public
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:
Code:
Dim S As String
S = Left$("1234567890X", 10)
Do While Len(S) > 0
Text1.SelText = S & vbNewLine
S = BigBag.Left(S)
Loop
Static modules can even have Property Let/Get/Set procedures in them.
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|