Global variables in a project.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • UJ

    Global variables in a project.

    How do I create an instance of a class that is accessible to all
    classes/forms in my project? I already have a startup module and I was
    thinking putting stuff in there and then accessing it through there.

    I need it for things like db connections, security manager, general db
    functions, .....

    Any help would be appreciated.... .

    UJ.


  • Bob Powell [MVP]

    #2
    Re: Global variables in a project.

    You can create a class that uses the Singleton Pattern.
    Something like...

    public class DBCentral

    private _theInstance as DBCentral

    public readonly shared property TheInstance() as DBCentral
    get()
    if _theInstance is nothing then
    _theInstance=ne w DBCentral()
    end if
    return _theInstance
    end get
    end property

    end class

    I didn't check the exact syntax but you get the picture....

    --
    Bob Powell [MVP]
    Visual C#, System.Drawing

    Find great Windows Forms articles in Windows Forms Tips and Tricks


    Answer those GDI+ questions with the GDI+ FAQ


    All new articles provide code in C# and VB.NET.
    Subscribe to the RSS feeds provided and never miss a new article.





    "UJ" <UJ@nowhere.com > wrote in message
    news:O9JQkMQFFH A.3376@TK2MSFTN GP12.phx.gbl...[color=blue]
    > How do I create an instance of a class that is accessible to all
    > classes/forms in my project? I already have a startup module and I was
    > thinking putting stuff in there and then accessing it through there.
    >
    > I need it for things like db connections, security manager, general db
    > functions, .....
    >
    > Any help would be appreciated.... .
    >
    > UJ.
    >
    >[/color]


    Comment

    • chanmmn

      #3
      Re: Global variables in a project.



      chanmm

      "UJ" <UJ@nowhere.com > wrote in message
      news:O9JQkMQFFH A.3376@TK2MSFTN GP12.phx.gbl...[color=blue]
      > How do I create an instance of a class that is accessible to all
      > classes/forms in my project? I already have a startup module and I was
      > thinking putting stuff in there and then accessing it through there.
      >
      > I need it for things like db connections, security manager, general db
      > functions, .....
      >
      > Any help would be appreciated.... .
      >
      > UJ.
      >
      >[/color]


      Comment

      • UJ

        #4
        Re: Global variables in a project.

        Chanmm,

        Can you point me in the correct direction? This is a general architecture
        page. I don't see anything specific to globals.

        UJ.

        "chanmmn" <chanmmn@hotmai l.com> wrote in message
        news:OgttB1QFFH A.3596@TK2MSFTN GP12.phx.gbl...[color=blue]
        > http://msdn.microsoft.com/architecture
        >
        > chanmm
        >
        > "UJ" <UJ@nowhere.com > wrote in message
        > news:O9JQkMQFFH A.3376@TK2MSFTN GP12.phx.gbl...[color=green]
        >> How do I create an instance of a class that is accessible to all
        >> classes/forms in my project? I already have a startup module and I was
        >> thinking putting stuff in there and then accessing it through there.
        >>
        >> I need it for things like db connections, security manager, general db
        >> functions, .....
        >>
        >> Any help would be appreciated.... .
        >>
        >> UJ.
        >>
        >>[/color]
        >
        >[/color]


        Comment

        • Samuel R. Neff

          #5
          Re: Global variables in a project.


          The .NET recommended practice for creating singletons is to use a
          single line..

          Public Shared ReadOnly Instance As New [ClassName]

          For example..

          Public NotInheritable Class DBCentral

          Public Shared ReadOnly Instance As New DBCentral

          Private Sub New()
          End Sub

          End Class


          Sam


          On Thu, 17 Feb 2005 17:09:03 +0100, "Bob Powell [MVP]"
          <bob@_spamkille r_bobpowell.net > wrote:
          [color=blue]
          >You can create a class that uses the Singleton Pattern.
          >Something like...
          >
          >public class DBCentral
          >
          >private _theInstance as DBCentral
          >
          >public readonly shared property TheInstance() as DBCentral
          >get()
          > if _theInstance is nothing then
          > _theInstance=ne w DBCentral()
          > end if
          > return _theInstance
          >end get
          >end property
          >
          >end class
          >
          >I didn't check the exact syntax but you get the picture....[/color]

          B-Line is now hiring one VB.NET developer for
          WinForms + WebServices position with ASPX in future.
          Seaking mid to senior level developer. For
          information or to apply e-mail sam_blinex_com.

          Comment

          • UJ

            #6
            Re: Global variables in a project.

            OK I'm completely lost. I think I didn't explain myself correctly. I want to
            be able define somewhere a user object called SecurityManager (which I
            wrote). I want to be able to define it somewhere so I can reference it from
            all kinds of objects. I have found so far that what I can do is create a
            startup module (let's call it StartItUp), and create the instance of the
            class as public in there. But then I always need to reference my Security
            Manager as StartItUp.SecMa nager.

            Is there an easier way?

            TIA - UJ

            "Samuel R. Neff" <blinex@newsgro up.nospam> wrote in message
            news:b4v911d8ia 0kjqhkb1pjcooui o87nh5gtd@4ax.c om...[color=blue]
            >
            > The .NET recommended practice for creating singletons is to use a
            > single line..
            >
            > Public Shared ReadOnly Instance As New [ClassName]
            >
            > For example..
            >
            > Public NotInheritable Class DBCentral
            >
            > Public Shared ReadOnly Instance As New DBCentral
            >
            > Private Sub New()
            > End Sub
            >
            > End Class
            >
            >
            > Sam
            >
            >
            > On Thu, 17 Feb 2005 17:09:03 +0100, "Bob Powell [MVP]"
            > <bob@_spamkille r_bobpowell.net > wrote:
            >[color=green]
            >>You can create a class that uses the Singleton Pattern.
            >>Something like...
            >>
            >>public class DBCentral
            >>
            >>private _theInstance as DBCentral
            >>
            >>public readonly shared property TheInstance() as DBCentral
            >>get()
            >> if _theInstance is nothing then
            >> _theInstance=ne w DBCentral()
            >> end if
            >> return _theInstance
            >>end get
            >>end property
            >>
            >>end class
            >>
            >>I didn't check the exact syntax but you get the picture....[/color]
            >
            > B-Line is now hiring one VB.NET developer for
            > WinForms + WebServices position with ASPX in future.
            > Seaking mid to senior level developer. For
            > information or to apply e-mail sam_blinex_com.[/color]


            Comment

            • Dennis

              #7
              RE: Global variables in a project.

              In your start-up module, you can just create a public instance and use it
              throughout your applciation:

              PUblic myClassInstance as myClass = new myclass

              You can then referr to myClassInstance throughout your applicaiton. Hope I
              understood your question correctly.

              "UJ" wrote:
              [color=blue]
              > How do I create an instance of a class that is accessible to all
              > classes/forms in my project? I already have a startup module and I was
              > thinking putting stuff in there and then accessing it through there.
              >
              > I need it for things like db connections, security manager, general db
              > functions, .....
              >
              > Any help would be appreciated.... .
              >
              > UJ.
              >
              >
              >[/color]

              Comment

              • Bob Powell [MVP]

                #8
                Re: Global variables in a project.

                Thats rubbish. This gives you a new instance of the class every time you ask
                for it. Not a single persistent instance that can store instance data.

                --
                Bob Powell [MVP]
                Visual C#, System.Drawing

                Find great Windows Forms articles in Windows Forms Tips and Tricks


                Answer those GDI+ questions with the GDI+ FAQ


                All new articles provide code in C# and VB.NET.
                Subscribe to the RSS feeds provided and never miss a new article.





                "Samuel R. Neff" <blinex@newsgro up.nospam> wrote in message
                news:b4v911d8ia 0kjqhkb1pjcooui o87nh5gtd@4ax.c om...[color=blue]
                >
                > The .NET recommended practice for creating singletons is to use a
                > single line..
                >
                > Public Shared ReadOnly Instance As New [ClassName]
                >
                > For example..
                >
                > Public NotInheritable Class DBCentral
                >
                > Public Shared ReadOnly Instance As New DBCentral
                >
                > Private Sub New()
                > End Sub
                >
                > End Class
                >
                >
                > Sam
                >
                >
                > On Thu, 17 Feb 2005 17:09:03 +0100, "Bob Powell [MVP]"
                > <bob@_spamkille r_bobpowell.net > wrote:
                >[color=green]
                >>You can create a class that uses the Singleton Pattern.
                >>Something like...
                >>
                >>public class DBCentral
                >>
                >>private _theInstance as DBCentral
                >>
                >>public readonly shared property TheInstance() as DBCentral
                >>get()
                >> if _theInstance is nothing then
                >> _theInstance=ne w DBCentral()
                >> end if
                >> return _theInstance
                >>end get
                >>end property
                >>
                >>end class
                >>
                >>I didn't check the exact syntax but you get the picture....[/color]
                >
                > B-Line is now hiring one VB.NET developer for
                > WinForms + WebServices position with ASPX in future.
                > Seaking mid to senior level developer. For
                > information or to apply e-mail sam_blinex_com.[/color]


                Comment

                • Bob Powell [MVP]

                  #9
                  Re: Global variables in a project.

                  What's easier than....

                  public class SecurityManager

                  private _sm As new SecurityManager

                  public readonly shared TheSecurityMana ger() as SecurityManager
                  get
                  return _sm
                  end get
                  end property



                  end class

                  Now whenever you need to access your security manager you use...

                  SecurityManager .TheSecurityMan ager.SomeMethor OrOther()

                  --
                  Bob Powell [MVP]
                  Visual C#, System.Drawing

                  Find great Windows Forms articles in Windows Forms Tips and Tricks


                  Answer those GDI+ questions with the GDI+ FAQ


                  All new articles provide code in C# and VB.NET.
                  Subscribe to the RSS feeds provided and never miss a new article.





                  "UJ" <UJ@nowhere.com > wrote in message
                  news:%23ZzMi$TF FHA.3068@TK2MSF TNGP10.phx.gbl. ..[color=blue]
                  > OK I'm completely lost. I think I didn't explain myself correctly. I want
                  > to be able define somewhere a user object called SecurityManager (which I
                  > wrote). I want to be able to define it somewhere so I can reference it
                  > from all kinds of objects. I have found so far that what I can do is
                  > create a startup module (let's call it StartItUp), and create the instance
                  > of the class as public in there. But then I always need to reference my
                  > Security Manager as StartItUp.SecMa nager.
                  >
                  > Is there an easier way?
                  >
                  > TIA - UJ
                  >
                  > "Samuel R. Neff" <blinex@newsgro up.nospam> wrote in message
                  > news:b4v911d8ia 0kjqhkb1pjcooui o87nh5gtd@4ax.c om...[color=green]
                  >>
                  >> The .NET recommended practice for creating singletons is to use a
                  >> single line..
                  >>
                  >> Public Shared ReadOnly Instance As New [ClassName]
                  >>
                  >> For example..
                  >>
                  >> Public NotInheritable Class DBCentral
                  >>
                  >> Public Shared ReadOnly Instance As New DBCentral
                  >>
                  >> Private Sub New()
                  >> End Sub
                  >>
                  >> End Class
                  >>
                  >>
                  >> Sam
                  >>
                  >>
                  >> On Thu, 17 Feb 2005 17:09:03 +0100, "Bob Powell [MVP]"
                  >> <bob@_spamkille r_bobpowell.net > wrote:
                  >>[color=darkred]
                  >>>You can create a class that uses the Singleton Pattern.
                  >>>Something like...
                  >>>
                  >>>public class DBCentral
                  >>>
                  >>>private _theInstance as DBCentral
                  >>>
                  >>>public readonly shared property TheInstance() as DBCentral
                  >>>get()
                  >>> if _theInstance is nothing then
                  >>> _theInstance=ne w DBCentral()
                  >>> end if
                  >>> return _theInstance
                  >>>end get
                  >>>end property
                  >>>
                  >>>end class
                  >>>
                  >>>I didn't check the exact syntax but you get the picture....[/color]
                  >>
                  >> B-Line is now hiring one VB.NET developer for
                  >> WinForms + WebServices position with ASPX in future.
                  >> Seaking mid to senior level developer. For
                  >> information or to apply e-mail sam_blinex_com.[/color]
                  >
                  >[/color]


                  Comment

                  • Bob Powell [MVP]

                    #10
                    Re: Global variables in a project.

                    Global variable usage is a very poor architectural choice. If you use them,
                    and global methods then you go back to the dark-ages of unstructured non
                    object oriented programming and create horrible spaghetti code. That's why
                    there is no mention of them in the architecture guidelines.

                    Of course, if you're moving over from VB6 you will be used to the concept of
                    spaghetti and feel quite at home with it but VB6 wasn't ever a real
                    programming language ;-).

                    --
                    Bob Powell [MVP]
                    Visual C#, System.Drawing

                    Find great Windows Forms articles in Windows Forms Tips and Tricks


                    Answer those GDI+ questions with the GDI+ FAQ


                    All new articles provide code in C# and VB.NET.
                    Subscribe to the RSS feeds provided and never miss a new article.





                    "UJ" <UJ@nowhere.com > wrote in message
                    news:e$qKk8QFFH A.3120@TK2MSFTN GP12.phx.gbl...[color=blue]
                    > Chanmm,
                    >
                    > Can you point me in the correct direction? This is a general architecture
                    > page. I don't see anything specific to globals.
                    >
                    > UJ.
                    >
                    > "chanmmn" <chanmmn@hotmai l.com> wrote in message
                    > news:OgttB1QFFH A.3596@TK2MSFTN GP12.phx.gbl...[color=green]
                    >> http://msdn.microsoft.com/architecture
                    >>
                    >> chanmm
                    >>
                    >> "UJ" <UJ@nowhere.com > wrote in message
                    >> news:O9JQkMQFFH A.3376@TK2MSFTN GP12.phx.gbl...[color=darkred]
                    >>> How do I create an instance of a class that is accessible to all
                    >>> classes/forms in my project? I already have a startup module and I was
                    >>> thinking putting stuff in there and then accessing it through there.
                    >>>
                    >>> I need it for things like db connections, security manager, general db
                    >>> functions, .....
                    >>>
                    >>> Any help would be appreciated.... .
                    >>>
                    >>> UJ.
                    >>>
                    >>>[/color]
                    >>
                    >>[/color]
                    >
                    >[/color]


                    Comment

                    • Cor Ligthert

                      #11
                      Re: Global variables in a project.

                      Bob,

                      Good morning,
                      [color=blue]
                      > Of course, if you're moving over from VB6 you will be used to the concept
                      > of spaghetti and feel quite at home with it but VB6 wasn't ever a real
                      > programming language ;-).
                      >[/color]
                      You need some coffee

                      :-)

                      Cor


                      Comment

                      • Bob Powell [MVP]

                        #12
                        Re: Global variables in a project.

                        Nah, never drink the stuff. It makes me all hyper :-))

                        --
                        Bob Powell [MVP]
                        Visual C#, System.Drawing

                        Find great Windows Forms articles in Windows Forms Tips and Tricks


                        Answer those GDI+ questions with the GDI+ FAQ


                        All new articles provide code in C# and VB.NET.
                        Subscribe to the RSS feeds provided and never miss a new article.





                        "Cor Ligthert" <notmyfirstname @planet.nl> wrote in message
                        news:O$oKXPbFFH A.464@TK2MSFTNG P09.phx.gbl...[color=blue]
                        > Bob,
                        >
                        > Good morning,
                        >[color=green]
                        >> Of course, if you're moving over from VB6 you will be used to the concept
                        >> of spaghetti and feel quite at home with it but VB6 wasn't ever a real
                        >> programming language ;-).
                        >>[/color]
                        > You need some coffee
                        >
                        > :-)
                        >
                        > Cor
                        >
                        >[/color]


                        Comment

                        • UJ

                          #13
                          Re: Global variables in a project.

                          Bob,
                          It's all fine and good to say you don't need globals. But then how do you do
                          something like a security manager class where every class is going to need
                          to access it? Do you spend you life passing references around for it?

                          Currently I am going to put references in my startup module and then make
                          references to it by startup.securit ymanager

                          I can't think of any other way. Remember - the whole point is to have only
                          one instance of the variable around. I don't want to have many instances.

                          I supposed in a really good world, I would make it a property of the startup
                          (and I may do that).

                          Thanks for the help.

                          UJ.


                          "Bob Powell [MVP]" <bob@_spamkille r_bobpowell.net > wrote in message
                          news:OYF9zBbFFH A.464@TK2MSFTNG P09.phx.gbl...[color=blue]
                          > Global variable usage is a very poor architectural choice. If you use
                          > them, and global methods then you go back to the dark-ages of unstructured
                          > non object oriented programming and create horrible spaghetti code. That's
                          > why there is no mention of them in the architecture guidelines.
                          >
                          > Of course, if you're moving over from VB6 you will be used to the concept
                          > of spaghetti and feel quite at home with it but VB6 wasn't ever a real
                          > programming language ;-).
                          >
                          > --
                          > Bob Powell [MVP]
                          > Visual C#, System.Drawing
                          >
                          > Find great Windows Forms articles in Windows Forms Tips and Tricks
                          > http://www.bobpowell.net/tipstricks.htm
                          >
                          > Answer those GDI+ questions with the GDI+ FAQ
                          > http://www.bobpowell.net/faqmain.htm
                          >
                          > All new articles provide code in C# and VB.NET.
                          > Subscribe to the RSS feeds provided and never miss a new article.
                          >
                          >
                          >
                          >
                          >
                          > "UJ" <UJ@nowhere.com > wrote in message
                          > news:e$qKk8QFFH A.3120@TK2MSFTN GP12.phx.gbl...[color=green]
                          >> Chanmm,
                          >>
                          >> Can you point me in the correct direction? This is a general architecture
                          >> page. I don't see anything specific to globals.
                          >>
                          >> UJ.
                          >>
                          >> "chanmmn" <chanmmn@hotmai l.com> wrote in message
                          >> news:OgttB1QFFH A.3596@TK2MSFTN GP12.phx.gbl...[color=darkred]
                          >>> http://msdn.microsoft.com/architecture
                          >>>
                          >>> chanmm
                          >>>
                          >>> "UJ" <UJ@nowhere.com > wrote in message
                          >>> news:O9JQkMQFFH A.3376@TK2MSFTN GP12.phx.gbl...
                          >>>> How do I create an instance of a class that is accessible to all
                          >>>> classes/forms in my project? I already have a startup module and I was
                          >>>> thinking putting stuff in there and then accessing it through there.
                          >>>>
                          >>>> I need it for things like db connections, security manager, general db
                          >>>> functions, .....
                          >>>>
                          >>>> Any help would be appreciated.... .
                          >>>>
                          >>>> UJ.
                          >>>>
                          >>>>
                          >>>
                          >>>[/color]
                          >>
                          >>[/color]
                          >
                          >[/color]


                          Comment

                          • Cor Ligthert

                            #14
                            Re: Global variables in a project.

                            UJ,

                            The simplest you can do is to pass the variables in the constructor.

                            Cor


                            Comment

                            • Jay B. Harlow [MVP - Outlook]

                              #15
                              Re: Global variables in a project.

                              Bob,[color=blue]
                              > Thats rubbish. This gives you a new instance of the class every time you
                              > ask for it. Not a single persistent instance that can store instance data.[/color]
                              Huh?

                              Looking at Samual's code, he has a readonly shared field. That shared field
                              will be initialized when the class itself is initialized (when the Shared
                              Sub New runs).
                              [color=blue][color=green]
                              >> Public Shared ReadOnly Instance As New DBCentral[/color][/color]

                              How does it give a new instance each time you ask for it?

                              Very curious
                              Jay

                              "Bob Powell [MVP]" <bob@_spamkille r_bobpowell.net > wrote in message
                              news:%231Snv8aF FHA.2608@TK2MSF TNGP10.phx.gbl. ..[color=blue]
                              > Thats rubbish. This gives you a new instance of the class every time you
                              > ask for it. Not a single persistent instance that can store instance data.
                              >
                              > --
                              > Bob Powell [MVP]
                              > Visual C#, System.Drawing
                              >
                              > Find great Windows Forms articles in Windows Forms Tips and Tricks
                              > http://www.bobpowell.net/tipstricks.htm
                              >
                              > Answer those GDI+ questions with the GDI+ FAQ
                              > http://www.bobpowell.net/faqmain.htm
                              >
                              > All new articles provide code in C# and VB.NET.
                              > Subscribe to the RSS feeds provided and never miss a new article.
                              >
                              >
                              >
                              >
                              >
                              > "Samuel R. Neff" <blinex@newsgro up.nospam> wrote in message
                              > news:b4v911d8ia 0kjqhkb1pjcooui o87nh5gtd@4ax.c om...[color=green]
                              >>
                              >> The .NET recommended practice for creating singletons is to use a
                              >> single line..
                              >>
                              >> Public Shared ReadOnly Instance As New [ClassName]
                              >>
                              >> For example..
                              >>
                              >> Public NotInheritable Class DBCentral
                              >>
                              >> Public Shared ReadOnly Instance As New DBCentral
                              >>
                              >> Private Sub New()
                              >> End Sub
                              >>
                              >> End Class
                              >>
                              >>
                              >> Sam
                              >>
                              >>
                              >> On Thu, 17 Feb 2005 17:09:03 +0100, "Bob Powell [MVP]"
                              >> <bob@_spamkille r_bobpowell.net > wrote:
                              >>[color=darkred]
                              >>>You can create a class that uses the Singleton Pattern.
                              >>>Something like...
                              >>>
                              >>>public class DBCentral
                              >>>
                              >>>private _theInstance as DBCentral
                              >>>
                              >>>public readonly shared property TheInstance() as DBCentral
                              >>>get()
                              >>> if _theInstance is nothing then
                              >>> _theInstance=ne w DBCentral()
                              >>> end if
                              >>> return _theInstance
                              >>>end get
                              >>>end property
                              >>>
                              >>>end class
                              >>>
                              >>>I didn't check the exact syntax but you get the picture....[/color]
                              >>
                              >> B-Line is now hiring one VB.NET developer for
                              >> WinForms + WebServices position with ASPX in future.
                              >> Seaking mid to senior level developer. For
                              >> information or to apply e-mail sam_blinex_com.[/color]
                              >
                              >[/color]


                              Comment

                              Working...