Issue with function which's read number from file

DeleteMe

Member
Joined
Apr 8, 2013
Messages
7
Programming Experience
Beginner
Hi,
I've got program which is checking OS' backups (if backup was created, what 'size' is backup file have, etc) and I've got Function which imports file's size:

VB.NET:
 Function wielkoscNiedzielnyOdczytaj() As Long
        Dim wielkoscNiedzielny As Long = 0
        Try
            Using sr As New StreamReader("C:\TAI\ServerCheckBackup\fWielkoscNiedzielny.txt")
                wielkoscNiedzielny = sr.ReadLine()
                Return wielkoscNiedzielny
            End Using
        Catch e As Exception
            Return wielkoscNiedzielny = 0
        End Try
    End Function
In file
C:\TAI\ServerCheckBackup\fWielkoscNiedzielny.txt
I've got sth like: 231,86. It's in MB. So when I give:
VB.NET:
Console.WriteLine( wielkoscNiedzielnyOdczytaj())
I get 632 not 631,68 at Console Window. Why is it rounded? I want get number from file which isn't rounded. What do I have to change in that function?
 
If you expect the function to return a value with decimal places then you have to declare its return type as one that supports decimal places. Does Long support decimal places?

Your code is a bit dodgy because it's expecting the text from the file to be implicitly converted to a numeric type, whatever type that may be. You should be validating and converting the data explicitly, e.g.
Dim text = sr.ReadLine()
Dim number As Double

If Double.TryParse(text, number) Then
    'The data is valid and the result is stored in 'number'.
Else
    'The data is not valid.
End If
 
Hi,
I'll try do it tomorow and I'll check if Long support decimal places ;)

In that file will be only numeric values, eg 234,12, 22, etc (there won't be sth like 123ab or "fdffd").

Thanks for your answer :)
 
I'll check if Long support decimal places ;)
It was a rhetorical question. It doesn't. The VB Long type is implemented using the Int64 structure. That name should be a giveaway, i.e. it represents a 64-bit integer.
 
Ok, for decimal laces I can use Single, Double or Decimal types - I did my homework ;)

Now I've got
Function wielkoscNiedzielnyOdczytaj() As Single
Dim wielkoscNiedzielny As Single = 0.0


Try
Using sr As New StreamReader("C:\TAI\ServerCheckBackup\fWielkoscNiedzielny.txt")
Dim text = sr.ReadLine()
Dim number As Single


If Double.TryParse(text, number) Then
'The data is valid and the result is stored in 'number'.
'wielkoscNiedzielny = sr.ReadLine()
Return number
Else
Return wielkoscNiedzielny = 0.0
'The data is not valid.
End If



End Using
Catch e As Exception
Return wielkoscNiedzielny = 0.0
End Try
End Function
and
Console.WriteLine(nazwaPlikuBackup & " ok " & wielkoscNiedzielnyOdczytaj() & " MB.")
but it still gives me 632 :(
 
Last edited:
hmm, i did sth like this:
Function wielkoscNiedzielnyOdczytaj() As Double


Dim fileReader As System.IO.StreamReader
Dim stringReader As Double


fileReader = My.Computer.FileSystem.OpenTextFileReader("C:\TAI\ServerCheckBackup\fWielkoscNiedzielny.txt")


stringReader = fileReader.ReadLine()


Return stringReader
End Function
and it's... working fine :D
 
Back
Top