too many IF`s

Hi,
The code below should first check to see whether the current day is a week day or a weekend, then obtain the value from a text box pending the current time. A timer is started and relays the value of gas being used during the time in which the timer is enabled. I have written the code below but it seems to bypass the Goto command
Any advice would be appreciated.

'Start Gas 1

Private Sub cmdT1Start_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdT1Start.Click

If (d.DayOfWeek = DayOfWeek.Saturday) Or (DayOfWeek.Sunday) Then

GoTo 4

End If

If (d.DayOfWeek = DayOfWeek.Monday) Or (DayOfWeek.Tuesday) Or (DayOfWeek.Wednesday) Or (DayOfWeek.Thursday) Or (DayOfWeek.Friday) Then

GoTo 6

End If

4:

If Convert.ToString(Now.Hour) < txtboxWDFHr.Text Then

txtboxT1R.Text = txtboxWDR.Text

Else

txtboxT1R.Text = txtboxWER.Text

End If

GoTo 8

6:

If Convert.ToString(Now.Hour) < txtboxWkndDFHr.Text Then

txtboxT1R.Text = txtboxWkndDR.Text

Else

txtboxT1R.Text = txtboxWkndER.Text

End If

GoTo 8

8:

If cmdT1Start.Text = "&Start" Then

cmdT1Start.Text = "S&top"

tmrT1.Enabled = True

StartTime1 = VB.Timer()

cmdT1Reset.Enabled = False

frmT1.Text = "Gas 1 In Use"

lblT1Time.BackColor = System.Drawing.Color.Lime

Else

cmdT1Start.Text = "&Start"

StartTime1 = VB.Timer() + TimeElapsed1

TimeAccu1 = TimeElapsed1

tmrT1.Enabled = False

cmdT1Reset.Enabled = True

frmT1.Text = "Gas 1"

lblT1Time.BackColor = System.Drawing.Color.White

End If

End Sub

'Gas Valve 1 Timer

Private Sub tmrT1_Tick(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles tmrT1.Tick

TimeElapsed1 = (VB.Timer() - StartTime1) / 3600 + TimeAccu1

hr1 = Fix(TimeElapsed1)

tmpVar1 = TimeElapsed1 - hr1

min1 = tmpVar1 * 60

min1 = Fix(min1)

tmpVar1 = tmpVar1 * 60 - min1

sec1 = tmpVar1 * 60

If sec1 < 10 Then

If sec1 < 1 Then DisplayTime1 = VB.Left(Str(sec1), 4) Else DisplayTime1 = VB.Left(Str(sec1), 5)

Else

DisplayTime1 = VB.Left(Str(sec1), 6)

End If

DisplayTime1 = LTrim(DisplayTime1)

If sec1 < 1 Then

DisplayTime1 = "00" & DisplayTime1

GoTo 10

End If

If sec1 < 10 Then DisplayTime1 = "0" & DisplayTime1

10:

DisplayTime1 = LTrim(RTrim(Str(hr1))) & ":" & LTrim(RTrim(Str(min1))) & ":" & DisplayTime1

lblT1Time.Text = DisplayTime1

lblTotal1.Text = CStr((CDbl(LTrim(RTrim(Str(hr1)))) * CDbl(txtboxT1R.Text) * 60) + (CDbl(LTrim(RTrim(Str(min1)))) * CDbl(txtboxT1R.Text)))

End Sub




Answer this question

too many IF`s

  • Ravi K BA

    Ouch! I haven't used goto's since I started seriously using QBasic in the early 90's. If I'm reading your code right, though, the following subroutine SHOULD function the same as what you have written here:


    'Start Gas 1

    Private Sub cmdT1Start_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdT1Start.Click

    If (d.DayOfWeek = DayOfWeek.Saturday) Or (DayOfWeek.Sunday) Then

    If Convert.ToString(Now.Hour) < txtboxWDFHr.Text Then

    txtboxT1R.Text = txtboxWDR.Text

    Else

    txtboxT1R.Text = txtboxWER.Text

    End If

    Else 'must be a weekday, since weekends are taken care of above

    If Convert.ToString(Now.Hour) < txtboxWkndDFHr.Text Then

    txtboxT1R.Text = txtboxWkndDR.Text

    Else

    txtboxT1R.Text = txtboxWkndER.Text

    End If

    End If 'end weekend/weekday section

    If cmdT1Start.Text = "&Start" Then

    cmdT1Start.Text = "S&top"

    tmrT1.Enabled = True

    StartTime1 = VB.Timer()

    cmdT1Reset.Enabled = False

    frmT1.Text = "Gas 1 In Use"

    lblT1Time.BackColor = System.Drawing.Color.Lime

    Else

    cmdT1Start.Text = "&Start"

    StartTime1 = VB.Timer() + TimeElapsed1

    TimeAccu1 = TimeElapsed1

    tmrT1.Enabled = False

    cmdT1Reset.Enabled = True

    frmT1.Text = "Gas 1"

    lblT1Time.BackColor = System.Drawing.Color.White

    End If

    End Sub

    --as for the last part, where you're formatting a time, you might save yourself a lot of headache by using the format function. (I know, I did something of the same thing you did here before noticing the format function.)

    http://msdn.microsoft.com/library/default.asp url=/library/en-us/vblr7/html/vafmtNamedDateFormats.asp

  • bielo

    You may want to consider using a select case rather than all of those ifs and gotos...

  • kikhan_21

    Generally speaking, I'd say one of two options would be best-

    1. Check with each timer tick and when it detects the change in time--
          calculate and save the first segment
          change the rate

    or...

    2. When the timer is turned off, have logic to detect if there has been a rate change and break it out then, if so.

    I suspect one would be easier.

  • u2jrmw

    You probably mean:


    If (d.DayOfWeek = DayOfWeek.Monday) Or (d.DayOfWeek = DayOfWeek.Tuesday) Or (d.DayOfWeek = DayOfWeek.Wednesday) Or (d.DayOfWeek = DayOfWeek.Thursday) Or (d.DayOfWeek = DayOfWeek.Friday) Then
     


    in you If statements... If not, there will be many implicit conversions between integers/enums and boolean, and it is very unlikely that you will get the result that you expect.

    Best regards,
    Johan Stenberg


  • JMOdom

    Thanks, it now works perfectly...
    I would still like to know why the GoTo cmds failed though
    The above code determines whether its a week day and what time of day it is to select a particular numeric rate from a textbox, my new problem is if the timer is started and during the duration the timer is activated the rate changes eg from daytime to evening. How could I achieve this I presume they would have to be a loop mechanism with the active timer which checks the time and stops, records the first amount and then starts with the new rate and when stopped calculates both final amounts. Would this be the logical way to go

  • too many IF`s