|
-
Nov 12th, 2010, 08:09 PM
#1
Thread Starter
New Member
Substring Help
I'm having issues with this program, I keep getting an error with the substrings on the "second" part...
It's an oven timer ...display is supposed to be like this 1:11:11, hour minute second....any help will be greatly appreciated....thank you
Code:
Private Sub startButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles startButton.Click
Dim second As Integer
Dim minute As Integer
Dim hour As Integer
' ensure that timeIs has 5 characters
timeIs = timeIs.PadLeft(5, "0"c)
' extract seconds minutes and hours
second = Convert.ToInt32(timeIs.Substring(3))
minute = Convert.ToInt32(timeIs.Substring(1, 2))
hour = Convert.ToInt32(timeIs.Substring(0, 1))
' create Time object to contain time entered by user
timeObject = New Time(hour, minute, second)
displayLabel.Text = String.Format("{0:D1}:{1:D2}:{2:D2}", timeObject.Hour, timeObject.Minute, timeObject.Second)
timeIs = "" ' clear timeIs for future input
clockTimer.Enabled = True ' start timer
windowPanel.BackColor = Color.Yellow ' turn "light" on
End Sub ' startButton_Click
' event handler to clear input
Private Sub clearButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles clearButton.Click
' reset each property or variable to its initial setting
displayLabel.Text = "Microwave Oven"
timeIs = ""
timeObject = Nothing
clockTimer.Enabled = False
windowPanel.BackColor = Control.DefaultBackColor
End Sub ' clearButton_Click
' method to display formatted time in timer window
Private Sub DisplayTime()
Dim hour As Integer
Dim second As Integer
Dim minute As Integer
Dim display As String ' String displays current input
' if too much input entered
If timeIs.Length > 5 Then
timeIs = timeIs.Substring(0, 5)
End If
display = timeIs.PadLeft(5, "0"c)
' extract seconds minutes and hours
second = Convert.ToInt32(timeIs.Substring(3))
minute = Convert.ToInt32(timeIs.Substring(1, 2))
hour = Convert.ToInt32(timeIs.Substring(0, 1))
' display number of hours ":" minutes, ":" and number of seconds
displayLabel.Text = String.Format("{0:D1}:{1:D2}:{2:D2}", hour, minute, second)
End Sub ' DisplayTime
' event handler displays new time each second
Private Sub clockTimer_Tick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles clockTimer.Tick
' perform countdown, subtract one second
If timeObject.Second > 0 Then
timeObject.Second -= 1
displayLabel.Text = String.Format("{0:D1}:{1:D2}:{2:D2}", timeObject.Hour, timeObject.Minute, timeObject.Second)
ElseIf timeObject.Minute > 0 Then
timeObject.Minute -= 1
timeObject.Second = 59
displayLabel.Text = String.Format("{0:D1}:{1:D2}:{2:D2}", timeObject.Hour, timeObject.Minute, timeObject.Second)
ElseIf timeObject.Hour > 0 Then
timeObject.Hour -= 1
timeObject.Minute = 59
timeObject.Second = 59
displayLabel.Text = String.Format("{0:D1}:{1:D2}:{2:D2}", timeObject.Hour, timeObject.Minute, timeObject.Second)
Else ' countdown finished
clockTimer.Enabled = False ' stop timer
Beep()
displayLabel.Text = "Done!" ' inform user time is finished
windowPanel.BackColor = Control.DefaultBackColor
End If
End Sub ' clockTimer_Tick
End Class ' MicrowaveOvenForm
Code:
Public Class Time
' declare Integers for hour minute and second
Private minuteValue As Integer
Private secondValue As Integer
Private hourValue As Integer
' Time constructor, minute and second supplied
Public Sub New(ByVal hh As Integer, ByVal mm As Integer, ByVal ss As Integer)
Hour = hh ' invokes Hour set accessor
Minute = mm ' invokes Minute set accessor
Second = ss ' invokes Second set accessor
End Sub ' New
' property Hour
Public Property Hour() As Integer
' return Hour value
Get
Return hourValue
End Get ' end of Get accessor
' set Hour value
Set(ByVal value As Integer)
'if hour value entered is valid
If (value < 10) Then
hourValue = value
Else
hourValue = 0 ' set invalid input to 0
End If
End Set ' end of Set accessor
End Property ' Hour
' property Minute
Public Property Minute() As Integer
' return Minute value
Get
Return minuteValue
End Get ' end of Get accessor
' set Minute value
Set(ByVal value As Integer)
'if minute value entered is valid
If (value < 60) Then
minuteValue = value
Else
minuteValue = 0 ' set invalid input to 0
End If
End Set ' end of Set accessor
End Property ' Minute
' property Second
Public Property Second() As Integer
' return Second value
Get
Return secondValue
End Get ' end of Get accessor
' set Second Value
Set(ByVal value As Integer)
' if second value entered is valid
If (value < 60) Then
secondValue = value
Else
secondValue = 0 ' set invalid input to 0
End If
End Set ' end of Set accessor
End Property ' Second
End Class ' Time
-
Nov 12th, 2010, 08:21 PM
#2
Re: Substring Help
it'd be easier to split timeIs on ":"
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 12th, 2010, 08:24 PM
#3
Re: Substring Help
Are you sure that the string is long enough to take the substring of it?
Substring is notorious for throwing a silly error, if the string isn't long enough, which requires that you check its length first.
-
Nov 12th, 2010, 08:48 PM
#4
Re: Substring Help
It's how the "seconds" are being extracted.... substring(3) means take the LEFT 3 characters.... "1:1" in this case... Which I doubt is what you want... Also, your extraction of the minute is wrong... in that case you're going to get ":1" which will also error out when you convert it to int.
-tg
-
Nov 12th, 2010, 08:59 PM
#5
Thread Starter
New Member
Re: Substring Help
 Originally Posted by techgnome
It's how the "seconds" are being extracted.... substring(3) means take the LEFT 3 characters.... "1:1" in this case... Which I doubt is what you want... Also, your extraction of the minute is wrong... in that case you're going to get ":1" which will also error out when you convert it to int.
-tg
I don't think the : is supposed to be part of the string, I'm just clicking buttons and it's adding each number to it, then the : is just on the output...unless I did it wrong......the original one had minutes and seconds, and I had to add the hour....
This is part of the original
Code:
Private Sub DisplayTime()
Dim second As Integer
Dim minute As Integer
Dim display As String ' String displays current input
' if too much input entered
If timeIs.Length > 4 Then
timeIs = timeIs.Substring(0, 4)
End If
display = timeIs.PadLeft(4, "0"c)
' extract seconds and minutes
second = Convert.ToInt32(display.Substring(2))
minute = Convert.ToInt32(display.Substring(0, 2))
' display number of minutes, ":" and number of seconds
displayLabel.Text = String.Format("{0:D2}:{1:D2}", _
minute, second)
End Sub ' DisplayTime
-
Nov 12th, 2010, 09:37 PM
#6
Re: Substring Help
dkm278, why don't you store each part of the timer in their own integer variables, then just increment/decrement those variables as needed and re-show them as a string with the colon separating them? It'd be far easier than parsing a string into integer variables all the time like that.
-
Nov 12th, 2010, 09:43 PM
#7
Re: Substring Help
dkm - here's the thing... you're tossing code out at us without any explanation on what any thing is or what the data looks like... you're manipulating something called timeIs .... what is that? What does it hold? I assumed it was the "1:11:11" ... but now you're saying that's not the case...
-tg
-
Nov 13th, 2010, 04:15 AM
#8
Thread Starter
New Member
Re: Substring Help
 Originally Posted by techgnome
dkm - here's the thing... you're tossing code out at us without any explanation on what any thing is or what the data looks like... you're manipulating something called timeIs .... what is that? What does it hold? I assumed it was the "1:11:11" ... but now you're saying that's not the case...
-tg
This is the beginning/rest of the code...
Code:
Public Class MicrowaveOvenForm
' contains time entered as a String
Private timeIs As String = ""
' contains time entered
Private timeObject As Time
' event handler appends 1 to time string
Private Sub oneButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles oneButton.Click
Beep() ' sound beep
timeIs &= "1" ' append digit to time input
DisplayTime() ' display time input properly
End Sub ' oneButton_Click
' event handler appends 2 to time string
Private Sub twoButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles twoButton.Click
Beep() ' sound beep
timeIs &= "2" ' append digit to time input
DisplayTime() ' display time input properly
End Sub ' twoButton_Click
' event handler appends 3 to time string
Private Sub threeButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles threeButton.Click
Beep() ' sound beep
timeIs &= "3" ' append digit to time input
DisplayTime() ' display time input properly
End Sub ' threeButton_Click
' event handler appends 4 to time string
Private Sub fourButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles fourButton.Click
Beep() ' sound beep
timeIs &= "4" ' append digit to time input
DisplayTime() ' display time input properly
End Sub ' fourButton_Click
' event handler appends 5 to time string
Private Sub fiveButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles fiveButton.Click
Beep() ' sound beep
timeIs &= "5" ' append digit to time input
DisplayTime() ' display time input properly
End Sub ' fiveButton_Click
' event handler appends 6 to time string
Private Sub sixButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles sixButton.Click
Beep() ' sound beep
timeIs &= "6" ' append digit to time input
DisplayTime() ' display time input properly
End Sub ' sixButton_Click
' event handler appends 7 to time string
Private Sub sevenButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles sevenButton.Click
Beep() ' sound beep
timeIs &= "7" ' append digit to time input
DisplayTime() ' display time input properly
End Sub ' sevenButton_Click
' event handler appends 8 to time string
Private Sub eightButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles eightButton.Click
Beep() ' sound beep
timeIs &= "8" ' append digit to time input
DisplayTime() ' display time input properly
End Sub ' eightButton_Click
' event handler appends 9 to time string
Private Sub nineButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles nineButton.Click
Beep() ' sound beep
timeIs &= "9" ' append digit to time input
DisplayTime() ' display time input properly
End Sub ' nineButton_Click
' event handler appends 0 to time string
Private Sub zeroButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles zeroButton.Click
Beep() ' sound beep
timeIs &= "0" ' append digit to time input
DisplayTime() ' display time input properly
End Sub ' zeroButton_Click
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|