Will calling sub's from another form cause problems?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mij
    New Member
    • Mar 2007
    • 2

    #1

    Will calling sub's from another form cause problems?

    Working with Visual Basic .net '03

    Not sure if it's an issue, but I was just wondering if there was any appreciable reason that I shouldn't use an otherwise blank form for all the sub's and public variables I need. Like, will it slow the program down on slower machines? Or would it take a pretty large program for that to happen? The method I'm planning on using is the 'Form1.VARIABLE ' thinger, which I actually figured out how to do on this site, heh.

    Now that I think about it, I'm not sure if I can even call a sub with that, so I should prolly check that next time I sit down to do some work. At any rate, kinda hoping to be able to organize things a little better with this.

    Thanks! =)
  • bergy
    New Member
    • Mar 2007
    • 89

    #2
    There are a few ways to accomplish what you're going for. My recommendation is to use a Module file instead of a blank form. In a module you can create subs, functions and variables and access them from any form inside your project as long as they're flagged as public.

    To add a module right click on your project in the Solution Explorer, goto Add > New Item... Then select "Module" from the dialog list. (OR Goto the Project > Add Module menu option)

    Your module could look similar to this:
    Code:
    Module Module1
        Public String global_variable_1 = "This is a global string"
        Public String global_variable_2 = "I can get this value from anywhere in my project!"
    
        Public Sub MsgUser(ByVal Text as String)
             Msgbox(Text)
        End Sub
    
        Public Function AddItUp() As Integer
             Dim intOne As Integer = 4 
             Dim intTwo As Integer = 5
             Dim myReturn as Integer = intOne + intTwo
             Return myReturn
        End Sub
    End Module
    As long as the Public keyword is in front of your variable, sub, or function you can access them anywhere in your project :)

    Modules are great in that you don't have to worry about creating a blank form and that you can reuse them in other projects. It's good practice to keep a module of functions and subs you commonly use and have it referenced in all of the projects you create. Huge time savers!

    Happy Coding!

    Adam

    Originally posted by Mij
    Working with Visual Basic .net '03

    Not sure if it's an issue, but I was just wondering if there was any appreciable reason that I shouldn't use an otherwise blank form for all the sub's and public variables I need. Like, will it slow the program down on slower machines? Or would it take a pretty large program for that to happen? The method I'm planning on using is the 'Form1.VARIABLE ' thinger, which I actually figured out how to do on this site, heh.

    Now that I think about it, I'm not sure if I can even call a sub with that, so I should prolly check that next time I sit down to do some work. At any rate, kinda hoping to be able to organize things a little better with this.

    Thanks! =)

    Comment

    • bergy
      New Member
      • Mar 2007
      • 89

      #3
      I just thought I'd clarify. When using variables and calling subs/functions in a form that reside your module you don't have to type anything special (like you mentioned with Form1.VARIABLE) . Using my example above, typing MsgUser("test") from Form1 will automatically reference the sub in Module1. You don't need to type Module1.MsgUser ("test")

      Comment

      • Mij
        New Member
        • Mar 2007
        • 2

        #4
        Hmm.... First off, handy, thanks! Second, does this mean that if I stored all my long-term variables in the module that I could access them from any other part of the application? Like, say, Form1 tosses a string to the variable in Module1 and then Form2 can play with it, too?

        *Goes off to tweak with this*

        -Mij

        Comment

        • bergy
          New Member
          • Mar 2007
          • 89

          #5
          Yep, that's exactly what it means! The variables can be accessed from anywhere in your project (not solution, project - keep in mind that form variables are not available to the rest of your solution either)

          Comment

          Working...