{Bug} Shortcut variable assignment

Vader

Well-Known Member
Licensed User
Longtime User
This works:
B4X:
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:
B4X:
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:
B4X:
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.
 

Vader

Well-Known Member
Licensed User
Longtime User
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.

Ok, so that first line is performing an implicit cast. How do I perform an explicit cast?
 

Vader

Well-Known Member
Licensed User
Longtime User
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).

Ok, so like I said, there is no explicit cast method(s).

Thanks.
 
Top