Setting the Connection String Just Once

I have Midi application that requires just two different connection strings, one for my dev environment and one for production.  The main form is frmMain.

The main form has a function that detects whidh connection is sucessful.  This is called in the load routine.  Since frmMain is alway open, it seem reasonable to create a public property (called Conn) which held that connection string.

But how do I get reference to the open instance of frmMain  The following code in the child window only gets a new instance right

Dim frm as New frmMain
sqlConn = frmMain.Conn  <-- produces and empty string.



Answer this question

Setting the Connection String Just Once

  • tism


    For information like that, that isn't really specific to the main form of the application, I like to group those items together into related classes that expose shared properties.

    For instance, you may have a "Globals" class that might look like this:

    public class Globals

          private _connectionString as String

          public property ConnectionString as String
             get
                return _connectionString
             end get
             set(value as string)
                _connectionString = value
             end set
          end property

       end class

    and then your code to access it may look like:

       dim conn as new sqlconnection(globals.connectionstring)


    To answer your specific question about how to access non-shared variables on your main form, you should do the following:

    1. Create a private shared class variable called _instance:

       private shared _instance as frmMain

    2. In your New method of your form, assign the current instance of mdiMain, "Me", to your _instance variable:

       _instance = Me

    3. Create a Shared friend property called Instance:

       friend shared readonly property Instance as frmMain
          get
             return _instance
          end get
       end property

    4. Now, if you need to access an instance property on frmMain, do so like this from anywhere in your application:

       frmMain.Instance.MyPropertyName


    HTH,

    -Paul

  • Lion4ever

    Looks great.

    I am somewhat new to OO programming.  Since I want to detect the connection just once, the global class would have to attempt to open a connection using one of two connection strings, then set the connectionstring property.  Afterwhich all forms would be able to access as you stated above.

    Does the public before the Class declaration mean when this class is instantiated in any form  -- it is available to all forms

    I have also notice that many OO programmers precede their variable names with an underscore.  Is this simply a convention or is there a reason behind it


  • philmwebb

    setting frm = me.mdiparent works
  • season


    Glad to help you out! I know it can be quite a challenge to learn a new framework, a new language (or a new way of using it if you're moving from VB6 or VBA), the in's and out's of the particular components, tools, etc. you need to use to build your application, etc. (Whew!)

    Actually, as far as exiting your application, that will be taken care of you automatically by the framework. The "Closing" event gives you the opportunity to abort the close of a form (for example, if some validation failed), and the "Closed" event lets you take care of any post-work that you may need to perform after the form has been closed by the user. (You can also call Application.Exit yourself from anywhere in the application anytime you want to the application to exit.)

    -Paul

  • Robert Horvick

    Ok, I am trying to follow your advice.

    I created the following main sub.

    AppSettings is a Public Class that holds the application settings I want to share across all forms.  This main sub instantiates that class.  Calls its SetAppSettings method.  Then tries to display the main form.

    At first this all worked perfectly.  Then without any change in the code, frmMain appears to display and then simply disappears as if Sub Main frm variable goes out of scope.  declaring a Public variable at the module level did not change the behaviour.

    What is wrong with this code

    Public Module Main
        Public AppSettings As New PPSCommonClasses.PPSAppSettings

        Public Sub Main()
            Dim frm As New frmMain
            Cursor.Current = Cursors.WaitCursor
            AppSettings.SetAppSettings()
            Cursor.Current = Cursors.Default
            frm.Show()
        End Sub
    End Module

  • Tallgoose

    Another porion would be to create a base from class and derive the forms from it, then access all the necessary properties from the the base class using me.propname
  • antcv

    Thanks, you have helped me step into a new "class" of programming.  No pun intended.

    I assume that to exit the application I can put Application.Exit in the Closed event of frmMain (MDI form).

    Bill

  • Michealmess


    Bill,

    If you are using your Main module as your entry point to your application, you will need to replace "frm.Show()" with "Application.Run(frm)"

    Application.Run will direct the Windows message pump to frmMain and allow your application to behave as expected, and frmMain will only close after you shutdown the application.

    Now, for a further refinement to the initialization of your AppSettings module, you may want to create a "Sub New" within it to do your initialization work. This contstructor will run the first time you access any property on the module, and will save you from an explicit call to initialize it before using it for the first time.

    HTH,

    -Paul


  • apaspula

    Hi Bill,

    So yes, you could have logic that is run from within your frmMain that sets your connection property to either of the two connection strings and then stores it into the _connectionString variable, accessible through a public or friend property. 

    For more information about access modifiers, take a look at this explanation in MSDN library:

    http://msdn.microsoft.com/library/default.asp url=/library/en-us/vblr7/html/vastmClass.asp

    The "_" underscore prefix is simply a convention you can use to distinguish class-level variables from variables declared in the current method or property. You might also see these variables prefixed with "m_". 

    -Paul

  • Setting the Connection String Just Once