Timer Problem

Conversion from string "" to type 'Double' is not va

Page 1 of 1

7 Replies - 1121 Views - Last Post: 06 January 2009 - 04:57 PM Rate Topic: -----

#1 tshauck  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 70
  • Joined: 04-January 09

Timer Problem

Post icon  Posted 04 January 2009 - 02:11 PM

I am creating a program to help my friends and I remember when to drink during the game power hour. However I am running into a problem when running the timer. Here is the code

 Public Class Form2
	Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
		Label1.Text = Label1.Text - 1
		If Label1.Text = 0 Then
			Timer1.Stop()
			My.Computer.Audio.Play(My.Resources.applause_wav, AudioPlayMode.Background)
			Form3.Show()
			Me.Close()
		End If
	End Sub

	Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
		Label2.Text = Label2.Text - 1
		If Label2.Text = 0 Then
			My.Computer.Audio.Play(My.Resources.zap1, AudioPlayMode.Background)
			Label2.Text = 60
		End If




	End Sub




	Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged, Timer1.Tick, Timer2.Tick
		'Toggles Pause?/Unpause? and Timer.start/.stop
		If CheckBox1.Checked Then
			Timer1.Enabled = False
			Timer2.Enabled = False
			CheckBox1.Text = "Unpause?"
		Else
			Timer1.Enabled = True
			Timer2.Enabled = True
			CheckBox1.Text = "Pause?"
		End If

	End Sub

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Form1.Show()
		Timer1.Enabled = False
		Timer2.Enabled = False
		Label2.Text = 60

	End Sub

	Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
		Me.Close()

	End Sub
End Class


Any help would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Timer Problem

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Timer Problem

Posted 04 January 2009 - 03:04 PM

This is VB.NET code so I'm moving this to the VB.NET forum :)
Was This Post Helpful? 0
  • +
  • -

#3 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 774
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

Re: Timer Problem

Posted 04 January 2009 - 03:23 PM

What line of code is generating the error?
Was This Post Helpful? 0
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,467
  • Joined: 29-May 08

Re: Timer Problem

Posted 04 January 2009 - 04:20 PM

The problem is caused by the original poster treating the value of the Label text as a number, when in truth the are strings.
Was This Post Helpful? 0
  • +
  • -

#5 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 774
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

Re: Timer Problem

Posted 04 January 2009 - 08:23 PM

That shouldn't be a problem because VB.NET supports implicit type conversion, so it should automatically convert string to double and double to string.
Was This Post Helpful? 0
  • +
  • -

#6 dbasnett  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 109
  • View blog
  • Posts: 603
  • Joined: 01-October 08

Re: Timer Problem

Posted 05 January 2009 - 09:26 AM

writing code based on implicit conversions is a recipe for disaster.

Option Strict On should either be the default for projects or the first line of code.
Was This Post Helpful? 0
  • +
  • -

#7 nofear217  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 14
  • View blog
  • Posts: 324
  • Joined: 08-November 07

Re: Timer Problem

Posted 05 January 2009 - 09:47 AM

Quote

writing code based on implicit conversions is a recipe for disaster.


agreed....he should at least check to make sure that there is a value within the text field and that it IsNumeric, otherwise, a much safer route would be to use a TryParse to ensure that everything is converted properly if it is able to be done.
Was This Post Helpful? 0
  • +
  • -

#8 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 774
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

Re: Timer Problem

Posted 06 January 2009 - 04:57 PM

View Postdbasnett, on 5 Jan, 2009 - 08:26 AM, said:

writing code based on implicit conversions is a recipe for disaster.

Option Strict On should either be the default for projects or the first line of code.


In practice, yes, you should rely on implicit conversion as less as possible. However, for the specified piece of code it should not generate any errors (of course, if Option Strict is disabled).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1