473,394 Members | 1,658 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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.InvalidCastException' occurred in
microsoft.visualbasic.dll

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

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


Nov 21 '05 #1
4 1772
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/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
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" <ch**********@hotmail.com> wrote in message
news:uy**************@TK2MSFTNGP14.phx.gbl...
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.InvalidCastException' occurred in
microsoft.visualbasic.dll

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

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

Nov 21 '05 #2
FB,
pstrSend = Chr(ActionCode1) + Chr(ActionCode2) + Msg + &HFF & vbCr &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" <ch**********@hotmail.com> wrote in message
news:uy**************@TK2MSFTNGP14.phx.gbl... 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.InvalidCastException' occurred in
microsoft.visualbasic.dll

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

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

Nov 21 '05 #3
Yes
Nov 21 '05 #4
"FB's .NET Dev PC" <ch**********@hotmail.com> schrieb:
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.


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

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

Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

50
by: dataangel | last post by:
I wrote a function to compare whether two strings are "similar" because I'm using python to make a small text adventure engine and I want to it to be responsive to slight mispellings, like...
20
by: verec | last post by:
One problem I've come accross in designing a specific version of auto_ptr is that I have to disntiguish between "polymorphic" arguments and "plain" ones, because the template has to, internally,...
11
by: Steve Gough | last post by:
Could anyone please help me to understand what is happening here? The commented line produces an error, which is what I expected given that there is no conversion defined from type double to type...
7
by: Jim Bancroft | last post by:
Hi everyone, A basic one here, I think. I haven't found the pattern yet, but sometimes when I cast a variable to another type using the "C" style cast operator the compiler refuses to play...
44
by: Agoston Bejo | last post by:
What happens exactly when I do the following: struct A { int i; string j; A() {} }; void f(A& a) { cout << a.i << endl;
17
by: arindam.mukerjee | last post by:
I was running code like: #include <stdio.h> int main() { printf("%f\n", 9/5); return 0; }
74
by: cman | last post by:
Can you "walk across" C strings or char pointers (using *(sz+1)) like you can with arrays. If not, why not? If so, how? cman
3
by: arnuld | last post by:
PURPOSE: write a pointer version of strcat function. WHAT I DID: I have 2 implementations of this function. Both compile and run but 1st one gives some strange results, 2nd is ok. I am unable...
14
by: KK | last post by:
Dear All I have a small problem with using as operator on value type array. Here is an example what I am trying to do. using System; using System.Collections.Generic; using System.Text;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.