Very very simple

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • C L Humphreys

    Very very simple

    Hi,

    I'm used to programming with Java, and am unsure of how this may work within
    VB.

    I want to pass some strings to a function, and alter them within the
    function. I want the strings to stay altered when the function ends
    *without returning the strings explicitly*. I think this is to do with
    passing references? How should it work (if possible?) using VB?

    Thanks in advance,
    Chris


  • Rob Strover

    #2
    Re: Very very simple

    Chris,

    Paramters in VB are ByRef ('by reference') by default, so what you want to
    do will happen automatically.

    HTH,

    Rob.

    "C L Humphreys" <clhumphreys@to ofgib.moc> wrote in message
    news:bk9mj6$se3 $1@ucsnew1.ncl. ac.uk...[color=blue]
    > Hi,
    >
    > I'm used to programming with Java, and am unsure of how this may work[/color]
    within[color=blue]
    > VB.
    >
    > I want to pass some strings to a function, and alter them within the
    > function. I want the strings to stay altered when the function ends
    > *without returning the strings explicitly*. I think this is to do with
    > passing references? How should it work (if possible?) using VB?
    >
    > Thanks in advance,
    > Chris
    >
    >[/color]


    Comment

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

      #3
      Re: Very very simple

      If I remember correctly (and I probably do), you have three choices...

      1. Define the variables that you want to retain information as STATIC types.

      2. Define the variables that you want to retain information as PUBLIC types
      in your general declarations area.

      3. Define the variables that you want to retain information as PUBLIC types
      in a separate MDI child form.

      I personally prefer to use method #2, unless I have one MAJOR padload of
      variables, then I switch to the MDI form system.

      ---------------8<-------------------cut
      here------------8<------------------------

      "C L Humphreys" <clhumphreys@to ofgib.moc> wrote in message
      news:bk9mj6$se3 $1@ucsnew1.ncl. ac.uk...[color=blue]
      > Hi,
      >
      > I'm used to programming with Java, and am unsure of how this may work[/color]
      within[color=blue]
      > VB.
      >
      > I want to pass some strings to a function, and alter them within the
      > function. I want the strings to stay altered when the function ends
      > *without returning the strings explicitly*. I think this is to do with
      > passing references? How should it work (if possible?) using VB?
      >
      > Thanks in advance,
      > Chris
      >
      >[/color]


      Comment

      • C L Humphreys

        #4
        Re: Very very simple

        "Rob Strover" <dislexic_wobma t@NOSPAMyahoo.c om.invalid> wrote in message
        news:Ha_9b.1085 87$bo1.62667@ne ws-server.bigpond. net.au...[color=blue]
        > Chris,
        >
        > Paramters in VB are ByRef ('by reference') by default, so what you want to
        > do will happen automatically.[/color]

        Thanks, through the interpreter I've realised the problem lies in a replace
        statement..

        ?Replace("Hello ", "lo", "p")
        Hello

        I'm wanting it to return "Help" - i.e, change the "lo" to "p"... how can I
        do this?

        Thanks again,
        Chris


        Comment

        • Rob Strover

          #5
          Re: Very very simple

          Chris,

          Can't help as I don't have VB6, where this function exists.

          The code that MS released to achieve this functionality for VB5 developers
          (Q188007: Simulate Visual Basic 6.0 String Functions in VB5) gives the
          result you expected, so I guess someone with VB6 will have to answer this
          question.

          Rob.

          "C L Humphreys" <clhumphreys@to ofgib.moc> wrote in message
          news:bk9s76$1ur $1@ucsnew1.ncl. ac.uk...[color=blue]
          > "Rob Strover" <dislexic_wobma t@NOSPAMyahoo.c om.invalid> wrote in message
          > news:Ha_9b.1085 87$bo1.62667@ne ws-server.bigpond. net.au...[color=green]
          > > Chris,
          > >
          > > Paramters in VB are ByRef ('by reference') by default, so what you want[/color][/color]
          to[color=blue][color=green]
          > > do will happen automatically.[/color]
          >
          > Thanks, through the interpreter I've realised the problem lies in a[/color]
          replace[color=blue]
          > statement..
          >
          > ?Replace("Hello ", "lo", "p")
          > Hello
          >
          > I'm wanting it to return "Help" - i.e, change the "lo" to "p"... how can I
          > do this?
          >
          > Thanks again,
          > Chris
          >
          >[/color]


          Comment

          • C L Humphreys

            #6
            Re: Very very simple

            "Rob Strover" <dislexic_wobma t@NOSPAMyahoo.c om.invalid> wrote in message
            news:tK_9b.1085 96$bo1.33353@ne ws-server.bigpond. net.au...[color=blue]
            > Chris,
            >
            > Can't help as I don't have VB6, where this function exists.
            >
            > The code that MS released to achieve this functionality for VB5 developers
            > (Q188007: Simulate Visual Basic 6.0 String Functions in VB5) gives the
            > result you expected, so I guess someone with VB6 will have to answer this
            > question.[/color]

            Hmm, trying the examples given on that KB article results in errors for me,
            I think this may be due to me using VB for access.

            FYI the optional parameters are not allowed, and it only works if the string
            to search for is a single character long.. odd.

            Thanks again,
            Chris


            Comment

            • Rob Strover

              #7
              Re: Very very simple

              Chris,

              I've been checking with VBA under Word 97 (rather than VB5 <not on this
              PC>), but in the version of the VB5/MSKB Replace version I got rid of the
              various optional parameters, so that may be the answer.

              BTW, just ran the following amended version in an Access 97 Module OK

              HTH

              Rob.

              -------------------- Code Begins --------

              Private Sub AAAA()

              MsgBox Replace("Hello" , "llo", "lp") ' ==> Help

              End Sub

              Private Function Replace(sIn As String, sFind As String, sReplace As String)
              As String

              Dim nC As Long
              Dim nPos As Integer
              Dim sOut As String

              sOut = sIn
              nPos = InStr(sOut, sFind)

              If nPos <> 0 Then
              Do
              nC = nC + 1
              sOut = Left$(sOut, nPos - 1) & sReplace & Mid$(sOut, nPos +
              Len(sFind))

              nPos = InStr(sOut, sFind)
              Loop While nPos > 0

              End If

              Replace = sOut

              End Function

              -------------------- Code Ends --------

              "C L Humphreys" <clhumphreys@to ofgib.moc> wrote in message
              news:bk9uo3$341 $1@ucsnew1.ncl. ac.uk...[color=blue]
              > "Rob Strover" <dislexic_wobma t@NOSPAMyahoo.c om.invalid> wrote in message
              > news:tK_9b.1085 96$bo1.33353@ne ws-server.bigpond. net.au...[color=green]
              > > Chris,
              > >
              > > Can't help as I don't have VB6, where this function exists.
              > >
              > > The code that MS released to achieve this functionality for VB5[/color][/color]
              developers[color=blue][color=green]
              > > (Q188007: Simulate Visual Basic 6.0 String Functions in VB5) gives the
              > > result you expected, so I guess someone with VB6 will have to answer[/color][/color]
              this[color=blue][color=green]
              > > question.[/color]
              >
              > Hmm, trying the examples given on that KB article results in errors for[/color]
              me,[color=blue]
              > I think this may be due to me using VB for access.
              >
              > FYI the optional parameters are not allowed, and it only works if the[/color]
              string[color=blue]
              > to search for is a single character long.. odd.
              >
              > Thanks again,
              > Chris
              >
              >[/color]


              Comment

              • Rick Rothstein

                #8
                Re: Very very simple

                > > Paramters in VB are ByRef ('by reference') by default, so what you want
                to[color=blue][color=green]
                > > do will happen automatically.[/color]
                >
                > Thanks, through the interpreter I've realised the problem lies in a[/color]
                replace[color=blue]
                > statement..
                >
                > ?Replace("Hello ", "lo", "p")
                > Hello
                >
                > I'm wanting it to return "Help" - i.e, change the "lo" to "p"... how can I
                > do this?[/color]

                Sub ChangeStringTha tIsPassedIn(Str ingIn As String)
                StringIn = Replace(StringI n, "lo", "p")
                End Sub

                You would call this either like this (simplistic, non-realistic example):

                MyVariable = "Hello"
                Call ChangeStringTha tIsPassedIn(MyV ariable)
                MsgBox MyVariable

                or like this (using the alternate method of calling a Sub)

                MyVariable = "Hello"
                ChangeStringTha tIsPassedIn MyVariable
                MsgBox MyVariable

                Note in this last example, you **must** leave out the parentheses or the Sub
                won't do what you expect. The reason, anything non-syntactical in
                parentheses is evaluated as an expression. If you wrote this

                ChangeStringTha tIsPassedIn (MyVariable)

                (note the space in front of the open parenthesis that VB will insert), then
                MyVariable would be evaluated as an expression (whose result was nothing
                more than its own contents) and the memory location of where VB housed the
                evaluated expression, **not** the variable, would be passed into the Sub.

                Rick - MVP

                Rick - MVP


                Comment

                • J French

                  #9
                  Re: Very very simple

                  On Wed, 17 Sep 2003 15:48:02 +0100, "C L Humphreys"
                  <clhumphreys@to ofgib.moc> wrote:
                  [color=blue]
                  >"Rob Strover" <dislexic_wobma t@NOSPAMyahoo.c om.invalid> wrote in message
                  >news:Ha_9b.108 587$bo1.62667@n ews-server.bigpond. net.au...[color=green]
                  >> Chris,
                  >>
                  >> Paramters in VB are ByRef ('by reference') by default, so what you want to
                  >> do will happen automatically.[/color]
                  >
                  >Thanks, through the interpreter I've realised the problem lies in a replace
                  >statement..
                  >
                  >?Replace("Hell o", "lo", "p")
                  >Hello
                  >
                  >I'm wanting it to return "Help" - i.e, change the "lo" to "p"... how can I
                  >do this?[/color]

                  It should be doing just that

                  Comment

                  • C L Humphreys

                    #10
                    Re: Very very simple

                    "Rick Rothstein" <rickNOSPAMnews @NOSPAMcomcast. net> wrote in message
                    news:MoadnTH--50kFPWiXTWJig@c omcast.com...[color=blue][color=green][color=darkred]
                    > > > Paramters in VB are ByRef ('by reference') by default, so what you[/color][/color][/color]
                    want[color=blue]
                    > to[color=green][color=darkred]
                    > > > do will happen automatically.[/color]
                    > >
                    > > Thanks, through the interpreter I've realised the problem lies in a[/color]
                    > replace[color=green]
                    > > statement..
                    > >
                    > > ?Replace("Hello ", "lo", "p")
                    > > Hello
                    > >
                    > > I'm wanting it to return "Help" - i.e, change the "lo" to "p"... how can[/color][/color]
                    I[color=blue][color=green]
                    > > do this?[/color]
                    >
                    > Sub ChangeStringTha tIsPassedIn(Str ingIn As String)
                    > StringIn = Replace(StringI n, "lo", "p")
                    > End Sub
                    >
                    > You would call this either like this (simplistic, non-realistic example):
                    >
                    > MyVariable = "Hello"
                    > Call ChangeStringTha tIsPassedIn(MyV ariable)
                    > MsgBox MyVariable
                    >
                    > or like this (using the alternate method of calling a Sub)
                    >
                    > MyVariable = "Hello"
                    > ChangeStringTha tIsPassedIn MyVariable
                    > MsgBox MyVariable
                    >
                    > Note in this last example, you **must** leave out the parentheses or the[/color]
                    Sub[color=blue]
                    > won't do what you expect. The reason, anything non-syntactical in
                    > parentheses is evaluated as an expression. If you wrote this
                    >
                    > ChangeStringTha tIsPassedIn (MyVariable)
                    >
                    > (note the space in front of the open parenthesis that VB will insert),[/color]
                    then[color=blue]
                    > MyVariable would be evaluated as an expression (whose result was nothing
                    > more than its own contents) and the memory location of where VB housed the
                    > evaluated expression, **not** the variable, would be passed into the Sub.
                    >[/color]


                    Thanks to all for their help.

                    Chris


                    Comment

                    Working...