+ and & operators

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

    #1

    + and & operators

    What is the difference between "+" and "&" operator when joining strings?


    Thanx!



  • Herfried K. Wagner [MVP]

    #2
    Re: + and & operators

    "Domac" <dd@dd.cc> schrieb:[color=blue]
    > What is the difference between "+" and "&" operator when joining strings?[/color]

    Did you already read the documentation on these operators?

    --
    M S Herfried K. Wagner
    M V P <URL:http://dotnet.mvps.org/>
    V B <URL:http://classicvb.org/petition/>

    Comment

    • Ted Harrison

      #3
      Re: + and &amp; operators

      On Thu, 29 Jun 2006 13:51:07 +0200 Domac <dd@dd.cc> wrote:
      [color=blue]
      > *From:* "Domac" <dd@dd.cc>
      > *Date:* Thu, 29 Jun 2006 13:51:07 +0200
      >
      > What is the difference between "+" and "&" operator when joining
      > strings?
      >[/color]

      Not much difference when joining two real strings.
      "aaa" + "bbb" would give the same result as "aaa" & "bbb" i.e. "aaabbb"

      However, if one expression is a number and one a string then things are
      less clear...

      "5" + 10 will give the number 15. The "5" is converted to a number "on the
      fly".

      Whereas "5" & 10 will give the string "510". The 10 is converted to a
      string "on the fly".

      + should really only be used to add numbers, and & should be used to
      concatenate strings.

      Comment

      • Domac

        #4
        Re: + and &amp; operators

        Thanks a lot!

        "Domac" <dd@dd.cc> wrote in message
        news:un%23SQJ3m GHA.732@TK2MSFT NGP04.phx.gbl.. .[color=blue]
        > What is the difference between "+" and "&" operator when joining strings?
        >
        >
        > Thanx!
        >
        >
        >[/color]


        Comment

        • Phill W.

          #5
          Re: + and &amp; operators

          Domac wrote:[color=blue]
          > What is the difference between "+" and "&" operator when joining strings?[/color]

          With "Option Strict On" - none at all.

          With "Option Strict Off" and joining only Strings - none at all.

          With "Option Strict Off" and joining Strings and /other/ data types -
          the effects can be "interestin g" ...

          HTH,
          Phill W.

          Comment

          • Brian Tkatch

            #6
            Re: + and &amp; operators


            Phill W. wrote:[color=blue]
            > Domac wrote:[color=green]
            > > What is the difference between "+" and "&" operator when joining strings?[/color]
            >
            > With "Option Strict On" - none at all.
            >
            > With "Option Strict Off" and joining only Strings - none at all.
            >
            > With "Option Strict Off" and joining Strings and /other/ data types -
            > the effects can be "interestin g" ...
            >
            > HTH,
            > Phill W.[/color]

            Except that you can do += but not &=.

            B.

            Comment

            • Dick Sutton

              #7
              Re: + and &amp; operators

              I use &= in assignment statements all the time. For example:

              strHTML &= MonthName(CInt( sMonth), False) & " - " & sYear &
              "</h3></center><br>"

              Hope this helps...

              Dick

              "Brian Tkatch" <Maxwell_Smart@ ThePentagon.com > wrote in message
              news:1151593333 .087137.67580@b 68g2000cwa.goog legroups.com...[color=blue]
              >
              > Phill W. wrote:[color=green]
              >> Domac wrote:[color=darkred]
              >> > What is the difference between "+" and "&" operator when joining
              >> > strings?[/color]
              >>
              >> With "Option Strict On" - none at all.
              >>
              >> With "Option Strict Off" and joining only Strings - none at all.
              >>
              >> With "Option Strict Off" and joining Strings and /other/ data types -
              >> the effects can be "interestin g" ...
              >>
              >> HTH,
              >> Phill W.[/color]
              >
              > Except that you can do += but not &=.
              >
              > B.
              >[/color]


              Comment

              • Brian Tkatch

                #8
                Re: + and &amp; operators

                Dick Sutton wrote:[color=blue]
                > I use &= in assignment statements all the time. For example:
                >
                > strHTML &= MonthName(CInt( sMonth), False) & " - " & sYear &
                > "</h3></center><br>"
                >
                > Hope this helps...
                >
                > Dick
                >
                > "Brian Tkatch" <Maxwell_Smart@ ThePentagon.com > wrote in message
                > news:1151593333 .087137.67580@b 68g2000cwa.goog legroups.com...[color=green]
                > >
                > > Phill W. wrote:[color=darkred]
                > >> Domac wrote:
                > >> > What is the difference between "+" and "&" operator when joining
                > >> > strings?
                > >>
                > >> With "Option Strict On" - none at all.
                > >>
                > >> With "Option Strict Off" and joining only Strings - none at all.
                > >>
                > >> With "Option Strict Off" and joining Strings and /other/ data types -
                > >> the effects can be "interestin g" ...
                > >>
                > >> HTH,
                > >> Phill W.[/color]
                > >
                > > Except that you can do += but not &=.
                > >
                > > B.
                > >[/color][/color]

                OK, i see, my mistake. Thanks for the correction.

                I quickly tested it out by opening a module and typing:

                Sub a()
                Dim a As String
                a+=a
                a&=a
                End Sub

                The editor separated a+=a into a += a, but it separated a&=a to a& = a
                and underlined a& as an error. Which i assumed (incorrectly) that &=
                was not supported.

                So, perhaps a difference is whether the editor put in that space before
                the operator. With = is does, with & it does not. Well, nothing too
                important. :)

                Thanx for the correction.

                B.

                Comment

                • Miro

                  #9
                  Re: + and &amp; operators

                  Excellent Answer.

                  Short, sweet, and most important a simple example.

                  I didnt know why either, till i seen this post.

                  Miro
                  - Learning VB Net


                  "Ted Harrison" <ted@ntsx.co.uk > wrote in message
                  news:memo.20060 629131952.384B@ ed.eddy.ed...[color=blue]
                  > On Thu, 29 Jun 2006 13:51:07 +0200 Domac <dd@dd.cc> wrote:
                  >[color=green]
                  >> *From:* "Domac" <dd@dd.cc>
                  >> *Date:* Thu, 29 Jun 2006 13:51:07 +0200
                  >>
                  >> What is the difference between "+" and "&" operator when joining
                  >> strings?
                  >>[/color]
                  >
                  > Not much difference when joining two real strings.
                  > "aaa" + "bbb" would give the same result as "aaa" & "bbb" i.e. "aaabbb"
                  >
                  > However, if one expression is a number and one a string then things are
                  > less clear...
                  >
                  > "5" + 10 will give the number 15. The "5" is converted to a number "on the
                  > fly".
                  >
                  > Whereas "5" & 10 will give the string "510". The 10 is converted to a
                  > string "on the fly".
                  >
                  > + should really only be used to add numbers, and & should be used to
                  > concatenate strings.
                  >[/color]


                  Comment

                  • Göran Andersson

                    #10
                    Re: + and &amp; operators

                    Brian Tkatch wrote:[color=blue]
                    > Dick Sutton wrote:[color=green]
                    >> I use &= in assignment statements all the time. For example:
                    >>
                    >> strHTML &= MonthName(CInt( sMonth), False) & " - " & sYear &
                    >> "</h3></center><br>"
                    >>
                    >> Hope this helps...
                    >>
                    >> Dick
                    >>
                    >> "Brian Tkatch" <Maxwell_Smart@ ThePentagon.com > wrote in message
                    >> news:1151593333 .087137.67580@b 68g2000cwa.goog legroups.com...[color=darkred]
                    >>> Phill W. wrote:
                    >>>> Domac wrote:
                    >>>>> What is the difference between "+" and "&" operator when joining
                    >>>>> strings?
                    >>>> With "Option Strict On" - none at all.
                    >>>>
                    >>>> With "Option Strict Off" and joining only Strings - none at all.
                    >>>>
                    >>>> With "Option Strict Off" and joining Strings and /other/ data types -
                    >>>> the effects can be "interestin g" ...
                    >>>>
                    >>>> HTH,
                    >>>> Phill W.
                    >>> Except that you can do += but not &=.
                    >>>
                    >>> B.
                    >>>[/color][/color]
                    >
                    > OK, i see, my mistake. Thanks for the correction.
                    >
                    > I quickly tested it out by opening a module and typing:
                    >
                    > Sub a()
                    > Dim a As String
                    > a+=a
                    > a&=a
                    > End Sub
                    >
                    > The editor separated a+=a into a += a, but it separated a&=a to a& = a
                    > and underlined a& as an error. Which i assumed (incorrectly) that &=
                    > was not supported.
                    >
                    > So, perhaps a difference is whether the editor put in that space before
                    > the operator. With = is does, with & it does not. Well, nothing too
                    > important. :)
                    >
                    > Thanx for the correction.
                    >
                    > B.
                    >[/color]

                    That is Basic Baggage. The & character once had a meaning at the end of
                    a variable name. The interpreter still recognises this, but it doesn't
                    work any more.

                    Comment

                    • guy

                      #11
                      Re: + and &amp; operators

                      i dont know about myvar& not working but myvar% still does,
                      it still documented as working too

                      guy

                      "Göran Andersson" wrote:
                      [color=blue]
                      > Brian Tkatch wrote:[color=green]
                      > > Dick Sutton wrote:[color=darkred]
                      > >> I use &= in assignment statements all the time. For example:
                      > >>
                      > >> strHTML &= MonthName(CInt( sMonth), False) & " - " & sYear &
                      > >> "</h3></center><br>"
                      > >>
                      > >> Hope this helps...
                      > >>
                      > >> Dick
                      > >>
                      > >> "Brian Tkatch" <Maxwell_Smart@ ThePentagon.com > wrote in message
                      > >> news:1151593333 .087137.67580@b 68g2000cwa.goog legroups.com...
                      > >>> Phill W. wrote:
                      > >>>> Domac wrote:
                      > >>>>> What is the difference between "+" and "&" operator when joining
                      > >>>>> strings?
                      > >>>> With "Option Strict On" - none at all.
                      > >>>>
                      > >>>> With "Option Strict Off" and joining only Strings - none at all.
                      > >>>>
                      > >>>> With "Option Strict Off" and joining Strings and /other/ data types -
                      > >>>> the effects can be "interestin g" ...
                      > >>>>
                      > >>>> HTH,
                      > >>>> Phill W.
                      > >>> Except that you can do += but not &=.
                      > >>>
                      > >>> B.
                      > >>>[/color]
                      > >
                      > > OK, i see, my mistake. Thanks for the correction.
                      > >
                      > > I quickly tested it out by opening a module and typing:
                      > >
                      > > Sub a()
                      > > Dim a As String
                      > > a+=a
                      > > a&=a
                      > > End Sub
                      > >
                      > > The editor separated a+=a into a += a, but it separated a&=a to a& = a
                      > > and underlined a& as an error. Which i assumed (incorrectly) that &=
                      > > was not supported.
                      > >
                      > > So, perhaps a difference is whether the editor put in that space before
                      > > the operator. With = is does, with & it does not. Well, nothing too
                      > > important. :)
                      > >
                      > > Thanx for the correction.
                      > >
                      > > B.
                      > >[/color]
                      >
                      > That is Basic Baggage. The & character once had a meaning at the end of
                      > a variable name. The interpreter still recognises this, but it doesn't
                      > work any more.
                      >[/color]

                      Comment

                      • Jay B. Harlow [MVP - Outlook]

                        #12
                        Re: + and &amp; operators

                        Göran
                        | That is Basic Baggage. The & character once had a meaning at the end of
                        | a variable name. The interpreter still recognises this, but it doesn't
                        | work any more.
                        As Guy suggests whatever& still works in both .NET 1.x & .NET 2.0.

                        For example:

                        Dim whatever&

                        is short hand for:

                        Dim whatever As Long

                        For details see:

                        http://msdn2.microsoft.com/en-us/library/s9cz43ek.aspx

                        However I don't recommend using it, as only a very small subset of types are
                        supported...

                        --
                        Hope this helps
                        Jay B. Harlow [MVP - Outlook]
                        ..NET Application Architect, Enthusiast, & Evangelist
                        T.S. Bradley - http://www.tsbradley.net


                        "Göran Andersson" <guffa@guffa.co m> wrote in message
                        news:%23DGmuV9m GHA.3544@TK2MSF TNGP05.phx.gbl. ..
                        | Brian Tkatch wrote:
                        | > Dick Sutton wrote:
                        | >> I use &= in assignment statements all the time. For example:
                        | >>
                        | >> strHTML &= MonthName(CInt( sMonth), False) & " - " & sYear &
                        | >> "</h3></center><br>"
                        | >>
                        | >> Hope this helps...
                        | >>
                        | >> Dick
                        | >>
                        | >> "Brian Tkatch" <Maxwell_Smart@ ThePentagon.com > wrote in message
                        | >> news:1151593333 .087137.67580@b 68g2000cwa.goog legroups.com...
                        | >>> Phill W. wrote:
                        | >>>> Domac wrote:
                        | >>>>> What is the difference between "+" and "&" operator when joining
                        | >>>>> strings?
                        | >>>> With "Option Strict On" - none at all.
                        | >>>>
                        | >>>> With "Option Strict Off" and joining only Strings - none at all.
                        | >>>>
                        | >>>> With "Option Strict Off" and joining Strings and /other/ data types -
                        | >>>> the effects can be "interestin g" ...
                        | >>>>
                        | >>>> HTH,
                        | >>>> Phill W.
                        | >>> Except that you can do += but not &=.
                        | >>>
                        | >>> B.
                        | >>>
                        | >
                        | > OK, i see, my mistake. Thanks for the correction.
                        | >
                        | > I quickly tested it out by opening a module and typing:
                        | >
                        | > Sub a()
                        | > Dim a As String
                        | > a+=a
                        | > a&=a
                        | > End Sub
                        | >
                        | > The editor separated a+=a into a += a, but it separated a&=a to a& = a
                        | > and underlined a& as an error. Which i assumed (incorrectly) that &=
                        | > was not supported.
                        | >
                        | > So, perhaps a difference is whether the editor put in that space before
                        | > the operator. With = is does, with & it does not. Well, nothing too
                        | > important. :)
                        | >
                        | > Thanx for the correction.
                        | >
                        | > B.
                        | >
                        |
                        | That is Basic Baggage. The & character once had a meaning at the end of
                        | a variable name. The interpreter still recognises this, but it doesn't
                        | work any more.


                        Comment

                        • Göran Andersson

                          #13
                          Re: + and &amp; operators

                          Jay B. Harlow [MVP - Outlook] wrote:
                          Göran
                          | That is Basic Baggage. The & character once had a meaning at the end of
                          | a variable name. The interpreter still recognises this, but it doesn't
                          | work any more.
                          As Guy suggests whatever& still works in both .NET 1.x & .NET 2.0.
                          >
                          For example:
                          >
                          Dim whatever&
                          >
                          is short hand for:
                          >
                          Dim whatever As Long
                          >
                          For details see:
                          >
                          http://msdn2.microsoft.com/en-us/library/s9cz43ek.aspx
                          >
                          However I don't recommend using it, as only a very small subset of types are
                          supported...
                          >
                          Oh, so it still works, even?

                          Why didn't they get rid of that? They had the chance when they created
                          VB.NET, now it's gonna haunt the VB language for ever...

                          Comment

                          Working...