Results 1 to 11 of 11

Thread: Declare a Class global to app but not to anything else.

  1. #1

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819

    Declare a Class global to app but not to anything else.

    I have a class module that I need to be global to the entire project.

    The only way to declare it that I know of is in a standard Module.

    And standard modules don't allow "Friend".

    I could try having every single module/class/form that needs it create their own private instance and set them all equal to the same instance.

    Is there a simpler way that I'm missing?

  2. #2
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    6,747

    Re: Declare a Class global to app but not to anything else.

    Public g_myClass As clsMyClass
    This defined in a module.
    I use this for the settings of project.

  3. #3

  4. #4
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,915

    Re: Declare a Class global to app but not to anything else.

    Yes, Trick nailed it. I do this with a few things, and it's very nice. You do have to edit the CLS module with something like Notepad to change this attribute to True. But, after that's done, that particular CLS is (basically) just always instantiated, and uses the same object name as the class name (no need to explicitly instantiate anything nor create any object variable for it). It's like writing CLS modules that become an extension of the language.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  5. #5

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819

    Re: Declare a Class global to app but not to anything else.

    OK, thank you. I'm sorry for taking so long to reply. I didn't realize anyone had answered.

    I just read the page The trick posted and I don't get it.

    Elroy - OK, so I edit the module in notepad. No problem.

    If I'm understanding this correctly, when I do that I no longer have to declare any instance of the class at all?

    And it's public to the project and private outside the project?

    How do I reference it in my code? Just use the class module name?

    I have a class that subclasses textboxes for a text-based game I'm creating.

    A window can be closed at any time which will make that instance of the class invalid.

    So instead of having to check for it's validity every time I try to do something with it, I want a Gate-keeper Class that intercepts everything, checks for validity and then takes appropriate action.

    In other words, all the validating is in one place and nothing else has to worry if it's valid or not. Just send your request and the Gate-keeper will take care of the details.

    So it's one instance of a class that's global so that everything has access to it.

  6. #6
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,268

    Re: Declare a Class global to app but not to anything else.

    And why are you posting in Codebank?
    reported to moderator
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  7. #7

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819

    Re: Declare a Class global to app but not to anything else.

    I didn't realize this was posted in code-bank.

    Yeah, it doesn't belong here.

    Sorry about that.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Declare a Class global to app but not to anything else.

    Thread moved.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819

    Re: Declare a Class global to app but not to anything else.

    Thank you.

  10. #10
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    10,915

    Re: Declare a Class global to app but not to anything else.

    Quote Originally Posted by cafeenman View Post
    OK, so I edit the module in notepad.
    That's correct. They didn't give us a way to set it in the VB6 IDE, but it works great if you just edit the CLS module in Notepad, change the VB_PredeclaredId from False to True, save it ... and then re-load your VB6 project.

    Quote Originally Posted by cafeenman View Post
    If I'm understanding this correctly, when I do that I no longer have to declare any instance of the class at all?
    That's correct. When you do this, you automatically get an instantiated object variable (automatically declared and instantiated). You just use it like any other object.

    Quote Originally Posted by cafeenman View Post
    And it's public to the project and private outside the project?
    Again correct. It's public to your entire VBP project, but not exposed to other programs.

    Quote Originally Posted by cafeenman View Post
    How do I reference it in my code? Just use the class module name?
    You got it.

    Quote Originally Posted by cafeenman View Post
    I have a class that subclasses textboxes for a text-based game I'm creating.
    When doing this, it imposes no limits on the class you do it to. If, inside that class, you instantiate other classes, no problem (so long as it worked before you did this).

    Quote Originally Posted by cafeenman View Post
    A window can be closed at any time which will make that instance of the class invalid.
    This has nothing to do with windows being opened and closed. Basically, it's instantiated when your program starts, and gets uninstantiated when your program terminates. (Actually, the instantiation takes place the first time you "touch" this VB_PredeclaredId=True class with code, but that's typically a detail that doesn't really matter.)

    Quote Originally Posted by cafeenman View Post
    So instead of having to check for it's validity every time I try to do something with it, I want a Gate-keeper Class that intercepts everything, checks for validity and then takes appropriate action.

    In other words, all the validating is in one place and nothing else has to worry if it's valid or not. Just send your request and the Gate-keeper will take care of the details.
    I don't really understand what you're saying there, but again, you can do anything with these VB_PredeclaredId=True classes that you can do with any other class.

    Quote Originally Posted by cafeenman View Post
    So it's one instance of a class that's global so that everything has access to it.
    Again, yep. The "implicit" object variable name (named the same as the class name) is seen project wide. Just to further elaborate on that just a bit, let's say you have a project with Form1. And you use that Form1 object variable. Forms actually always have the VB_PredeclaredId=True set. And that's why we always get an object variable that's the same name as the form's class name. Many people don't like using that "Form1" object variable, but it's still there. And, as stated above, if you never "touch" that Form1 object variable, a form for that Form1 variable doesn't get instantiated.

    You can really think of any FRM module as a CLS module with VB_PredeclaredId=True and a User-Interface ... and that would be very accurate.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  11. #11

    Thread Starter
    PowerPoster cafeenman's Avatar
    Join Date
    Mar 2002
    Location
    Florida
    Posts
    2,819

    Re: Declare a Class global to app but not to anything else.

    Thanks for your help. I really appreciate it.

    I'll try to clear up a couple things you said you didn't understand.

    The PlayerMessage class has multiple instances that contain a Textbox.

    They have a AddPlayerMessage procedure.

    I don't want anything in the program to directly send messages to that procedure because the class instance may be invalid at any time.

    So instead I have a Public Sub named AddMessage in a standard module.

    I send messages to that with a preferred Instance I want the message to go to.

    Code:
    AddMessage "Hello, world.", idx_PlayerMessage_Aux ' idx_PlayerMessage_Primary
    AddMessage then does all the housekeeping. E.g. if the class is not valid then it decides if it should re-route the message to another instance or discard it.

    Subs like this are what prevent everything else in the code having to do those checks and take action which would significantly increase the size of the code and make it an order of magnitude harder to maintain.

    But they're Public Subs and I don't like that.

    Which is why I want to move all that stuff to a Gate-keeping class.

    That would apply to anything having anything to do with the textbox (fonts, text, colors, etc.)

    I'm assuming that if the user closes the window on the left then they don't want to see the stats and whatever else, so those messages get discarded.

    But the messages on the right are the main game so if that window is closed, it ends the app. But if the messages are paused then they get re-routed to the left window.

    Last edited by cafeenman; Jan 29th, 2025 at 04:18 PM.

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