Results 1 to 9 of 9

Thread: Declaring ClassModule as public

  1. #1

    Thread Starter
    Member
    Join Date
    May 2011
    Posts
    35

    Question 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"
    ----------------------------------------------------------------------------------------------------------------------------------------------------

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    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)

  3. #3
    Fanatic Member
    Join Date
    Mar 2009
    Posts
    804

    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.

  4. #4

    Thread Starter
    Member
    Join Date
    May 2011
    Posts
    35

    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"
    ----------------------------------------------------------------------------------------------------------------------------------------------------

  5. #5
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Declaring ClassModule as public

    Quote Originally Posted by WeeBee View Post
    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".

  6. #6

    Thread Starter
    Member
    Join Date
    May 2011
    Posts
    35

    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"
    ----------------------------------------------------------------------------------------------------------------------------------------------------

  7. #7

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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)

  9. #9
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    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
  •  



Click Here to Expand Forum to Full Width