Global Functions

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

    #1

    Global Functions

    I am trying to set up a function that connects to the database that I can
    then use gloablly. I set up a class called L3Global in which I have a
    function as follows:

    Public Function SetDBConnect()
    Dim ConnStrL3Produc er As String
    ConnStrL3Produc er = "User ID=UID;Tag with column collation when
    possible=False; Data Source=" & Chr(34) & "a.b.c.d" & Chr(34) &
    ";Password=PWD; "
    ConnStrL3Produc er = ConnStrL3Produc er & "Initial Catalog=L3;Use Procedure
    for Prepare=1;Auto Translate=True; Persist Security Info=True;Provi der="
    ConnStrL3Produc er = ConnStrL3Produc er & Chr(34) & "SQLOLEDB.1 " & Chr(34) &
    ";Workstati on ID=AUM;Use Encryption for Data=False;Pack et Size=4096"
    Dim ConnL3Producer As New OleDbConnection (ConnStrL3Produ cer)
    Return ConnL3Producer
    End Function

    Now I want to use this is all my forms (MDI). I set up
    Public L3G As New L3Global

    and then under the approporate button click I have:
    Dim ConnL3Producer As New OleDbConnection

    ConnL3Producer = L3G.SetDBConnec t

    The problem is, do I have to setup an instance of L3Global in each form?
    That defeats the purpose, because each one opens a new connection. How can
    I use this connection object in all my forms but open it only once? Ideally
    I would like to move as much of the intialization (creating the OLEDBAdapter
    etc) to the global routine.

    Thanx for any suggestions.
    --
    Anil Gupte
    Innovate with UsLeading the Way in Advanced Technology Solutions Discover state-of-the-art services in logistics, databases, and AI designed for evolving

    www.icinema.com


  • Michel Posseth  [MCP]

    #2
    Re: Global Functions


    Well i would not recomend a global connection aproach in .Net

    Because of the superb connection pool mechanism , you can now safely and
    without anny performance overhead create , use , and destroy
    especially the using statement is verry handy with this

    personally i would store the connection string in the config file

    regards

    Michel



    "Anil Gupte" <anil-list@icinema.co mschreef in bericht
    news:Ofv0ON0HHH A.1188@TK2MSFTN GP06.phx.gbl...
    >I am trying to set up a function that connects to the database that I can
    >then use gloablly. I set up a class called L3Global in which I have a
    >function as follows:
    >
    Public Function SetDBConnect()
    Dim ConnStrL3Produc er As String
    ConnStrL3Produc er = "User ID=UID;Tag with column collation when
    possible=False; Data Source=" & Chr(34) & "a.b.c.d" & Chr(34) &
    ";Password=PWD; "
    ConnStrL3Produc er = ConnStrL3Produc er & "Initial Catalog=L3;Use Procedure
    for Prepare=1;Auto Translate=True; Persist Security Info=True;Provi der="
    ConnStrL3Produc er = ConnStrL3Produc er & Chr(34) & "SQLOLEDB.1 " & Chr(34) &
    ";Workstati on ID=AUM;Use Encryption for Data=False;Pack et Size=4096"
    Dim ConnL3Producer As New OleDbConnection (ConnStrL3Produ cer)
    Return ConnL3Producer
    End Function
    >
    Now I want to use this is all my forms (MDI). I set up
    Public L3G As New L3Global
    >
    and then under the approporate button click I have:
    Dim ConnL3Producer As New OleDbConnection
    >
    ConnL3Producer = L3G.SetDBConnec t
    >
    The problem is, do I have to setup an instance of L3Global in each form?
    That defeats the purpose, because each one opens a new connection. How
    can I use this connection object in all my forms but open it only once?
    Ideally I would like to move as much of the intialization (creating the
    OLEDBAdapter etc) to the global routine.
    >
    Thanx for any suggestions.
    --
    Anil Gupte
    Innovate with UsLeading the Way in Advanced Technology Solutions Discover state-of-the-art services in logistics, databases, and AI designed for evolving

    www.icinema.com
    >
    >

    Comment

    • Anil Gupte

      #3
      Re: Global Functions

      Thanx for that tip, but please explain:
      "...especia lly the using statement is verry handy with this"

      and also
      "...persona lly i would store the connection string in the config file"

      I am a newbie at .Net and last used VB five years ago for a few months.

      --
      Anil Gupte
      Innovate with UsLeading the Way in Advanced Technology Solutions Discover state-of-the-art services in logistics, databases, and AI designed for evolving

      www.icinema.com

      "Michel Posseth [MCP]" <MSDN@posseth.c omwrote in message
      news:eqa5xv0HHH A.1280@TK2MSFTN GP04.phx.gbl...
      >
      Well i would not recomend a global connection aproach in .Net
      >
      Because of the superb connection pool mechanism , you can now safely and
      without anny performance overhead create , use , and destroy
      especially the using statement is verry handy with this
      >
      personally i would store the connection string in the config file
      >
      regards
      >
      Michel
      >
      >
      >
      "Anil Gupte" <anil-list@icinema.co mschreef in bericht
      news:Ofv0ON0HHH A.1188@TK2MSFTN GP06.phx.gbl...
      >>I am trying to set up a function that connects to the database that I can
      >>then use gloablly. I set up a class called L3Global in which I have a
      >>function as follows:
      >>
      >Public Function SetDBConnect()
      >Dim ConnStrL3Produc er As String
      >ConnStrL3Produ cer = "User ID=UID;Tag with column collation when
      >possible=False ;Data Source=" & Chr(34) & "a.b.c.d" & Chr(34) &
      >";Password=PWD ;"
      >ConnStrL3Produ cer = ConnStrL3Produc er & "Initial Catalog=L3;Use Procedure
      >for Prepare=1;Auto Translate=True; Persist Security Info=True;Provi der="
      >ConnStrL3Produ cer = ConnStrL3Produc er & Chr(34) & "SQLOLEDB.1 " & Chr(34)
      >& ";Workstati on ID=AUM;Use Encryption for Data=False;Pack et Size=4096"
      >Dim ConnL3Producer As New OleDbConnection (ConnStrL3Produ cer)
      >Return ConnL3Producer
      >End Function
      >>
      >Now I want to use this is all my forms (MDI). I set up
      >Public L3G As New L3Global
      >>
      >and then under the approporate button click I have:
      >Dim ConnL3Producer As New OleDbConnection
      >>
      >ConnL3Produc er = L3G.SetDBConnec t
      >>
      >The problem is, do I have to setup an instance of L3Global in each form?
      >That defeats the purpose, because each one opens a new connection. How
      >can I use this connection object in all my forms but open it only once?
      >Ideally I would like to move as much of the intialization (creating the
      >OLEDBAdapter etc) to the global routine.
      >>
      >Thanx for any suggestions.
      >--
      >Anil Gupte
      >www.keeninc.net
      >www.icinema.com
      >>
      >>
      >
      >

      Comment

      • Tom Leylan

        #4
        Re: Global Functions

        I can't know if you have but if you haven't heard of design patterns I think
        you should spend a little time reading about them. Here is an overview:
        http://en.wikipedia.org/wiki/Design_...mputer_science)

        Particularly you are describing a Singleton pattern (there is code here in
        C# which you can take a look at)



        "Anil Gupte" <anil-list@icinema.co mwrote in message
        news:Ofv0ON0HHH A.1188@TK2MSFTN GP06.phx.gbl...
        >I am trying to set up a function that connects to the database that I can
        >then use gloablly. I set up a class called L3Global in which I have a
        >function as follows:
        >
        Public Function SetDBConnect()
        Dim ConnStrL3Produc er As String
        ConnStrL3Produc er = "User ID=UID;Tag with column collation when
        possible=False; Data Source=" & Chr(34) & "a.b.c.d" & Chr(34) &
        ";Password=PWD; "
        ConnStrL3Produc er = ConnStrL3Produc er & "Initial Catalog=L3;Use Procedure
        for Prepare=1;Auto Translate=True; Persist Security Info=True;Provi der="
        ConnStrL3Produc er = ConnStrL3Produc er & Chr(34) & "SQLOLEDB.1 " & Chr(34) &
        ";Workstati on ID=AUM;Use Encryption for Data=False;Pack et Size=4096"
        Dim ConnL3Producer As New OleDbConnection (ConnStrL3Produ cer)
        Return ConnL3Producer
        End Function
        >
        Now I want to use this is all my forms (MDI). I set up
        Public L3G As New L3Global
        >
        and then under the approporate button click I have:
        Dim ConnL3Producer As New OleDbConnection
        >
        ConnL3Producer = L3G.SetDBConnec t
        >
        The problem is, do I have to setup an instance of L3Global in each form?
        That defeats the purpose, because each one opens a new connection. How
        can I use this connection object in all my forms but open it only once?
        Ideally I would like to move as much of the intialization (creating the
        OLEDBAdapter etc) to the global routine.
        >
        Thanx for any suggestions.
        --
        Anil Gupte
        Innovate with UsLeading the Way in Advanced Technology Solutions Discover state-of-the-art services in logistics, databases, and AI designed for evolving

        www.icinema.com
        >
        >

        Comment

        • Anil Gupte

          #5
          Re: Global Functions

          Hmm, I must be in a parallel universe. Just kidding of course, but I did
          not follow most of what I read on those links, and I am not that much of a
          programmer (nor do I ever hope to be). I am just building a prototype of
          software that I want real programmers to write.

          Anyway, if you have seen specific tutorials relating to this, please do let
          me know.

          Anyway Thanx,
          --
          Anil Gupte
          Innovate with UsLeading the Way in Advanced Technology Solutions Discover state-of-the-art services in logistics, databases, and AI designed for evolving

          www.icinema.com

          "Tom Leylan" <gee@iamtiredof spam.comwrote in message
          news:eQJth94HHH A.420@TK2MSFTNG P06.phx.gbl...
          >I can't know if you have but if you haven't heard of design patterns I
          >think you should spend a little time reading about them. Here is an
          >overview: http://en.wikipedia.org/wiki/Design_...mputer_science)
          >
          Particularly you are describing a Singleton pattern (there is code here in
          C# which you can take a look at)

          >
          >
          "Anil Gupte" <anil-list@icinema.co mwrote in message
          news:Ofv0ON0HHH A.1188@TK2MSFTN GP06.phx.gbl...
          >>I am trying to set up a function that connects to the database that I can
          >>then use gloablly. I set up a class called L3Global in which I have a
          >>function as follows:
          >>
          >Public Function SetDBConnect()
          >Dim ConnStrL3Produc er As String
          >ConnStrL3Produ cer = "User ID=UID;Tag with column collation when
          >possible=False ;Data Source=" & Chr(34) & "a.b.c.d" & Chr(34) &
          >";Password=PWD ;"
          >ConnStrL3Produ cer = ConnStrL3Produc er & "Initial Catalog=L3;Use Procedure
          >for Prepare=1;Auto Translate=True; Persist Security Info=True;Provi der="
          >ConnStrL3Produ cer = ConnStrL3Produc er & Chr(34) & "SQLOLEDB.1 " & Chr(34)
          >& ";Workstati on ID=AUM;Use Encryption for Data=False;Pack et Size=4096"
          >Dim ConnL3Producer As New OleDbConnection (ConnStrL3Produ cer)
          >Return ConnL3Producer
          >End Function
          >>
          >Now I want to use this is all my forms (MDI). I set up
          >Public L3G As New L3Global
          >>
          >and then under the approporate button click I have:
          >Dim ConnL3Producer As New OleDbConnection
          >>
          >ConnL3Produc er = L3G.SetDBConnec t
          >>
          >The problem is, do I have to setup an instance of L3Global in each form?
          >That defeats the purpose, because each one opens a new connection. How
          >can I use this connection object in all my forms but open it only once?
          >Ideally I would like to move as much of the intialization (creating the
          >OLEDBAdapter etc) to the global routine.
          >>
          >Thanx for any suggestions.
          >--
          >Anil Gupte
          >www.keeninc.net
          >www.icinema.com
          >>
          >>
          >
          >

          Comment

          • Tom Leylan

            #6
            Re: Global Functions

            A fairly common problem developers run into is having non-developers explain
            how to develop applications :-) I'll suggest that you ordinarily wouldn't
            need to explain to a developer your preference for a single connection,
            globally accessible from anywhere in the application. The developer(s) can
            figure out a) if that is a good idea and b) how best to implement it.

            Design patterns have been around for a very long time but (more or less)
            recently were given names and categorized. There are many books and
            thousands of articles on the subject but in a nutshell most of what
            developers do isn't "new" and whether we are instantiating a customer,
            inventory item, baseball score or anything else the process is the same. I
            don't mean that the code exists I mean the "pattern" that the code will
            follow exists. Your house may look completely different from another but it
            will have a foundation, walls and a roof, those are givens.

            Bottom line is when you have some time consider reading an introduction
            articles on design patterns. If it does nothing else you may have the "aha"
            moment when you find something you've been trying to explain to a developer
            actually has a name and everybody already knows about it.

            Also if you create a singleton class your "I don't want two instances"
            problem goes away. There may be a dozen other tricky ways to do it but ask
            yourself what is the advantage to not using a proven design that everybody
            else is using?

            Take care,
            Tom


            "Anil Gupte" <anil-list@icinema.co mwrote in message
            news:%23pT5Xg5H HHA.1276@TK2MSF TNGP04.phx.gbl. ..
            Hmm, I must be in a parallel universe. Just kidding of course, but I did
            not follow most of what I read on those links, and I am not that much of a
            programmer (nor do I ever hope to be). I am just building a prototype of
            software that I want real programmers to write.
            >
            Anyway, if you have seen specific tutorials relating to this, please do
            let me know.
            >
            Anyway Thanx,
            --
            Anil Gupte
            Innovate with UsLeading the Way in Advanced Technology Solutions Discover state-of-the-art services in logistics, databases, and AI designed for evolving

            www.icinema.com
            >
            "Tom Leylan" <gee@iamtiredof spam.comwrote in message
            news:eQJth94HHH A.420@TK2MSFTNG P06.phx.gbl...
            >>I can't know if you have but if you haven't heard of design patterns I
            >>think you should spend a little time reading about them. Here is an
            >>overview: http://en.wikipedia.org/wiki/Design_...mputer_science)
            >>
            >Particularly you are describing a Singleton pattern (there is code here
            >in C# which you can take a look at)
            >http://en.wikipedia.org/wiki/Singleton_pattern
            >>
            >>
            >"Anil Gupte" <anil-list@icinema.co mwrote in message
            >news:Ofv0ON0HH HA.1188@TK2MSFT NGP06.phx.gbl.. .
            >>>I am trying to set up a function that connects to the database that I can
            >>>then use gloablly. I set up a class called L3Global in which I have a
            >>>function as follows:
            >>>
            >>Public Function SetDBConnect()
            >>Dim ConnStrL3Produc er As String
            >>ConnStrL3Prod ucer = "User ID=UID;Tag with column collation when
            >>possible=Fals e;Data Source=" & Chr(34) & "a.b.c.d" & Chr(34) &
            >>";Password=PW D;"
            >>ConnStrL3Prod ucer = ConnStrL3Produc er & "Initial Catalog=L3;Use
            >>Procedure for Prepare=1;Auto Translate=True; Persist Security
            >>Info=True;Pro vider="
            >>ConnStrL3Prod ucer = ConnStrL3Produc er & Chr(34) & "SQLOLEDB.1 " & Chr(34)
            >>& ";Workstati on ID=AUM;Use Encryption for Data=False;Pack et Size=4096"
            >>Dim ConnL3Producer As New OleDbConnection (ConnStrL3Produ cer)
            >>Return ConnL3Producer
            >>End Function
            >>>
            >>Now I want to use this is all my forms (MDI). I set up
            >>Public L3G As New L3Global
            >>>
            >>and then under the approporate button click I have:
            >>Dim ConnL3Producer As New OleDbConnection
            >>>
            >>ConnL3Produce r = L3G.SetDBConnec t
            >>>
            >>The problem is, do I have to setup an instance of L3Global in each form?
            >>That defeats the purpose, because each one opens a new connection. How
            >>can I use this connection object in all my forms but open it only once?
            >>Ideally I would like to move as much of the intialization (creating the
            >>OLEDBAdapte r etc) to the global routine.
            >>>
            >>Thanx for any suggestions.
            >>--
            >>Anil Gupte
            >>www.keeninc.net
            >>www.icinema.com
            >>>
            >>>
            >>
            >>
            >
            >

            Comment

            • Branco Medeiros

              #7
              Re: Global Functions

              Anil Gupte wrote:
              I am trying to set up a function that connects to the database that I can
              then use gloablly. I set up a class called L3Global in which I have a
              function as follows:
              Public Function SetDBConnect()
              <snip>
              Now I want to use this is all my forms (MDI). I set up
              Public L3G As New L3Global
              >
              and then under the approporate button click I have:
              Dim ConnL3Producer As New OleDbConnection
              >
              ConnL3Producer = L3G.SetDBConnec t
              >
              The problem is, do I have to setup an instance of L3Global in each form?
              That defeats the purpose, because each one opens a new connection. How can
              I use this connection object in all my forms but open it only once? Ideally
              I would like to move as much of the intialization (creating the OLEDBAdapter
              etc) to the global routine.
              <snip>

              I guess you can declare a Module, instead of a class:

              <aircode>
              Module L3Global
              Private mConnection As OleDBConnection

              Public Function ConnectDB() As OleDBConnection
              If mConnection Is Nothing Then
              Dim ConText As String = "Your connection string"
              mCOnnection = New OleDBConnectins (ConText)
              End If
              Return mConnection
              End Function
              End Module
              </aircode>

              And then, in your Forms you'd have:

              Dim ConnL3Producer _
              As OleDbConnection = L3Global.Connec tDB()

              Improvise over that.

              Regards,

              Branco.

              Comment

              • Stephany Young

                #8
                Re: Global Functions

                Because you are not that much of a programmer (nor do you ever hope to be),
                I strongly suggest that you do NOT build a prototype of software that you
                want real programmers to write.

                Instead, write a specification document that describes, at a fairly high
                level, the purpose of the software, what the 'inputs' are, what the
                'outputs' must be and what the expected outcomes are. Include details of
                business rules and pictures of what various forms and reports should look
                like. If there are any aspects of the specification that are immutable then
                make those aspect clear and unambiguous. Also include details of what
                technologies may or may not be used.

                You will find that a real programmer, when presented with such a document,
                will have no problems figuring out the best way to implement the design and
                will be more productive than having to work from a protoype developed by
                someone who doesn't really know what they are doing.


                "Anil Gupte" <anil-list@icinema.co mwrote in message
                news:%23pT5Xg5H HHA.1276@TK2MSF TNGP04.phx.gbl. ..
                Hmm, I must be in a parallel universe. Just kidding of course, but I did
                not follow most of what I read on those links, and I am not that much of a
                programmer (nor do I ever hope to be). I am just building a prototype of
                software that I want real programmers to write.
                >
                Anyway, if you have seen specific tutorials relating to this, please do
                let me know.
                >
                Anyway Thanx,
                --
                Anil Gupte
                Innovate with UsLeading the Way in Advanced Technology Solutions Discover state-of-the-art services in logistics, databases, and AI designed for evolving

                www.icinema.com
                >
                "Tom Leylan" <gee@iamtiredof spam.comwrote in message
                news:eQJth94HHH A.420@TK2MSFTNG P06.phx.gbl...
                >>I can't know if you have but if you haven't heard of design patterns I
                >>think you should spend a little time reading about them. Here is an
                >>overview: http://en.wikipedia.org/wiki/Design_...mputer_science)
                >>
                >Particularly you are describing a Singleton pattern (there is code here
                >in C# which you can take a look at)
                >http://en.wikipedia.org/wiki/Singleton_pattern
                >>
                >>
                >"Anil Gupte" <anil-list@icinema.co mwrote in message
                >news:Ofv0ON0HH HA.1188@TK2MSFT NGP06.phx.gbl.. .
                >>>I am trying to set up a function that connects to the database that I can
                >>>then use gloablly. I set up a class called L3Global in which I have a
                >>>function as follows:
                >>>
                >>Public Function SetDBConnect()
                >>Dim ConnStrL3Produc er As String
                >>ConnStrL3Prod ucer = "User ID=UID;Tag with column collation when
                >>possible=Fals e;Data Source=" & Chr(34) & "a.b.c.d" & Chr(34) &
                >>";Password=PW D;"
                >>ConnStrL3Prod ucer = ConnStrL3Produc er & "Initial Catalog=L3;Use
                >>Procedure for Prepare=1;Auto Translate=True; Persist Security
                >>Info=True;Pro vider="
                >>ConnStrL3Prod ucer = ConnStrL3Produc er & Chr(34) & "SQLOLEDB.1 " & Chr(34)
                >>& ";Workstati on ID=AUM;Use Encryption for Data=False;Pack et Size=4096"
                >>Dim ConnL3Producer As New OleDbConnection (ConnStrL3Produ cer)
                >>Return ConnL3Producer
                >>End Function
                >>>
                >>Now I want to use this is all my forms (MDI). I set up
                >>Public L3G As New L3Global
                >>>
                >>and then under the approporate button click I have:
                >>Dim ConnL3Producer As New OleDbConnection
                >>>
                >>ConnL3Produce r = L3G.SetDBConnec t
                >>>
                >>The problem is, do I have to setup an instance of L3Global in each form?
                >>That defeats the purpose, because each one opens a new connection. How
                >>can I use this connection object in all my forms but open it only once?
                >>Ideally I would like to move as much of the intialization (creating the
                >>OLEDBAdapte r etc) to the global routine.
                >>>
                >>Thanx for any suggestions.
                >>--
                >>Anil Gupte
                >>www.keeninc.net
                >>www.icinema.com
                >>>
                >>>
                >>
                >>
                >
                >

                Comment

                • Anil Gupte

                  #9
                  Re: Global Functions

                  Thanx, that will get me started. Do you happen to know any place where I
                  can read more about when to use a Module and when to use a class?

                  Thanx again,
                  --
                  Anil Gupte
                  Innovate with UsLeading the Way in Advanced Technology Solutions Discover state-of-the-art services in logistics, databases, and AI designed for evolving

                  www.icinema.com

                  "Branco Medeiros" <branco.medeiro s@gmail.comwrot e in message
                  news:1166131605 .256508.207250@ n67g2000cwd.goo glegroups.com.. .
                  Anil Gupte wrote:
                  >I am trying to set up a function that connects to the database that I can
                  >then use gloablly. I set up a class called L3Global in which I have a
                  >function as follows:
                  >
                  >Public Function SetDBConnect()
                  <snip>
                  >
                  >Now I want to use this is all my forms (MDI). I set up
                  >Public L3G As New L3Global
                  >>
                  >and then under the approporate button click I have:
                  >Dim ConnL3Producer As New OleDbConnection
                  >>
                  >ConnL3Produc er = L3G.SetDBConnec t
                  >>
                  >The problem is, do I have to setup an instance of L3Global in each form?
                  >That defeats the purpose, because each one opens a new connection. How
                  >can
                  >I use this connection object in all my forms but open it only once?
                  >Ideally
                  >I would like to move as much of the intialization (creating the
                  >OLEDBAdapter
                  >etc) to the global routine.
                  <snip>
                  >
                  I guess you can declare a Module, instead of a class:
                  >
                  <aircode>
                  Module L3Global
                  Private mConnection As OleDBConnection
                  >
                  Public Function ConnectDB() As OleDBConnection
                  If mConnection Is Nothing Then
                  Dim ConText As String = "Your connection string"
                  mCOnnection = New OleDBConnectins (ConText)
                  End If
                  Return mConnection
                  End Function
                  End Module
                  </aircode>
                  >
                  And then, in your Forms you'd have:
                  >
                  Dim ConnL3Producer _
                  As OleDbConnection = L3Global.Connec tDB()
                  >
                  Improvise over that.
                  >
                  Regards,
                  >
                  Branco.
                  >

                  Comment

                  • Tom Leylan

                    #10
                    Re: Global Functions

                    Anil: Yes you can read it right here :-) Never use a module!

                    VB.Net is an object-oriented language and by definition that should lead to
                    object-oriented solutions. If there is no OOP solution (and that would seem
                    unlikely given apps written in SmallTalk, C++, Java, C# and all the other
                    OOP-based languages) then one might find themselves forced into having to
                    resort to a "hack". Take my word for it don't start with the "hack" or you
                    will never stop applying hacks. People will stare at your code and giggle
                    behind your back...

                    What you are describing isn't a "module" you are describing a Singleton
                    Class. If you want to read something perhaps about the Singleton Class
                    would be a good start.

                    Try it you may like it.

                    "Anil Gupte" <anil-list@icinema.co mwrote in message
                    news:OCFPtkBIHH A.3676@TK2MSFTN GP03.phx.gbl...
                    Thanx, that will get me started. Do you happen to know any place where I
                    can read more about when to use a Module and when to use a class?
                    >
                    Thanx again,
                    --
                    Anil Gupte
                    Innovate with UsLeading the Way in Advanced Technology Solutions Discover state-of-the-art services in logistics, databases, and AI designed for evolving

                    www.icinema.com
                    >
                    "Branco Medeiros" <branco.medeiro s@gmail.comwrot e in message
                    news:1166131605 .256508.207250@ n67g2000cwd.goo glegroups.com.. .
                    >Anil Gupte wrote:
                    >>I am trying to set up a function that connects to the database that I
                    >>can
                    >>then use gloablly. I set up a class called L3Global in which I have a
                    >>function as follows:
                    >>
                    >>Public Function SetDBConnect()
                    ><snip>
                    >>
                    >>Now I want to use this is all my forms (MDI). I set up
                    >>Public L3G As New L3Global
                    >>>
                    >>and then under the approporate button click I have:
                    >>Dim ConnL3Producer As New OleDbConnection
                    >>>
                    >>ConnL3Produce r = L3G.SetDBConnec t
                    >>>
                    >>The problem is, do I have to setup an instance of L3Global in each form?
                    >>That defeats the purpose, because each one opens a new connection. How
                    >>can
                    >>I use this connection object in all my forms but open it only once?
                    >>Ideally
                    >>I would like to move as much of the intialization (creating the
                    >>OLEDBAdapte r
                    >>etc) to the global routine.
                    ><snip>
                    >>
                    >I guess you can declare a Module, instead of a class:
                    >>
                    ><aircode>
                    > Module L3Global
                    > Private mConnection As OleDBConnection
                    >>
                    > Public Function ConnectDB() As OleDBConnection
                    > If mConnection Is Nothing Then
                    > Dim ConText As String = "Your connection string"
                    > mCOnnection = New OleDBConnectins (ConText)
                    > End If
                    > Return mConnection
                    > End Function
                    > End Module
                    ></aircode>
                    >>
                    >And then, in your Forms you'd have:
                    >>
                    > Dim ConnL3Producer _
                    > As OleDbConnection = L3Global.Connec tDB()
                    >>
                    >Improvise over that.
                    >>
                    >Regards,
                    >>
                    >Branco.
                    >>
                    >
                    >

                    Comment

                    • =?Utf-8?B?RGVubmlz?=

                      #11
                      Re: Global Functions

                      Funny, I always thought a module was a Singleton Class!
                      --
                      Dennis in Houston


                      "Tom Leylan" wrote:
                      Anil: Yes you can read it right here :-) Never use a module!
                      >
                      VB.Net is an object-oriented language and by definition that should lead to
                      object-oriented solutions. If there is no OOP solution (and that would seem
                      unlikely given apps written in SmallTalk, C++, Java, C# and all the other
                      OOP-based languages) then one might find themselves forced into having to
                      resort to a "hack". Take my word for it don't start with the "hack" or you
                      will never stop applying hacks. People will stare at your code and giggle
                      behind your back...
                      >
                      What you are describing isn't a "module" you are describing a Singleton
                      Class. If you want to read something perhaps about the Singleton Class
                      would be a good start.
                      >
                      Try it you may like it.
                      >
                      "Anil Gupte" <anil-list@icinema.co mwrote in message
                      news:OCFPtkBIHH A.3676@TK2MSFTN GP03.phx.gbl...
                      Thanx, that will get me started. Do you happen to know any place where I
                      can read more about when to use a Module and when to use a class?

                      Thanx again,
                      --
                      Anil Gupte
                      Innovate with UsLeading the Way in Advanced Technology Solutions Discover state-of-the-art services in logistics, databases, and AI designed for evolving

                      www.icinema.com

                      "Branco Medeiros" <branco.medeiro s@gmail.comwrot e in message
                      news:1166131605 .256508.207250@ n67g2000cwd.goo glegroups.com.. .
                      Anil Gupte wrote:
                      >I am trying to set up a function that connects to the database that I
                      >can
                      >then use gloablly. I set up a class called L3Global in which I have a
                      >function as follows:
                      >
                      >Public Function SetDBConnect()
                      <snip>
                      >
                      >Now I want to use this is all my forms (MDI). I set up
                      >Public L3G As New L3Global
                      >>
                      >and then under the approporate button click I have:
                      >Dim ConnL3Producer As New OleDbConnection
                      >>
                      >ConnL3Produc er = L3G.SetDBConnec t
                      >>
                      >The problem is, do I have to setup an instance of L3Global in each form?
                      >That defeats the purpose, because each one opens a new connection. How
                      >can
                      >I use this connection object in all my forms but open it only once?
                      >Ideally
                      >I would like to move as much of the intialization (creating the
                      >OLEDBAdapter
                      >etc) to the global routine.
                      <snip>
                      >
                      I guess you can declare a Module, instead of a class:
                      >
                      <aircode>
                      Module L3Global
                      Private mConnection As OleDBConnection
                      >
                      Public Function ConnectDB() As OleDBConnection
                      If mConnection Is Nothing Then
                      Dim ConText As String = "Your connection string"
                      mCOnnection = New OleDBConnectins (ConText)
                      End If
                      Return mConnection
                      End Function
                      End Module
                      </aircode>
                      >
                      And then, in your Forms you'd have:
                      >
                      Dim ConnL3Producer _
                      As OleDbConnection = L3Global.Connec tDB()
                      >
                      Improvise over that.
                      >
                      Regards,
                      >
                      Branco.
                      >
                      >
                      >
                      >

                      Comment

                      • Cor Ligthert [MVP]

                        #12
                        Re: Global Functions

                        Tom,

                        I hope to disappoint you, however it seems that C# programmers cannot stand
                        the word "module".

                        However a singleton C# class is as far as I know nothing more than a module,
                        which residence at the stack.

                        Beside the slight misbehaviour that a variable declared in a module can be
                        called in VB.Net without its module name, is in my idea the module much
                        nicer for declaring and using shared parts in a program.

                        I would never write, "Never use a module", I agree with you when it becomes.
                        "Try to avoid a module, static class, singleton, shared class or whatever
                        you name them."

                        Cor

                        "Tom Leylan" <tleylan@nospam .netschreef in bericht
                        news:%23GH2CULI HHA.3424@TK2MSF TNGP02.phx.gbl. ..
                        Anil: Yes you can read it right here :-) Never use a module!
                        >
                        VB.Net is an object-oriented language and by definition that should lead
                        to object-oriented solutions. If there is no OOP solution (and that would
                        seem unlikely given apps written in SmallTalk, C++, Java, C# and all the
                        other OOP-based languages) then one might find themselves forced into
                        having to resort to a "hack". Take my word for it don't start with the
                        "hack" or you will never stop applying hacks. People will stare at your
                        code and giggle behind your back...
                        >
                        What you are describing isn't a "module" you are describing a Singleton
                        Class. If you want to read something perhaps about the Singleton Class
                        would be a good start.
                        >
                        Try it you may like it.
                        >
                        "Anil Gupte" <anil-list@icinema.co mwrote in message
                        news:OCFPtkBIHH A.3676@TK2MSFTN GP03.phx.gbl...
                        >Thanx, that will get me started. Do you happen to know any place where I
                        >can read more about when to use a Module and when to use a class?
                        >>
                        >Thanx again,
                        >--
                        >Anil Gupte
                        >www.keeninc.net
                        >www.icinema.com
                        >>
                        >"Branco Medeiros" <branco.medeiro s@gmail.comwrot e in message
                        >news:116613160 5.256508.207250 @n67g2000cwd.go oglegroups.com. ..
                        >>Anil Gupte wrote:
                        >>>I am trying to set up a function that connects to the database that I
                        >>>can
                        >>>then use gloablly. I set up a class called L3Global in which I have a
                        >>>function as follows:
                        >>>
                        >>>Public Function SetDBConnect()
                        >><snip>
                        >>>
                        >>>Now I want to use this is all my forms (MDI). I set up
                        >>>Public L3G As New L3Global
                        >>>>
                        >>>and then under the approporate button click I have:
                        >>>Dim ConnL3Producer As New OleDbConnection
                        >>>>
                        >>>ConnL3Produc er = L3G.SetDBConnec t
                        >>>>
                        >>>The problem is, do I have to setup an instance of L3Global in each
                        >>>form?
                        >>>That defeats the purpose, because each one opens a new connection. How
                        >>>can
                        >>>I use this connection object in all my forms but open it only once?
                        >>>Ideally
                        >>>I would like to move as much of the intialization (creating the
                        >>>OLEDBAdapt er
                        >>>etc) to the global routine.
                        >><snip>
                        >>>
                        >>I guess you can declare a Module, instead of a class:
                        >>>
                        >><aircode>
                        >> Module L3Global
                        >> Private mConnection As OleDBConnection
                        >>>
                        >> Public Function ConnectDB() As OleDBConnection
                        >> If mConnection Is Nothing Then
                        >> Dim ConText As String = "Your connection string"
                        >> mCOnnection = New OleDBConnectins (ConText)
                        >> End If
                        >> Return mConnection
                        >> End Function
                        >> End Module
                        >></aircode>
                        >>>
                        >>And then, in your Forms you'd have:
                        >>>
                        >> Dim ConnL3Producer _
                        >> As OleDbConnection = L3Global.Connec tDB()
                        >>>
                        >>Improvise over that.
                        >>>
                        >>Regards,
                        >>>
                        >>Branco.
                        >>>
                        >>
                        >>
                        >
                        >

                        Comment

                        • Tom Leylan

                          #13
                          Re: Global Functions

                          Hi Cor (and Dennis):

                          A quick search of newsgroup messages reveals that similar conversations have
                          taken place before. One in particular involved Kevin Spencer explaining why
                          the VB Module exists (backwards compatibility) and offering his opinion that
                          it should have been removed. I agree with him.

                          While it may constitute "a class with static properties and no constructor"
                          I see no reason to provide a second syntax for creating such a thing. Why
                          not a third or fourth syntax as well? I really don't want to rehash all the
                          points because for every point made there will be a counterpoint and we
                          simply duplicate countless other threads.

                          I would hesitate to describe it as a Singleton class however. There is in
                          fact a single object in a Singleton class and all properties do not need to
                          be static. The instantiation of that object can be delayed until "first
                          use". One can obtain a reference to the singleton object which can be
                          passed as a parameter to other methods. If these things can be done with
                          the VB module that's cool but again I ask why do we need two ways to obtain
                          identical results? Furthermore we cannot create a singleton class using
                          Module syntax subclassed from an arbitrary class right? So it is not only a
                          "dupe" it is a limited use duplication.

                          I believe it comes down to clarity of design. I can't explain it better but
                          we don't need a language to implement every person's favorite way to do
                          something. If anybody wants "module" functionality couldn't they simply
                          declare a class in the way that the module does and get it? There it is,
                          available to anybody who wants it, not part of the official language syntax,
                          not misinterpreted as being a VB feature, etc.

                          Cor, you're misinterpreting or simply guessing. It isn't that C#
                          programmers can or cannot stand the word module. A C programmer doesn't
                          think more clearly than a VB programmer, a dBASE programmer doesn't rank
                          somewhere in a good programmer chart. There are good and bad (shall we call
                          them neat and sloppy) programmers using every language. There is often a
                          division between people who love having "&" and "+" available to concatenate
                          strings and those who ask, why two operators? Some may find two ways "a
                          brilliant design" while others will say it creates unnecessary ambiguity. I
                          wouldn't say that C# developers find it odd and VB developers find it cool.

                          The VB module doesn't add anything to the language (that I am aware of) and
                          as such it should be removed from the language.


                          "Cor Ligthert [MVP]" <notmyfirstname @planet.nlwrote in message
                          news:%233D%23WL hIHHA.2456@TK2M SFTNGP06.phx.gb l...
                          Tom,
                          >
                          I hope to disappoint you, however it seems that C# programmers cannot
                          stand the word "module".
                          >
                          However a singleton C# class is as far as I know nothing more than a
                          module, which residence at the stack.
                          >
                          Beside the slight misbehaviour that a variable declared in a module can be
                          called in VB.Net without its module name, is in my idea the module much
                          nicer for declaring and using shared parts in a program.
                          >
                          I would never write, "Never use a module", I agree with you when it
                          becomes. "Try to avoid a module, static class, singleton, shared class or
                          whatever you name them."
                          >
                          Cor
                          >
                          "Tom Leylan" <tleylan@nospam .netschreef in bericht
                          news:%23GH2CULI HHA.3424@TK2MSF TNGP02.phx.gbl. ..
                          >Anil: Yes you can read it right here :-) Never use a module!
                          >>
                          >VB.Net is an object-oriented language and by definition that should lead
                          >to object-oriented solutions. If there is no OOP solution (and that
                          >would seem unlikely given apps written in SmallTalk, C++, Java, C# and
                          >all the other OOP-based languages) then one might find themselves forced
                          >into having to resort to a "hack". Take my word for it don't start with
                          >the "hack" or you will never stop applying hacks. People will stare at
                          >your code and giggle behind your back...
                          >>
                          >What you are describing isn't a "module" you are describing a Singleton
                          >Class. If you want to read something perhaps about the Singleton Class
                          >would be a good start.
                          >>
                          >Try it you may like it.
                          >>
                          >"Anil Gupte" <anil-list@icinema.co mwrote in message
                          >news:OCFPtkBIH HA.3676@TK2MSFT NGP03.phx.gbl.. .
                          >>Thanx, that will get me started. Do you happen to know any place where
                          >>I can read more about when to use a Module and when to use a class?
                          >>>
                          >>Thanx again,
                          >>--
                          >>Anil Gupte
                          >>www.keeninc.net
                          >>www.icinema.com
                          >>>
                          >>"Branco Medeiros" <branco.medeiro s@gmail.comwrot e in message
                          >>news:11661316 05.256508.20725 0@n67g2000cwd.g ooglegroups.com ...
                          >>>Anil Gupte wrote:
                          >>>>I am trying to set up a function that connects to the database that I
                          >>>>can
                          >>>>then use gloablly. I set up a class called L3Global in which I have a
                          >>>>function as follows:
                          >>>>
                          >>>>Public Function SetDBConnect()
                          >>><snip>
                          >>>>
                          >>>>Now I want to use this is all my forms (MDI). I set up
                          >>>>Public L3G As New L3Global
                          >>>>>
                          >>>>and then under the approporate button click I have:
                          >>>>Dim ConnL3Producer As New OleDbConnection
                          >>>>>
                          >>>>ConnL3Produ cer = L3G.SetDBConnec t
                          >>>>>
                          >>>>The problem is, do I have to setup an instance of L3Global in each
                          >>>>form?
                          >>>>That defeats the purpose, because each one opens a new connection.
                          >>>>How can
                          >>>>I use this connection object in all my forms but open it only once?
                          >>>>Ideally
                          >>>>I would like to move as much of the intialization (creating the
                          >>>>OLEDBAdapte r
                          >>>>etc) to the global routine.
                          >>><snip>
                          >>>>
                          >>>I guess you can declare a Module, instead of a class:
                          >>>>
                          >>><aircode>
                          >>> Module L3Global
                          >>> Private mConnection As OleDBConnection
                          >>>>
                          >>> Public Function ConnectDB() As OleDBConnection
                          >>> If mConnection Is Nothing Then
                          >>> Dim ConText As String = "Your connection string"
                          >>> mCOnnection = New OleDBConnectins (ConText)
                          >>> End If
                          >>> Return mConnection
                          >>> End Function
                          >>> End Module
                          >>></aircode>
                          >>>>
                          >>>And then, in your Forms you'd have:
                          >>>>
                          >>> Dim ConnL3Producer _
                          >>> As OleDbConnection = L3Global.Connec tDB()
                          >>>>
                          >>>Improvise over that.
                          >>>>
                          >>>Regards,
                          >>>>
                          >>>Branco.
                          >>>>
                          >>>
                          >>>
                          >>
                          >>
                          >
                          >

                          Comment

                          • Cor Ligthert [MVP]

                            #14
                            Re: Global Functions

                            Tom,

                            A class in OOP has to be instanced, (you wrote it in my idea yourself) so
                            that it can sit on the heap. You are true that I am guessing that a
                            Singleton Class is doing the same as a so called not official existing
                            Static Class in C# does, I could not yet find it where it residence, so I
                            should not have written that. There are differences in with the VB Module
                            and the Singleton, however as much as the behaviour between + and $
                            concationation in VB.

                            I like however the name "Module". It makes it for me very clear, that a
                            module is not a class, it is a part of the main object as it is fixed in the
                            program. An instances object however sits on the managed heap and will be as
                            as soon as that is possible removed. I find this name Module therefore nicer
                            than any Shared/Static class, which is in fact no class in the OOP meaning.

                            It is possible to write a Module in the same way as a class, and be than not
                            forced to use the Shared keyword. In a very short discussion between Kevin
                            Spencer and me about this (with a very small sample from me) he somewhere
                            wrote a relative short while ago something in these newsgroups as. "You are
                            right Cor, I did not know this behaviour from the Module". However, to
                            search special messages on Google from either Kevin or me is not easy.

                            Cor


                            "Tom Leylan" <tleylan@nospam .netschreef in bericht
                            news:eZiI8qhIHH A.2232@TK2MSFTN GP02.phx.gbl...
                            Hi Cor (and Dennis):
                            >
                            A quick search of newsgroup messages reveals that similar conversations
                            have taken place before. One in particular involved Kevin Spencer
                            explaining why the VB Module exists (backwards compatibility) and offering
                            his opinion that it should have been removed. I agree with him.
                            >
                            While it may constitute "a class with static properties and no
                            constructor" I see no reason to provide a second syntax for creating such
                            a thing. Why not a third or fourth syntax as well? I really don't want
                            to rehash all the points because for every point made there will be a
                            counterpoint and we simply duplicate countless other threads.
                            >
                            I would hesitate to describe it as a Singleton class however. There is in
                            fact a single object in a Singleton class and all properties do not need
                            to be static. The instantiation of that object can be delayed until
                            "first use". One can obtain a reference to the singleton object which can
                            be passed as a parameter to other methods. If these things can be done
                            with the VB module that's cool but again I ask why do we need two ways to
                            obtain identical results? Furthermore we cannot create a singleton class
                            using Module syntax subclassed from an arbitrary class right? So it is
                            not only a "dupe" it is a limited use duplication.
                            >
                            I believe it comes down to clarity of design. I can't explain it better
                            but we don't need a language to implement every person's favorite way to
                            do something. If anybody wants "module" functionality couldn't they
                            simply declare a class in the way that the module does and get it? There
                            it is, available to anybody who wants it, not part of the official
                            language syntax, not misinterpreted as being a VB feature, etc.
                            >
                            Cor, you're misinterpreting or simply guessing. It isn't that C#
                            programmers can or cannot stand the word module. A C programmer doesn't
                            think more clearly than a VB programmer, a dBASE programmer doesn't rank
                            somewhere in a good programmer chart. There are good and bad (shall we
                            call them neat and sloppy) programmers using every language. There is
                            often a division between people who love having "&" and "+" available to
                            concatenate strings and those who ask, why two operators? Some may find
                            two ways "a brilliant design" while others will say it creates unnecessary
                            ambiguity. I wouldn't say that C# developers find it odd and VB
                            developers find it cool.
                            >
                            The VB module doesn't add anything to the language (that I am aware of)
                            and as such it should be removed from the language.
                            >
                            >
                            "Cor Ligthert [MVP]" <notmyfirstname @planet.nlwrote in message
                            news:%233D%23WL hIHHA.2456@TK2M SFTNGP06.phx.gb l...
                            >Tom,
                            >>
                            >I hope to disappoint you, however it seems that C# programmers cannot
                            >stand the word "module".
                            >>
                            >However a singleton C# class is as far as I know nothing more than a
                            >module, which residence at the stack.
                            >>
                            >Beside the slight misbehaviour that a variable declared in a module can
                            >be called in VB.Net without its module name, is in my idea the module
                            >much nicer for declaring and using shared parts in a program.
                            >>
                            >I would never write, "Never use a module", I agree with you when it
                            >becomes. "Try to avoid a module, static class, singleton, shared class or
                            >whatever you name them."
                            >>
                            >Cor
                            >>
                            >"Tom Leylan" <tleylan@nospam .netschreef in bericht
                            >news:%23GH2CUL IHHA.3424@TK2MS FTNGP02.phx.gbl ...
                            >>Anil: Yes you can read it right here :-) Never use a module!
                            >>>
                            >>VB.Net is an object-oriented language and by definition that should lead
                            >>to object-oriented solutions. If there is no OOP solution (and that
                            >>would seem unlikely given apps written in SmallTalk, C++, Java, C# and
                            >>all the other OOP-based languages) then one might find themselves forced
                            >>into having to resort to a "hack". Take my word for it don't start with
                            >>the "hack" or you will never stop applying hacks. People will stare at
                            >>your code and giggle behind your back...
                            >>>
                            >>What you are describing isn't a "module" you are describing a Singleton
                            >>Class. If you want to read something perhaps about the Singleton Class
                            >>would be a good start.
                            >>>
                            >>Try it you may like it.
                            >>>
                            >>"Anil Gupte" <anil-list@icinema.co mwrote in message
                            >>news:OCFPtkBI HHA.3676@TK2MSF TNGP03.phx.gbl. ..
                            >>>Thanx, that will get me started. Do you happen to know any place where
                            >>>I can read more about when to use a Module and when to use a class?
                            >>>>
                            >>>Thanx again,
                            >>>--
                            >>>Anil Gupte
                            >>>www.keeninc.net
                            >>>www.icinema.com
                            >>>>
                            >>>"Branco Medeiros" <branco.medeiro s@gmail.comwrot e in message
                            >>>news:1166131 605.256508.2072 50@n67g2000cwd. googlegroups.co m...
                            >>>>Anil Gupte wrote:
                            >>>>>I am trying to set up a function that connects to the database that I
                            >>>>>can
                            >>>>>then use gloablly. I set up a class called L3Global in which I have
                            >>>>>a
                            >>>>>function as follows:
                            >>>>>
                            >>>>>Public Function SetDBConnect()
                            >>>><snip>
                            >>>>>
                            >>>>>Now I want to use this is all my forms (MDI). I set up
                            >>>>>Public L3G As New L3Global
                            >>>>>>
                            >>>>>and then under the approporate button click I have:
                            >>>>>Dim ConnL3Producer As New OleDbConnection
                            >>>>>>
                            >>>>>ConnL3Prod ucer = L3G.SetDBConnec t
                            >>>>>>
                            >>>>>The problem is, do I have to setup an instance of L3Global in each
                            >>>>>form?
                            >>>>>That defeats the purpose, because each one opens a new connection.
                            >>>>>How can
                            >>>>>I use this connection object in all my forms but open it only once?
                            >>>>>Ideally
                            >>>>>I would like to move as much of the intialization (creating the
                            >>>>>OLEDBAdapt er
                            >>>>>etc) to the global routine.
                            >>>><snip>
                            >>>>>
                            >>>>I guess you can declare a Module, instead of a class:
                            >>>>>
                            >>>><aircode>
                            >>>> Module L3Global
                            >>>> Private mConnection As OleDBConnection
                            >>>>>
                            >>>> Public Function ConnectDB() As OleDBConnection
                            >>>> If mConnection Is Nothing Then
                            >>>> Dim ConText As String = "Your connection string"
                            >>>> mCOnnection = New OleDBConnectins (ConText)
                            >>>> End If
                            >>>> Return mConnection
                            >>>> End Function
                            >>>> End Module
                            >>>></aircode>
                            >>>>>
                            >>>>And then, in your Forms you'd have:
                            >>>>>
                            >>>> Dim ConnL3Producer _
                            >>>> As OleDbConnection = L3Global.Connec tDB()
                            >>>>>
                            >>>>Improvise over that.
                            >>>>>
                            >>>>Regards,
                            >>>>>
                            >>>>Branco.
                            >>>>>
                            >>>>
                            >>>>
                            >>>
                            >>>
                            >>
                            >>
                            >
                            >

                            Comment

                            • Cor Ligthert [MVP]

                              #15
                              Re: Global Functions

                              Tom,

                              My expection that the Singleton is not instanced but seats on the Main part
                              is that it is in my eyes very inefficient to do it in another way if it has
                              to be forever in a program.

                              Cor

                              "Cor Ligthert [MVP]" <notmyfirstname @planet.nlschre ef in bericht
                              news:eMiSmcmIHH A.3952@TK2MSFTN GP02.phx.gbl...
                              Tom,
                              >
                              A class in OOP has to be instanced, (you wrote it in my idea yourself) so
                              that it can sit on the heap. You are true that I am guessing that a
                              Singleton Class is doing the same as a so called not official existing
                              Static Class in C# does, I could not yet find it where it residence, so I
                              should not have written that. There are differences in with the VB Module
                              and the Singleton, however as much as the behaviour between + and $
                              concationation in VB.
                              >
                              I like however the name "Module". It makes it for me very clear, that a
                              module is not a class, it is a part of the main object as it is fixed in
                              the program. An instances object however sits on the managed heap and will
                              be as as soon as that is possible removed. I find this name Module
                              therefore nicer than any Shared/Static class, which is in fact no class in
                              the OOP meaning.
                              >
                              It is possible to write a Module in the same way as a class, and be than
                              not forced to use the Shared keyword. In a very short discussion between
                              Kevin Spencer and me about this (with a very small sample from me) he
                              somewhere wrote a relative short while ago something in these newsgroups
                              as. "You are right Cor, I did not know this behaviour from the Module".
                              However, to search special messages on Google from either Kevin or me is
                              not easy.
                              >
                              Cor
                              >
                              >
                              "Tom Leylan" <tleylan@nospam .netschreef in bericht
                              news:eZiI8qhIHH A.2232@TK2MSFTN GP02.phx.gbl...
                              >Hi Cor (and Dennis):
                              >>
                              >A quick search of newsgroup messages reveals that similar conversations
                              >have taken place before. One in particular involved Kevin Spencer
                              >explaining why the VB Module exists (backwards compatibility) and
                              >offering his opinion that it should have been removed. I agree with him.
                              >>
                              >While it may constitute "a class with static properties and no
                              >constructor" I see no reason to provide a second syntax for creating such
                              >a thing. Why not a third or fourth syntax as well? I really don't want
                              >to rehash all the points because for every point made there will be a
                              >counterpoint and we simply duplicate countless other threads.
                              >>
                              >I would hesitate to describe it as a Singleton class however. There is
                              >in fact a single object in a Singleton class and all properties do not
                              >need to be static. The instantiation of that object can be delayed until
                              >"first use". One can obtain a reference to the singleton object which
                              >can be passed as a parameter to other methods. If these things can be
                              >done with the VB module that's cool but again I ask why do we need two
                              >ways to obtain identical results? Furthermore we cannot create a
                              >singleton class using Module syntax subclassed from an arbitrary class
                              >right? So it is not only a "dupe" it is a limited use duplication.
                              >>
                              >I believe it comes down to clarity of design. I can't explain it better
                              >but we don't need a language to implement every person's favorite way to
                              >do something. If anybody wants "module" functionality couldn't they
                              >simply declare a class in the way that the module does and get it? There
                              >it is, available to anybody who wants it, not part of the official
                              >language syntax, not misinterpreted as being a VB feature, etc.
                              >>
                              >Cor, you're misinterpreting or simply guessing. It isn't that C#
                              >programmers can or cannot stand the word module. A C programmer doesn't
                              >think more clearly than a VB programmer, a dBASE programmer doesn't rank
                              >somewhere in a good programmer chart. There are good and bad (shall we
                              >call them neat and sloppy) programmers using every language. There is
                              >often a division between people who love having "&" and "+" available to
                              >concatenate strings and those who ask, why two operators? Some may find
                              >two ways "a brilliant design" while others will say it creates
                              >unnecessary ambiguity. I wouldn't say that C# developers find it odd and
                              >VB developers find it cool.
                              >>
                              >The VB module doesn't add anything to the language (that I am aware of)
                              >and as such it should be removed from the language.
                              >>
                              >>
                              >"Cor Ligthert [MVP]" <notmyfirstname @planet.nlwrote in message
                              >news:%233D%23W LhIHHA.2456@TK2 MSFTNGP06.phx.g bl...
                              >>Tom,
                              >>>
                              >>I hope to disappoint you, however it seems that C# programmers cannot
                              >>stand the word "module".
                              >>>
                              >>However a singleton C# class is as far as I know nothing more than a
                              >>module, which residence at the stack.
                              >>>
                              >>Beside the slight misbehaviour that a variable declared in a module can
                              >>be called in VB.Net without its module name, is in my idea the module
                              >>much nicer for declaring and using shared parts in a program.
                              >>>
                              >>I would never write, "Never use a module", I agree with you when it
                              >>becomes. "Try to avoid a module, static class, singleton, shared class
                              >>or whatever you name them."
                              >>>
                              >>Cor
                              >>>
                              >>"Tom Leylan" <tleylan@nospam .netschreef in bericht
                              >>news:%23GH2CU LIHHA.3424@TK2M SFTNGP02.phx.gb l...
                              >>>Anil: Yes you can read it right here :-) Never use a module!
                              >>>>
                              >>>VB.Net is an object-oriented language and by definition that should
                              >>>lead to object-oriented solutions. If there is no OOP solution (and
                              >>>that would seem unlikely given apps written in SmallTalk, C++, Java, C#
                              >>>and all the other OOP-based languages) then one might find themselves
                              >>>forced into having to resort to a "hack". Take my word for it don't
                              >>>start with the "hack" or you will never stop applying hacks. People
                              >>>will stare at your code and giggle behind your back...
                              >>>>
                              >>>What you are describing isn't a "module" you are describing a Singleton
                              >>>Class. If you want to read something perhaps about the Singleton Class
                              >>>would be a good start.
                              >>>>
                              >>>Try it you may like it.
                              >>>>
                              >>>"Anil Gupte" <anil-list@icinema.co mwrote in message
                              >>>news:OCFPtkB IHHA.3676@TK2MS FTNGP03.phx.gbl ...
                              >>>>Thanx, that will get me started. Do you happen to know any place
                              >>>>where I can read more about when to use a Module and when to use a
                              >>>>class?
                              >>>>>
                              >>>>Thanx again,
                              >>>>--
                              >>>>Anil Gupte
                              >>>>www.keeninc.net
                              >>>>www.icinema.com
                              >>>>>
                              >>>>"Branco Medeiros" <branco.medeiro s@gmail.comwrot e in message
                              >>>>news:116613 1605.256508.207 250@n67g2000cwd .googlegroups.c om...
                              >>>>>Anil Gupte wrote:
                              >>>>>>I am trying to set up a function that connects to the database that
                              >>>>>>I can
                              >>>>>>then use gloablly. I set up a class called L3Global in which I have
                              >>>>>>a
                              >>>>>>functio n as follows:
                              >>>>>>
                              >>>>>>Public Function SetDBConnect()
                              >>>>><snip>
                              >>>>>>
                              >>>>>>Now I want to use this is all my forms (MDI). I set up
                              >>>>>>Public L3G As New L3Global
                              >>>>>>>
                              >>>>>>and then under the approporate button click I have:
                              >>>>>>Dim ConnL3Producer As New OleDbConnection
                              >>>>>>>
                              >>>>>>ConnL3Pro ducer = L3G.SetDBConnec t
                              >>>>>>>
                              >>>>>>The problem is, do I have to setup an instance of L3Global in each
                              >>>>>>form?
                              >>>>>>That defeats the purpose, because each one opens a new connection.
                              >>>>>>How can
                              >>>>>>I use this connection object in all my forms but open it only once?
                              >>>>>>Ideally
                              >>>>>>I would like to move as much of the intialization (creating the
                              >>>>>>OLEDBAdap ter
                              >>>>>>etc) to the global routine.
                              >>>>><snip>
                              >>>>>>
                              >>>>>I guess you can declare a Module, instead of a class:
                              >>>>>>
                              >>>>><aircode >
                              >>>>> Module L3Global
                              >>>>> Private mConnection As OleDBConnection
                              >>>>>>
                              >>>>> Public Function ConnectDB() As OleDBConnection
                              >>>>> If mConnection Is Nothing Then
                              >>>>> Dim ConText As String = "Your connection string"
                              >>>>> mCOnnection = New OleDBConnectins (ConText)
                              >>>>> End If
                              >>>>> Return mConnection
                              >>>>> End Function
                              >>>>> End Module
                              >>>>></aircode>
                              >>>>>>
                              >>>>>And then, in your Forms you'd have:
                              >>>>>>
                              >>>>> Dim ConnL3Producer _
                              >>>>> As OleDbConnection = L3Global.Connec tDB()
                              >>>>>>
                              >>>>>Improvis e over that.
                              >>>>>>
                              >>>>>Regards,
                              >>>>>>
                              >>>>>Branco.
                              >>>>>>
                              >>>>>
                              >>>>>
                              >>>>
                              >>>>
                              >>>
                              >>>
                              >>
                              >>
                              >
                              >

                              Comment

                              Working...