SldCvr
SldCvr

Reputation: 5

Time Span not working properly

I'm working on a Download ETA Calculator..So I use the Timespan code to tell the ETA..The timespan code in Visual Basic is not working as it should be... because when ever I type the file size and speed i.e 1GB and 1 Mb/s, the time span which is Label1.Text is 5.17:00:00. Here is my code

Public Class Form1

Private Property z As Object

Private Sub LinkLabel1_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
    Process.Start("www.speedtest.net")
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim x, y, z, a, b, c As Single

    x = TextBox1.Text
    y = TextBox2.Text

    If RadioButton1.Checked = True And RadioButton3.Checked = True Then
        Label4.Text = "Minutes"
        z = x * 1024
        c = y / 8
        a = z / c
        Label1.Text = New TimeSpan(a / 60, 0, 0).ToString()
    End If

    If RadioButton1.Checked = True And RadioButton4.Checked = True Then
        Label4.Text = "Minutes"
        z = x * 1024
        c = 1024 / 8 / y
        a = z / c
        Label1.Text = New TimeSpan(a / 60, 0, 0).ToString()
    End If
    If RadioButton2.Checked = True And RadioButton3.Checked = True Then
        Label4.Text = "Hours"
        z = x * 1048576
        c = y / 8
        a = z / c
        b = a / 60
        Label1.Text = New TimeSpan(a / 60, 0, 0).ToString()
    End If
    If RadioButton2.Checked = True And RadioButton4.Checked = True Then
        Label4.Text = "Hours"
        z = x * 1048576
        c = 1024 / 8 * y
        a = z / c
        b = a / 60
        Label1.Text = New TimeSpan(a / 60, 0, 0).ToString()
    End If
End Sub

Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged

End Sub

Private Sub Button2_Click_1(sender As Object, e As EventArgs) Handles Button2.Click
    MsgBox("Made by SldCvr Enterprises", MsgBoxStyle.OkOnly, "About")

End Sub

End Class

Here is the designer

https://i.sstatic.net/f3pFm.jpg

Upvotes: 0

Views: 343

Answers (1)

woutervs
woutervs

Reputation: 1510

Timespan is working correctly your implementation is wrong.

First fault: a/60 will return an integer instead of a double. Second fault: new TimeSpan(h,m,s,ms) expects integers

The reason your code compiles is because the compiler does a rounding on your a/60 if you would change a/60 to a/60d (d for decimal) you would get a correct division but also a compiler error. Due to the integer limitation.

To fix this error make your division to a total number of seconds and do (new Timespan()).addSeconds(your number);

Upvotes: 4

Related Questions