How do you tell if two variables are the same?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Harry J. Smith

    How do you tell if two variables are the same?


    How do you tell if two variables past as arguments to a subroutine are
    actually the same variable? In some languages you can compare their
    addresses. Visual Basic doesn't have pointers so what is the best way to
    do this?

    Public Sub SetToUI(X As MultiUI, Z As MultiUI) 'Z = X
    'MultiUI is a user defined type (Multiple-precision unsigned
    integer)
    If Z = X Then Exit Sub
    ....
    End Sub 'SetToUI

    --

    -Harry http://home.netcom.com/~hjsmith


  • Larry Serflaten

    #2
    Re: How do you tell if two variables are the same?

    "Harry J. Smith" <hjsmith@ix.net com.com> wrote[color=blue]
    >
    > How do you tell if two variables past as arguments to a subroutine are
    > actually the same variable? In some languages you can compare their
    > addresses. Visual Basic doesn't have pointers so what is the best way to
    > do this?
    >
    > Public Sub SetToUI(X As MultiUI, Z As MultiUI) 'Z = X
    > 'MultiUI is a user defined type (Multiple-precision unsigned
    > integer)
    > If Z = X Then Exit Sub
    > ...
    > End Sub 'SetToUI[/color]


    Did you try:

    If Z Is X Then Exit Sub

    LFS




    -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
    http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
    -----== Over 100,000 Newsgroups - 19 Different Servers! =-----

    Comment

    • Auric__

      #3
      Re: How do you tell if two variables are the same?

      "...And the next sign of the Apocalypse will be..."
      *****
      On Mon, 05 Jan 2004 18:48:46 GMT, Harry J. Smith wrote:
      [color=blue]
      >
      >How do you tell if two variables past as arguments to a subroutine are
      >actually the same variable? In some languages you can compare their
      >addresses. Visual Basic doesn't have pointers so what is the best way to
      >do this?
      >
      >Public Sub SetToUI(X As MultiUI, Z As MultiUI) 'Z = X
      > 'MultiUI is a user defined type (Multiple-precision unsigned
      >integer)
      > If Z = X Then Exit Sub
      >...
      >End Sub 'SetToUI[/color]

      VB does indeed have pointers, they're just not well-documented. There
      are three keywords that return pointers - VarPtr, StrPtr, and ObjPtr,
      which return pointers to a variable, a string, and an object,
      repsectively.

      This code should do what you want, but it only works when args are
      passed ByRef (which is the default) - if you pass them ByVal then copies
      are made, each with its own address.
      Public Sub SetToUI(X As MultiUI, Z As MultiUI)
      Dim ptrX As Long, ptrZ As Long
      ptrX = VarPtr(X)
      ptrZ = VarPtr(Z)
      If ptrX = ptrZ Then Exit Sub
      '...
      End Sub

      --
      auric "underscore " "underscore " "at" hotmail "dot" com
      *****
      Youth is not a time of life, it's a state of mind.

      Comment

      • Harry J. Smith

        #4
        Re: How do you tell if two variables are the same?


        "Larry Serflaten" <Abuse@SpamBust ers.com> wrote in message
        news:3ff9e0b7$1 _7@corp.newsgro ups.com...[color=blue]
        > "Harry J. Smith" <hjsmith@ix.net com.com> wrote[color=green]
        > >
        > > How do you tell if two variables past as arguments to a subroutine[/color][/color]
        are[color=blue][color=green]
        > > actually the same variable? In some languages you can compare their
        > > addresses. Visual Basic doesn't have pointers so what is the best[/color][/color]
        way to[color=blue][color=green]
        > > do this?
        > >
        > > Public Sub SetToUI(X As MultiUI, Z As MultiUI) 'Z = X
        > > 'MultiUI is a user defined type (Multiple-precision unsigned
        > > integer)
        > > If Z = X Then Exit Sub
        > > ...
        > > End Sub 'SetToUI[/color]
        >
        >
        > Did you try:
        >
        > If Z Is X Then Exit Sub
        >
        > LFS[/color]

        Yes, but that doesn't do what I want. See the next message.

        -Harry


        Comment

        • Harry J. Smith

          #5
          Re: How do you tell if two variables are the same?


          "Auric__" <not.my.real@em ail.address> wrote in message
          news:4j2kvvo2tn 21lbsee88fjvbn7 9hgjp2ftq@4ax.c om...[color=blue]
          > "...And the next sign of the Apocalypse will be..."
          > *****
          > On Mon, 05 Jan 2004 18:48:46 GMT, Harry J. Smith wrote:
          >[color=green]
          > >
          > >How do you tell if two variables past as arguments to a subroutine[/color][/color]
          are[color=blue][color=green]
          > >actually the same variable? In some languages you can compare their
          > >addresses. Visual Basic doesn't have pointers so what is the best way[/color][/color]
          to[color=blue][color=green]
          > >do this?
          > >
          > >Public Sub SetToUI(X As MultiUI, Z As MultiUI) 'Z = X
          > > 'MultiUI is a user defined type (Multiple-precision unsigned
          > >integer)
          > > If Z = X Then Exit Sub
          > >...
          > >End Sub 'SetToUI[/color]
          >
          > VB does indeed have pointers, they're just not well-documented. There
          > are three keywords that return pointers - VarPtr, StrPtr, and ObjPtr,
          > which return pointers to a variable, a string, and an object,
          > repsectively.
          >
          > This code should do what you want, but it only works when args are
          > passed ByRef (which is the default) - if you pass them ByVal then[/color]
          copies[color=blue]
          > are made, each with its own address.
          > Public Sub SetToUI(X As MultiUI, Z As MultiUI)
          > Dim ptrX As Long, ptrZ As Long
          > ptrX = VarPtr(X)
          > ptrZ = VarPtr(Z)
          > If ptrX = ptrZ Then Exit Sub
          > '...
          > End Sub
          >
          > --
          > auric "underscore " "underscore " "at" hotmail "dot" com
          > *****
          > Youth is not a time of life, it's a state of mind.[/color]

          Sounds good, I will give it a try.

          -Harry http://home.netcom.com/~hjsmith


          Comment

          • Harry J. Smith

            #6
            Re: How do you tell if two variables are the same?


            "Harry J. Smith" <hjsmith@ix.net com.com> wrote in message
            news:r1ZKb.4307 6$Pg1.37238@new sread1.news.pas .earthlink.net. ..[color=blue]
            >
            > "Auric__" <not.my.real@em ail.address> wrote in message
            > news:4j2kvvo2tn 21lbsee88fjvbn7 9hgjp2ftq@4ax.c om...[color=green]
            > > "...And the next sign of the Apocalypse will be..."
            > > *****
            > > On Mon, 05 Jan 2004 18:48:46 GMT, Harry J. Smith wrote:
            > >[color=darkred]
            > > >
            > > >How do you tell if two variables past as arguments to a subroutine[/color][/color]
            > are[color=green][color=darkred]
            > > >actually the same variable? In some languages you can compare their
            > > >addresses. Visual Basic doesn't have pointers so what is the best[/color][/color][/color]
            way[color=blue]
            > to[color=green][color=darkred]
            > > >do this?
            > > >
            > > >Public Sub SetToUI(X As MultiUI, Z As MultiUI) 'Z = X
            > > > 'MultiUI is a user defined type (Multiple-precision unsigned
            > > >integer)
            > > > If Z = X Then Exit Sub
            > > >...
            > > >End Sub 'SetToUI[/color]
            > >
            > > VB does indeed have pointers, they're just not well-documented.[/color][/color]
            There[color=blue][color=green]
            > > are three keywords that return pointers - VarPtr, StrPtr, and[/color][/color]
            ObjPtr,[color=blue][color=green]
            > > which return pointers to a variable, a string, and an object,
            > > repsectively.
            > >
            > > This code should do what you want, but it only works when args are
            > > passed ByRef (which is the default) - if you pass them ByVal then[/color]
            > copies[color=green]
            > > are made, each with its own address.
            > > Public Sub SetToUI(X As MultiUI, Z As MultiUI)
            > > Dim ptrX As Long, ptrZ As Long
            > > ptrX = VarPtr(X)
            > > ptrZ = VarPtr(Z)
            > > If ptrX = ptrZ Then Exit Sub
            > > '...
            > > End Sub
            > >
            > > --
            > > auric "underscore " "underscore " "at" hotmail "dot" com
            > > *****
            > > Youth is not a time of life, it's a state of mind.[/color]
            >
            > Sounds good, I will give it a try.
            >
            > -Harry http://home.netcom.com/~hjsmith[/color]

            Works great:

            Type MultiUI 'A multiple precision unsigned integer
            M As Long 'Max number of super digits ever in this number
            N As Long 'Current length in super digits, 1...NMax <= MuNMax
            V() As Single 'Value, LSD at index 0
            End Type

            Public Sub SetToUI(X As MultiUI, Z As MultiUI) 'Z = X
            Dim I As Long
            'Subject: Re: How do you tell if two variables are the same?
            'From: auric "underscore " "underscore " "at" hotmail "dot" com
            '
            'VB does indeed have pointers, they're just not well-documented. There
            'are three keywords that return pointers - VarPtr, StrPtr, and ObjPtr,
            'which return pointers to a variable, a string, and an object,
            'respectively.
            '
            If VarPtr(Z) = VarPtr(X) Then Exit Sub
            Z.N = X.N
            If Z.N > Z.M Then
            MuWriteErr "SetTo overflow, continuing... (N=" & _
            Z.N & " > M=" & Z.M & ")"
            Z.N = Z.M
            End If
            For I = 0 To Z.N - 1
            Z.V(I) = X.V(I)
            Next I
            End Sub 'SetToUI

            -Harry


            Comment

            • Larry Serflaten

              #7
              Re: How do you tell if two variables are the same?

              "Harry J. Smith" <hjsmith@ix.net com.com> wrote
              [color=blue][color=green][color=darkred]
              > > > Public Sub SetToUI(X As MultiUI, Z As MultiUI) 'Z = X
              > > > 'MultiUI is a user defined type (Multiple-precision unsigned
              > > > integer)[/color][/color]
              >
              > Yes, but that doesn't do what I want. See the next message.[/color]


              Oops, I overlooked the second line where you mentioned it was
              a UDT (instead of an object) Sorry!

              LFS




              -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
              http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
              -----== Over 100,000 Newsgroups - 19 Different Servers! =-----

              Comment

              • Harry J. Smith

                #8
                Re: How do you tell if two variables are the same?


                "Harry J. Smith" <hjsmith@ix.net com.com> wrote in message
                news:ikiKb.3864 1$Pg1.7194@news read1.news.pas. earthlink.net.. .[color=blue]
                >
                > How do you tell if two variables past as arguments to a subroutine are
                > actually the same variable? In some languages you can compare their
                > addresses. Visual Basic doesn't have pointers so what is the best way[/color]
                to[color=blue]
                > do this?
                >
                > Public Sub SetToUI(X As MultiUI, Z As MultiUI) 'Z = X
                > 'MultiUI is a user defined type (Multiple-precision unsigned
                > integer)
                > If Z = X Then Exit Sub
                > ...
                > End Sub 'SetToUI
                >
                > --
                >
                > -Harry http://home.netcom.com/~hjsmith[/color]

                Now that we have pointers, how do you reference the thing that it points
                to?
                Like MuitiUI(ptrX) ?

                Public Sub AddUI(X As MultiUI, Y As MultiUI, Z As MultiUI) 'Z = X + Y
                'Sets MuErr True if overflow
                Dim ptrX As Long, ptrY As Long, ptrZ As Long, tmp As Long
                ptrX = VarPtr(X)
                ptrY = VarPtr(Y)
                ptrZ = VarPtr(Z)
                If ptrZ = ptrX Then
                RAddUI Y, Z
                Exit Sub
                ElseIf ptrZ = ptrY Then
                RAddUI X, Z
                Exit Sub
                ElseIf X.N < Y.N Then 'If X is the smaller
                tmp = prtX 'Interchange pointers
                prtX = prtY
                ptrY = tmp
                End If
                SetToUI MuitiUI(ptrX), Z 'Set Z to the larger
                RAddUI MultiUI(ptrY), Z 'and add the smaller
                End Sub 'AddUI

                -Harry


                Comment

                • Auric__

                  #9
                  Re: How do you tell if two variables are the same?

                  "...And the next sign of the Apocalypse will be..."
                  *****
                  On Thu, 08 Jan 2004 21:11:31 GMT, Harry J. Smith wrote:
                  [color=blue]
                  >Now that we have pointers, how do you reference the thing that it points
                  >to?
                  >Like MuitiUI(ptrX) ?
                  >
                  >Public Sub AddUI(X As MultiUI, Y As MultiUI, Z As MultiUI) 'Z = X + Y
                  >'Sets MuErr True if overflow
                  > Dim ptrX As Long, ptrY As Long, ptrZ As Long, tmp As Long
                  > ptrX = VarPtr(X)
                  > ptrY = VarPtr(Y)
                  > ptrZ = VarPtr(Z)
                  > If ptrZ = ptrX Then
                  > RAddUI Y, Z
                  > Exit Sub
                  > ElseIf ptrZ = ptrY Then
                  > RAddUI X, Z
                  > Exit Sub
                  > ElseIf X.N < Y.N Then 'If X is the smaller
                  > tmp = prtX 'Interchange pointers
                  > prtX = prtY
                  > ptrY = tmp
                  > End If
                  > SetToUI MuitiUI(ptrX), Z 'Set Z to the larger
                  > RAddUI MultiUI(ptrY), Z 'and add the smaller
                  >End Sub 'AddUI[/color]

                  That's a damn good question. VB isn't like C-based languages, where the
                  variable and its address can be used interchangeably . QuickBasic, MS's
                  command-line BASIC compiler, had a handful of memory functions - notable
                  peek and poke - but they aren't in either version of VB I have installed
                  (VB4 and VB6), so my guess is you'll have to use API calls. (For the
                  record, VB4 doesn't have VarPtr either.) I *think* CopyMemory should do
                  it, but that's just a guess.
                  --
                  auric "underscore " "underscore " "at" hotmail "dot" com
                  *****
                  This night methinks is but the daylight sick.

                  Comment

                  • Randy Birch

                    #10
                    Re: How do you tell if two variables are the same?

                    : peek and poke - but they aren't in either version of VB I have installed
                    : (VB4 and VB6), so my guess is you'll have to use API calls. (For the
                    : record, VB4 doesn't have VarPtr either.) I *think* CopyMemory should do
                    : it, but that's just a guess.

                    VarPtr is a defined API in VB3 and VB4 as well, just not intrinsic as
                    methods within the IDE.

                    VB4-32
                    Declare Function VarPtr Lib "vb40032.dl l" (Var as Long) As Long

                    'VB4-16
                    Declare Function VarPtr Lib "vb40016.dl l" (Var as Integer) As Integer

                    In VB5 (if desired) the declaration is:

                    Declare Function VarPtr Lib "msvbvm50.d ll" (Var as Long) As Long

                    --

                    Randy Birch
                    MVP Visual Basic

                    Please respond only to the newsgroups so all can benefit.



                    Comment

                    Working...