Using + or & for concatenating strings - and an implicit cast

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • FB's .NET Dev PC

    Using + or & for concatenating strings - and an implicit cast

    Interesting note, the code below as is will attempt to cast what is clearly
    indicated as a string into a double. This is becuase the use of + as a
    concatenation operator. The error message generated is included with the
    code. Use & instead and it works.



    Dim pstrSend As String = ""

    'ActionCode 1 and 2 are type BYTE, Msg is a byVal string parameter



    pstrSend = Chr(ActionCode1 ) + Chr(ActionCode2 ) + Msg + &HFF & vbCr

    'write and flush

    objSocketWriter .Write(pstrSend )

    objSocketWriter .Flush()

    End Function



    An unhandled exception of type 'System.Invalid CastException' occurred in
    microsoft.visua lbasic.dll

    Additional information: Cast from string "cThis is a longer test message" to
    type 'Double' is not valid.



    System.InvalidC astException: Cast from string "cThis is a longer test
    message" to type 'Double' is not valid. ---> System.FormatEx ception: Input
    string was not in a correct format.




  • One Handed Man \( OHM - Terry Burns \)

    #2
    Re: Using + or & for concatenating strings - and an implicit cast

    Now try it with OPTION Strict = ON and see if it compiles

    --
    OHM ( Terry Burns ) * Use the following to email me *

    Dim ch() As Char = "ufssz/cvsotAhsfbuTpmv ujpotXjui/OFU".ToCharArra y()
    For i As Int32 = 0 To ch.Length - 1
    ch(i) = Convert.ToChar( Convert.ToInt16 (ch(i)) - 1)
    Next
    Process.Start(" mailto:" & New String(ch))
    --


    "FB's .NET Dev PC" <chipmonk3775@h otmail.com> wrote in message
    news:uy9bJq6rEH A.2136@TK2MSFTN GP14.phx.gbl...[color=blue]
    > Interesting note, the code below as is will attempt to cast what is
    > clearly indicated as a string into a double. This is becuase the use of +
    > as a concatenation operator. The error message generated is included with
    > the code. Use & instead and it works.
    >
    >
    >
    > Dim pstrSend As String = ""
    >
    > 'ActionCode 1 and 2 are type BYTE, Msg is a byVal string parameter
    >
    >
    >
    > pstrSend = Chr(ActionCode1 ) + Chr(ActionCode2 ) + Msg + &HFF & vbCr
    >
    > 'write and flush
    >
    > objSocketWriter .Write(pstrSend )
    >
    > objSocketWriter .Flush()
    >
    > End Function
    >
    >
    >
    > An unhandled exception of type 'System.Invalid CastException' occurred in
    > microsoft.visua lbasic.dll
    >
    > Additional information: Cast from string "cThis is a longer test message"
    > to type 'Double' is not valid.
    >
    >
    >
    > System.InvalidC astException: Cast from string "cThis is a longer test
    > message" to type 'Double' is not valid. ---> System.FormatEx ception: Input
    > string was not in a correct format.
    >
    >
    >
    >[/color]


    Comment

    • Jay B. Harlow [MVP - Outlook]

      #3
      Re: Using + or &amp; for concatenating strings - and an implicit cast

      FB,[color=blue]
      > pstrSend = Chr(ActionCode1 ) + Chr(ActionCode2 ) + Msg + &HFF & vbCr[/color]
      &HFF is an "number", Msg needs to be converted to a number so that the two
      numbers can be added together.

      Which is why I use & for concatenating strings, the only implicit
      conversions are to string.

      Also if you use Option Strict On, you will get a compile error instead of a
      runtime error.

      Hope this helps
      Jay

      "FB's .NET Dev PC" <chipmonk3775@h otmail.com> wrote in message
      news:uy9bJq6rEH A.2136@TK2MSFTN GP14.phx.gbl...[color=blue]
      > Interesting note, the code below as is will attempt to cast what is
      > clearly indicated as a string into a double. This is becuase the use of +
      > as a concatenation operator. The error message generated is included with
      > the code. Use & instead and it works.
      >
      >
      >
      > Dim pstrSend As String = ""
      >
      > 'ActionCode 1 and 2 are type BYTE, Msg is a byVal string parameter
      >
      >
      >
      > pstrSend = Chr(ActionCode1 ) + Chr(ActionCode2 ) + Msg + &HFF & vbCr
      >
      > 'write and flush
      >
      > objSocketWriter .Write(pstrSend )
      >
      > objSocketWriter .Flush()
      >
      > End Function
      >
      >
      >
      > An unhandled exception of type 'System.Invalid CastException' occurred in
      > microsoft.visua lbasic.dll
      >
      > Additional information: Cast from string "cThis is a longer test message"
      > to type 'Double' is not valid.
      >
      >
      >
      > System.InvalidC astException: Cast from string "cThis is a longer test
      > message" to type 'Double' is not valid. ---> System.FormatEx ception: Input
      > string was not in a correct format.
      >
      >
      >
      >[/color]


      Comment

      • Cor Ligthert

        #4
        Re: Using + or &amp; for concatenating strings - and an implicit cast

        Yes


        Comment

        • Herfried K. Wagner [MVP]

          #5
          Re: Using + or &amp; for concatenating strings - and an implicit cast

          "FB's .NET Dev PC" <chipmonk3775@h otmail.com> schrieb:[color=blue]
          > Interesting note, the code below as is will attempt to cast what is
          > clearly indicated as a string into a double. This is becuase the use of +
          > as a concatenation operator. The error message generated is included with
          > the code. Use & instead and it works.[/color]

          Add 'Option Strict On' on top of your source file and "try again".

          --
          Herfried K. Wagner [MVP]
          <URL:http://dotnet.mvps.org/>

          Comment

          Working...