Variable Conversion

alfcen

Well-Known Member
Licensed User
Longtime User
Hi
I'm aware that once declared variables cannot be converted and that B4A tries to convert when assigned to each other. However, it mostly fails.

Reading data with the TextReader returns a string that, when assigned to a Double variable, throws a NumberFormatException. Example:

B4X:
Dim gb, gl ,hg as Double

Sub GetLastLocation()
    Dim Reader As TextReader
    Reader.Initialize(File.OpenInput(File.DirInternal, "astrotime.ini"))
    Dim line As String
    line = Reader.ReadLine: gb = line
    line = Reader.ReadLine: gl = line
    line = Reader.ReadLine: hg = line
    Reader.Close
End Sub

Is there any elegant way to accomplish this?
 

agraham

Expert
Licensed User
Longtime User
It should work. One possibility is that your ini file was written under Windows and uses CR (13)and LF (10) as line separators wheras Android only uses LF as line separators so the CRs are ending up in the read string. You could try a String.Replace(Chr(13), "") or use a text editor that supports Unix line endings to create your ini file.
 
Upvote 0

alfcen

Well-Known Member
Licensed User
Longtime User
The INI file has been generated by TextWriter, assuming that the TextWriter terminates a string with LF, Chr(10) only. I tried String.Replace without success.

Besides, even if you replace Chr(13) with "" the read data is still a String.
 
Upvote 0

alfcen

Well-Known Member
Licensed User
Longtime User
Thank you Erel, I have added "& Chr(10)" to each string to write by the TextWriter. Without this addition, the first line = Reader.ReadLine will read the entire file, thus causing an exception.

B4X:
Sub Button1_Click
    Dim Writer As TextWriter
    Writer.Initialize(File.OpenOutput(File.DirInternal, "astrotime.ini", False))
    Writer.Write(main.gb [COLOR="Red"]& Chr(10)[/COLOR]) 
    Writer.Write(main.gl [COLOR="Red"]& Chr(10)[/COLOR]) 
    Writer.Write(main.hg [COLOR="Red"]& Chr(10)[/COLOR]) 
    Writer.Flush
    Writer.Close 
End Sub
 
Upvote 0

alfcen

Well-Known Member
Licensed User
Longtime User
Yes, Erel, this works fine, thank you! The difference between Writer.Write and Writer.WriteLine is absolutely clear now, and... it does make good sense.
 
Upvote 0
Top