Vader Well-Known Member Licensed User Longtime User Jan 18, 2013 #1 This works: Code: Copy Private Sub WriteToSerialPort(Data As Int) Dim StringToSend As String = Chr(Data) Dim DataToSend() As Byte = StringToSend.GetBytes("ISO8859_1") Stream.Write(DataToSend) Log("-----------Sent->" & Data ) End Sub This does not: Code: Copy Private Sub WriteToSerialPort(Data As Int) Dim DataToSend() As Byte = Chr(Data).GetBytes("ISO8859_1") Stream.Write(DataToSend) Log("-----------Sent->" & Data ) End Sub This also does not: Code: Copy Private Sub WriteToSerialPort(Data As Int) Stream.Write(Chr(Data).GetBytes("ISO8859_1")) Log("-----------Sent->" & Data ) End Sub By my understanding, each of the above should produce the same result. The first version is the easiest to follow, but the last is the most terse.
This works: Code: Copy Private Sub WriteToSerialPort(Data As Int) Dim StringToSend As String = Chr(Data) Dim DataToSend() As Byte = StringToSend.GetBytes("ISO8859_1") Stream.Write(DataToSend) Log("-----------Sent->" & Data ) End Sub This does not: Code: Copy Private Sub WriteToSerialPort(Data As Int) Dim DataToSend() As Byte = Chr(Data).GetBytes("ISO8859_1") Stream.Write(DataToSend) Log("-----------Sent->" & Data ) End Sub This also does not: Code: Copy Private Sub WriteToSerialPort(Data As Int) Stream.Write(Chr(Data).GetBytes("ISO8859_1")) Log("-----------Sent->" & Data ) End Sub By my understanding, each of the above should produce the same result. The first version is the easiest to follow, but the last is the most terse.
Erel B4X founder Staff member Licensed User Longtime User Jan 20, 2013 #2 Chr keyword returns a Char object. Char object doesn't support GetBytes method. You need to cast it to string as you did in the first code snippet.
Chr keyword returns a Char object. Char object doesn't support GetBytes method. You need to cast it to string as you did in the first code snippet.
Vader Well-Known Member Licensed User Longtime User Jan 20, 2013 #3 Erel said: Chr keyword returns a Char object. Char object doesn't support GetBytes method. You need to cast it to string as you did in the first code snippet. Click to expand... Ok, so that first line is performing an implicit cast. How do I perform an explicit cast?
Erel said: Chr keyword returns a Char object. Char object doesn't support GetBytes method. You need to cast it to string as you did in the first code snippet. Click to expand... Ok, so that first line is performing an implicit cast. How do I perform an explicit cast?
Erel B4X founder Staff member Licensed User Longtime User Jan 20, 2013 #4 There is no cast keywords in Basic4android as the compiler takes care of it automatically. If you want to explicitly cast an object then you need to define a variable of the target type and assign it (like you did in the first example).
There is no cast keywords in Basic4android as the compiler takes care of it automatically. If you want to explicitly cast an object then you need to define a variable of the target type and assign it (like you did in the first example).
Vader Well-Known Member Licensed User Longtime User Jan 20, 2013 #5 Erel said: There is no cast keywords in Basic4android as the compiler takes care of it automatically. If you want to explicitly cast an object then you need to define a variable of the target type and assign it (like you did in the first example). Click to expand... Ok, so like I said, there is no explicit cast method(s). Thanks.
Erel said: There is no cast keywords in Basic4android as the compiler takes care of it automatically. If you want to explicitly cast an object then you need to define a variable of the target type and assign it (like you did in the first example). Click to expand... Ok, so like I said, there is no explicit cast method(s). Thanks.
Erel B4X founder Staff member Licensed User Longtime User Jan 20, 2013 #6 There is no built-in cast keywords. If you like you can create such methods. For example: Code: Copy Sub CInt (Val As Object) As Int Return Val End Sub
There is no built-in cast keywords. If you like you can create such methods. For example: Code: Copy Sub CInt (Val As Object) As Int Return Val End Sub