avoiding global variables

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

    #1

    avoiding global variables

    Quick question about passing variables to subs to reduce the need for
    publicly declared variables in VB6. If I have an event sub (MouseDown) that
    runs a few lines of code, how can I use a variable in that sub without
    making it global? I tried adding the variable to the arguments of the sub,
    but VB doesn't seem to like anything other than the predescribed format of
    the event. So, for example, given the MouseDown event:

    Private Sub Form_MouseDown( Button As Integer, Shift As Integer, X As Single,
    Y As Single)

    I would intuitively add a variable in such a manner:

    Private Sub Form_MouseDown( Button As Integer, Shift As Integer, X As Single,
    Y As Single, foo as Whatever)

    which doesn't work. How should I do this instead?

    -Dan


  • CajunCoiler \(http://www.cajuncoiler.tk\)

    #2
    Re: avoiding global variables

    Have you tried making the variable STATIC ?

    "Dan" <a@a.com> wrote in message
    news:INuZb.7148 0$n62.40231@twi ster.nyroc.rr.c om...[color=blue]
    > Quick question about passing variables to subs to reduce the need for
    > publicly declared variables in VB6. If I have an event sub (MouseDown)[/color]
    that[color=blue]
    > runs a few lines of code, how can I use a variable in that sub without
    > making it global? I tried adding the variable to the arguments of the[/color]
    sub,[color=blue]
    > but VB doesn't seem to like anything other than the predescribed format of
    > the event. So, for example, given the MouseDown event:
    >
    > Private Sub Form_MouseDown( Button As Integer, Shift As Integer, X As[/color]
    Single,[color=blue]
    > Y As Single)
    >
    > I would intuitively add a variable in such a manner:
    >
    > Private Sub Form_MouseDown( Button As Integer, Shift As Integer, X As[/color]
    Single,[color=blue]
    > Y As Single, foo as Whatever)
    >
    > which doesn't work. How should I do this instead?
    >
    > -Dan
    >
    >[/color]


    Comment

    • Auric__

      #3
      Re: avoiding global variables

      On Fri, 20 Feb 2004 21:15:20 GMT, Dan wrote:
      [color=blue]
      >Quick question about passing variables to subs to reduce the need for
      >publicly declared variables in VB6. If I have an event sub (MouseDown) that
      >runs a few lines of code, how can I use a variable in that sub without
      >making it global? I tried adding the variable to the arguments of the sub,
      >but VB doesn't seem to like anything other than the predescribed format of
      >the event. So, for example, given the MouseDown event:
      >
      >Private Sub Form_MouseDown( Button As Integer, Shift As Integer, X As Single,
      >Y As Single)
      >
      >I would intuitively add a variable in such a manner:
      >
      >Private Sub Form_MouseDown( Button As Integer, Shift As Integer, X As Single,
      >Y As Single, foo as Whatever)
      >
      >which doesn't work. How should I do this instead?[/color]

      Have your MouseDown event call a separate sub and pass the args to that.
      Private Sub Form_MouseDown( Button As Integer, Shift As Integer, _
      X As Single, Y As Single)
      mySub Button, Shift, X, Y, foo
      End Sub

      ....and then mySub would do the actual work, and could be called from
      somewhere else directly.
      --
      auric "underscore " "underscore " "at" hotmail "dot" com
      *****
      The computer industry did not get where it is today by fixing its
      problems.

      Comment

      • Steve Gerrard

        #4
        Re: avoiding global variables


        "Dan" <a@a.com> wrote in message
        news:INuZb.7148 0$n62.40231@twi ster.nyroc.rr.c om...[color=blue]
        >
        > I would intuitively add a variable in such a manner:
        >
        > Private Sub Form_MouseDown( Button As Integer, Shift As Integer, X As[/color]
        Single,[color=blue]
        > Y As Single, foo as Whatever)
        >
        > which doesn't work. How should I do this instead?
        >
        > -Dan
        >
        >[/color]

        Since the mouse down event is called by VB, it wouldn't know what to
        pass for foo anyway.

        Is this what you mean?

        Private Sub Form_MouseDown( Button As Integer, Shift As Integer, X As
        Single,
        Y As Single)
        Dim Foo as Variant
        'use Foo here
        End Sub



        Comment

        • Dan

          #5
          Re: avoiding global variables

          "Steve Gerrard" <notstevegerrar d@comcast.net> wrote in message
          news:ze6dnRVOb6 OXIavdRVn-gQ@comcast.com. ..[color=blue]
          >
          > "Dan" <a@a.com> wrote in message
          > news:INuZb.7148 0$n62.40231@twi ster.nyroc.rr.c om...[color=green]
          > >
          > > I would intuitively add a variable in such a manner:
          > >
          > > Private Sub Form_MouseDown( Button As Integer, Shift As Integer, X As[/color]
          > Single,[color=green]
          > > Y As Single, foo as Whatever)
          > >
          > > which doesn't work. How should I do this instead?
          > >
          > > -Dan
          > >
          > >[/color]
          >
          > Since the mouse down event is called by VB, it wouldn't know what to
          > pass for foo anyway.
          >
          > Is this what you mean?
          >
          > Private Sub Form_MouseDown( Button As Integer, Shift As Integer, X As
          > Single,
          > Y As Single)
          > Dim Foo as Variant
          > 'use Foo here
          > End Sub[/color]

          I should have been a little more explicit in my question. Let me use an
          example. So I have the variable foo that I'm using in various parts of the
          program. Now, when the user presses a mouse button, the code inside the
          MouseDown event needs to know what foo is. But, foo is not specific just to
          the MouseDown event. There are other subs and funtions that also need to
          know what foo is. So making foo STATIC would not solve the problem.

          -Dan


          Comment

          • J French

            #6
            Re: avoiding global variables

            On Sat, 21 Feb 2004 05:13:35 GMT, "Dan" <a@a.com> wrote:
            <snip>
            [color=blue]
            >
            >I should have been a little more explicit in my question. Let me use an
            >example. So I have the variable foo that I'm using in various parts of the
            >program. Now, when the user presses a mouse button, the code inside the
            >MouseDown event needs to know what foo is. But, foo is not specific just to
            >the MouseDown event. There are other subs and funtions that also need to
            >know what foo is. So making foo STATIC would not solve the problem.[/color]

            Personally I use a Form/Module level UDT for such variables

            Private Type TCMN
            MouseIsDownFlag As Boolean
            ....
            End Type

            Private cmn As TCMN

            ......

            If cmn.MouseIsDown Flag Then


            Comment

            • preben nielsen

              #7
              Re: avoiding global variables


              "Dan" <a@a.com> skrev i en meddelelse
              news:3OBZb.3391 5$um1.29900@twi ster.nyroc.rr.c om...[color=blue]
              > "Steve Gerrard" <notstevegerrar d@comcast.net> wrote in message
              > news:ze6dnRVOb6 OXIavdRVn-gQ@comcast.com. ..[/color]
              [color=blue][color=green]
              > > Since the mouse down event is called by VB, it wouldn't know[/color][/color]
              what to[color=blue][color=green]
              > > pass for foo anyway.
              > >
              > > Is this what you mean?
              > >
              > > Private Sub Form_MouseDown( Button As Integer, Shift As[/color][/color]
              Integer, X As[color=blue][color=green]
              > > Single,
              > > Y As Single)
              > > Dim Foo as Variant
              > > 'use Foo here
              > > End Sub[/color]
              >
              > I should have been a little more explicit in my question. Let[/color]
              me use an[color=blue]
              > example. So I have the variable foo that I'm using in various[/color]
              parts of the[color=blue]
              > program. Now, when the user presses a mouse button, the code[/color]
              inside the[color=blue]
              > MouseDown event needs to know what foo is. But, foo is not[/color]
              specific just to[color=blue]
              > the MouseDown event. There are other subs and funtions that[/color]
              also need to[color=blue]
              > know what foo is. So making foo STATIC would not solve the[/color]
              problem.

              To make it more elegant I would make a module called Global. In
              that module I would place the public variables _and_ the
              procedures that use them !

              In the form events I would then call those global procedures and
              not play with the global variables directly. I.e. define your
              global variable and a few procedures/functions to work on them.
              You could be extreme and place the variables in a class and make a
              single public instantiated variable of that class and access the
              variables through properties...

              --
              /\ preben nielsen
              \/\ prel@post.tele. dk


              Comment

              • Dan

                #8
                Re: avoiding global variables

                "preben nielsen" <prel@post.tele .dk> wrote in message
                news:40375071$0 $1644$edfadb0f@ dread14.news.te le.dk...[color=blue]
                >
                > "Dan" <a@a.com> skrev i en meddelelse
                > news:3OBZb.3391 5$um1.29900@twi ster.nyroc.rr.c om...[color=green]
                > > "Steve Gerrard" <notstevegerrar d@comcast.net> wrote in message
                > > news:ze6dnRVOb6 OXIavdRVn-gQ@comcast.com. ..[/color]
                >[color=green][color=darkred]
                > > > Since the mouse down event is called by VB, it wouldn't know[/color][/color]
                > what to[color=green][color=darkred]
                > > > pass for foo anyway.
                > > >
                > > > Is this what you mean?
                > > >
                > > > Private Sub Form_MouseDown( Button As Integer, Shift As[/color][/color]
                > Integer, X As[color=green][color=darkred]
                > > > Single,
                > > > Y As Single)
                > > > Dim Foo as Variant
                > > > 'use Foo here
                > > > End Sub[/color]
                > >
                > > I should have been a little more explicit in my question. Let[/color]
                > me use an[color=green]
                > > example. So I have the variable foo that I'm using in various[/color]
                > parts of the[color=green]
                > > program. Now, when the user presses a mouse button, the code[/color]
                > inside the[color=green]
                > > MouseDown event needs to know what foo is. But, foo is not[/color]
                > specific just to[color=green]
                > > the MouseDown event. There are other subs and funtions that[/color]
                > also need to[color=green]
                > > know what foo is. So making foo STATIC would not solve the[/color]
                > problem.
                >
                > To make it more elegant I would make a module called Global. In
                > that module I would place the public variables _and_ the
                > procedures that use them !
                >
                > In the form events I would then call those global procedures and
                > not play with the global variables directly. I.e. define your
                > global variable and a few procedures/functions to work on them.
                > You could be extreme and place the variables in a class and make a
                > single public instantiated variable of that class and access the
                > variables through properties...
                >
                > --
                > /\ preben nielsen
                > \/\ prel@post.tele. dk[/color]

                Which is similar to what I do now. But I hear lots of shouting and fussing
                about avoiding Public variables entirely and that people have spent their
                entire programming career without using a single Public/Global variable. As
                a QBasic native, that was not an impossible feat (liberally passing
                variables amongst functions was elementary), but I still don't see how it's
                possible in VB.

                -Dan


                Comment

                • Steve Gerrard

                  #9
                  Re: avoiding global variables


                  "Dan" <a@a.com> wrote in message
                  news:5uQ_b.8526 8$%72.32749@twi ster.nyroc.rr.c om...[color=blue]
                  > "preben nielsen" <prel@post.tele .dk> wrote in message
                  > Which is similar to what I do now. But I hear lots of shouting and[/color]
                  fussing[color=blue]
                  > about avoiding Public variables entirely and that people have spent[/color]
                  their[color=blue]
                  > entire programming career without using a single Public/Global[/color]
                  variable. As[color=blue]
                  > a QBasic native, that was not an impossible feat (liberally passing
                  > variables amongst functions was elementary), but I still don't see how[/color]
                  it's[color=blue]
                  > possible in VB.
                  >[/color]

                  Either of these would work, and are essentially equivalent:

                  '---------
                  'Module1
                  Private mMyLong As Long

                  'use function and sub

                  Public Function GetMyLong() As Long
                  GetMyLong = mMyLong
                  End Function

                  Public Sub LetMyLong(ByVal NewVal As Long)
                  mMyLong = NewVal
                  End Sub

                  'or use property get/let

                  Public Property Get MyLong() As Long
                  MyLong = mMyLong
                  End Property

                  Public Property Let MyLong(ByVal RHS As Long)
                  mMyLong = RHS
                  End Property

                  'The calls look like this:
                  '-------
                  'Form1

                  Private Sub Command1_Click( )
                  'sub/function
                  LetMyLongBe 3
                  MsgBox GetMyLong
                  End Sub

                  Private Sub Command2_Click( )
                  'property let/get
                  MyLong = 2
                  MsgBox MyLong
                  End Sub


                  Comment

                  Working...