10 Replies - 3730 Views - Last Post: 11 December 2009 - 04:29 AM Rate Topic: -----

#1 dominion_vortar  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 52
  • Joined: 07-November 09

Conversion from string to double is not valid..

Posted 10 December 2009 - 11:24 AM

I've been at this for hours.. No idea where I'm going wrong! I have a form to calculate charges based on input data and charges stored in an array. When I try to save an entry to a file my program crashes and I get the error "Conversion from string "" to type 'Double' is not valid.

If rdoDay.Checked = True Then
			Quote.DrivingHrs = txtDrivingHours.Text
			Quote.WaitingCount = txtWaiting.Text
			Quote.TripCost = (Quote.F75Mileage * DayCharge(2)) + (Quote.AddMileage * DayCharge(3)) + (Quote.DrivingHrs * DayCharge(0)) + (Quote.WaitingCount * DayCharge(1))
		ElseIf rdoOvernight.Checked = True Then
			Quote.DrivingHrs = txtDrivingHours.Text
			Quote.OverCount = txtNights.Text
			Quote.TripCost = (Quote.F75Mileage * OverCharge(2)) + (Quote.AddMileage * OverCharge(3)) + (Quote.DrivingHrs * OverCharge(0)) + (Quote.OverCount * OverCharge(1))
		End If



The error highlights the last line of the if statement beginning "Quote.TripCost" as the error, but I don't understand why..

Any help is appreciated, thanks.
(sorry if this is a basic question, i'm new at this..) :v:

Is This A Good Question/Topic? 0
  • +

Replies To: Conversion from string to double is not valid..

#2 Metitron  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 78
  • Joined: 13-October 09

Re: Conversion from string to double is not valid..

Posted 10 December 2009 - 11:37 AM

well it could be that if your arrays are declared as strings that would cause the problem but i would need to see more code befor ei could really help you.
Was This Post Helpful? 0
  • +
  • -

#3 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Conversion from string to double is not valid..

Posted 10 December 2009 - 11:39 AM

It's because you're trying to set an empty string to some double variable.

THIS WILL WORK
Dim a As Double
a = 5

THIS WILL WORK
Dim b As Double
b = 7.1

THIS WILL NOT WORK (same thing you're trying to do)
Dim c As Double
c = ""
Was This Post Helpful? 0
  • +
  • -

#4 dominion_vortar  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 52
  • Joined: 07-November 09

Re: Conversion from string to double is not valid..

Posted 10 December 2009 - 11:39 AM

View PostMetitron, on 10 Dec, 2009 - 10:37 AM, said:

well it could be that if your arrays are declared as strings that would cause the problem but i would need to see more code befor ei could really help you.

The form consists of around 200 lines

Imports System.IO

Public Class frmNewQuote
	Dim Filename As String
	Dim quotecount As Short
	Dim lstQuoteFormat As String = "{0,1}{1,1}{2,1}"
	Dim intval As Integer
	Dim DayChargeDesc(3), OverChargeDesc(3), DayCharge(3), OverCharge(3) As String

	'Structure created for storing quote details
	Structure QuoteFile
		Dim CustID As String
		Dim QuoteID As String
		Dim Mileage As String
		Dim DrivingHrs As Integer
		Dim F75Mileage As Integer
		Dim AddMileage As Integer
		Dim WaitingCount As Single
		Dim WaitingCharge As Single
		Dim OverCount As Single
		Dim OverCharge As Single
		Dim TripCost As Single
		Dim VATCharge As Single
		Dim TotalCost As Single
		Dim StartTime As Date
		Dim EndTime As Date

	End Structure

	Dim Quote As QuoteFile


	Structure CustomerFile
		<VBFixedString(15)> Public CustID As String
		<VBFixedString(15)> Public CustForename As String
		<VBFixedString(15)> Public CustSurname As String
		<VBFixedString(15)> Public CustAddress1 As String
		<VBFixedString(15)> Public CustAddress2 As String
		<VBFixedString(15)> Public CustPostcode As String
		<VBFixedString(15)> Public CustPhone As String
		<VBFixedString(30)> Public CustEmail As String
	End Structure

	Dim Customer As CustomerFile

	Private Sub frmNewQuote_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

		'Setting up the Array

		Dim FileNameforOverCharge As String
		Dim FileNameforDayCharge As String
		Dim DayCount As Integer
		Dim OverCount As Integer

		Dim CurrentFileReader As StreamReader

		FileNameforDayCharge = "F:\College\Computing\\Program\Berties Buses/daycharge.txt"
		FileNameforOverCharge = "F:\College\Computing\\Program\Berties Buses/overcharge.txt"

		CurrentFileReader = New StreamReader(FileNameforDayCharge)

		For DayCount = 0 To 3
			DayChargeDesc(DayCount) = CurrentFileReader.ReadLine
			DayCharge(DayCount) = CurrentFileReader.ReadLine
		Next

		For OverCount = 0 To 3
			OverChargeDesc(OverCount) = CurrentFileReader.ReadLine
			OverCharge(OverCount) = CurrentFileReader.ReadLine
		Next

		CurrentFileReader.Close()


	End Sub


	Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
		Me.Visible = False
		frmMainMenu.Visible = True
	End Sub

	Private Sub NewCustomerToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewCustomerToolStripMenuItem.Click
		Me.Visible = False
		frmNewCust.Visible = True
	End Sub

	Private Sub QuitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles QuitToolStripMenuItem.Click
		Me.Close()
	End Sub

	Private Sub txtQuoteID_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCustID.TextChanged

		txtQuoteID.Text = "Q" & txtCustID.Text & "1"
	End Sub

   

	Private Sub btnMainMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMainMenu.Click
		Me.Hide()
		frmMainMenu.Show()
	End Sub

	Private Sub rdoDay_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdoDay.CheckedChanged

		txtNights.ReadOnly = True
		txtWaiting.BackColor = Color.Gainsboro
		txtWaiting.Cursor = Cursors.No

		txtWaiting.ReadOnly = False
		txtWaiting.BackColor = Color.White
		txtWaiting.Cursor = Cursors.Default

	End Sub

	Private Sub rdoOvernight_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdoOvernight.CheckedChanged

		txtNights.ReadOnly = False
		txtWaiting.BackColor = Color.White
		txtWaiting.Cursor = Cursors.Default

		txtWaiting.ReadOnly = True
		txtWaiting.BackColor = Color.Gainsboro
		txtWaiting.Cursor = Cursors.No

	End Sub

	Private Sub btnSaveCust_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveCust.Click

		Dim intvalmsg As DialogResult
		Dim errormsg As DialogResult

		If txtCustID.Text = "" Or _
			txtQuoteID.Text = "" Or _
			txtMileage.Text = "" Then
			errormsg = MessageBox.Show("Please enter information in the required fields", "Bertie's Buses", MessageBoxButtons.OK, MessageBoxIcon.Stop)
		End If

		If errormsg = Windows.Forms.DialogResult.OK Then
			'Do nothing
		Else
			Quote.Mileage = txtMileage.Text()
		End If

		Call MileValidation()

		If intval = 1 Then
			intvalmsg = MessageBox.Show("Please enter a value over 20", "Bertie's Buses", MessageBoxButtons.OK, MessageBoxIcon.Stop)
		End If

		If intvalmsg = Windows.Forms.DialogResult.OK Then
			'Do nothing
		Else
			Call MileCalculate()
		End If

		Call CostCalculate()

		Quote.TotalCost = Quote.TripCost * Quote.VATCharge

		'Writes information to the file
		FileOpen(1, Filename, OpenMode.Random, , , Len(Customer))
		FilePut(1, Customer, quotecount + 1)
		FileClose(1)

		quotecount = quotecount + 1

	End Sub
	Private Sub MileValidation()

		If Quote.Mileage < 20 Then
			intval = 1
		Else
			intval = 0
		End If

	End Sub

	Private Sub MileCalculate()

		If Quote.Mileage <= 75 Then
			Quote.F75Mileage = Quote.Mileage
			Quote.AddMileage = 0
		End If

		If Quote.Mileage > 75 Then
			Quote.F75Mileage = 75
			Quote.AddMileage = Quote.Mileage - 75
		End If

	End Sub

	Private Sub CostCalculate()

		If rdoDay.Checked = True Then
			Quote.DrivingHrs = txtDrivingHours.Text
			Quote.WaitingCount = txtWaiting.Text
			Quote.TripCost = (Quote.F75Mileage * DayCharge(2)) + (Quote.AddMileage * DayCharge(3)) + (Quote.DrivingHrs * DayCharge(0)) + (Quote.WaitingCount * DayCharge(1))
		ElseIf rdoOvernight.Checked = True Then
			Quote.DrivingHrs = txtDrivingHours.Text
			Quote.OverCount = txtNights.Text
			Quote.TripCost = (Quote.F75Mileage * OverCharge(2)) + (Quote.AddMileage * OverCharge(3)) + (Quote.DrivingHrs * OverCharge(0)) + (Quote.OverCount * OverCharge(1))
		End If

	End Sub
End Class


Was This Post Helpful? 0
  • +
  • -

#5 dominion_vortar  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 52
  • Joined: 07-November 09

Re: Conversion from string to double is not valid..

Posted 10 December 2009 - 03:26 PM

Any ideas at all?
Was This Post Helpful? 0
  • +
  • -

#6 Metitron  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 78
  • Joined: 13-October 09

Re: Conversion from string to double is not valid..

Posted 10 December 2009 - 03:44 PM

yeah you declare your arrays as strings not as doubles and strings<>doubles you can either declare them as doubles or typecast them as doubles later either will work but do neither wont
Was This Post Helpful? 0
  • +
  • -

#7 Bort  Icon User is offline

  • Ill-informed Mongoloid
  • member icon

Reputation: 401
  • View blog
  • Posts: 2,958
  • Joined: 18-September 06

Re: Conversion from string to double is not valid..

Posted 11 December 2009 - 03:34 AM

You are using strings from TextBoxes in your calculations. They need to be converted into doubles before you do anything with them.

A very simple solution, just modifying your code from your OP should look like this:

If rdoDay.Checked = True Then
			Quote.DrivingHrs = CDbl(txtDrivingHours.Text)
			Quote.WaitingCount = CDbl(txtWaiting.Text)
			Quote.TripCost = (Quote.F75Mileage * DayCharge(2)) + (Quote.AddMileage * DayCharge(3)) + (Quote.DrivingHrs * DayCharge(0)) + (Quote.WaitingCount * DayCharge(1))
		ElseIf rdoOvernight.Checked = True Then
			Quote.DrivingHrs = CDbl(txtDrivingHours.Text)
			Quote.OverCount = CDbl(txtNights.Text)
			Quote.TripCost = (Quote.F75Mileage * OverCharge(2)) + (Quote.AddMileage * OverCharge(3)) + (Quote.DrivingHrs * OverCharge(0)) + (Quote.OverCount * OverCharge(1))
		End If



You should also check the data types for the variables you use here that aren't given values here, F75Mileage for example.

HTH :)

Edit: Oh, and if you are using the contents of TextBoxes to work with numbers, you might want to take a look at IsNumeric to stop people typing 'Bob' or something equally silly into your 'Driving Hours' box.

This post has been edited by Bort: 11 December 2009 - 03:36 AM

Was This Post Helpful? 0
  • +
  • -

#8 dominion_vortar  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 52
  • Joined: 07-November 09

Re: Conversion from string to double is not valid..

Posted 11 December 2009 - 04:07 AM

Thanks for the response - same error still occuring "Conversion from string "" to type 'Double' is not valid." on the last line of code though :v:

 Quote.TripCost = (Quote.F75Mileage * OverCharge(2)) + (Quote.AddMileage * OverCharge(3)) + (Quote.DrivingHrs * OverCharge(0)) + (Quote.OverCount * OverCharge(1))



Are there anymore possible solutions for this? Maybe because the arrays are declared as strings? I tried delaring the array values as single, but then I got the same 'String to single' conversion error when streamreading the text file that the charges are stored in.

Thanks again.

This post has been edited by dominion_vortar: 11 December 2009 - 04:09 AM

Was This Post Helpful? 0
  • +
  • -

#9 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Conversion from string to double is not valid..

Posted 11 December 2009 - 04:10 AM

I think that everything has been already said. There are no other solutions possible. Read through all the answers in this thread and find where exactly in your code you have the problem that has been described by various users in this thread.

There is 100% something wrong with your code and you have been given very good answers on how to correct it.
Was This Post Helpful? 0
  • +
  • -

#10 Bort  Icon User is offline

  • Ill-informed Mongoloid
  • member icon

Reputation: 401
  • View blog
  • Posts: 2,958
  • Joined: 18-September 06

Re: Conversion from string to double is not valid..

Posted 11 December 2009 - 04:14 AM

Put a break point on that line, then when you run your application, take a look at the Autos window in VB and check the values for everything. If you are converting everything to double, but it is still having problems, there might be something wrong with one of your variables in there.

After all, it is saying it is trying to convert "" into a double which could be an empty string. Are any of the values in your error line '0'?

Also, does this line work as it is meant to?

Quote.TripCost = (Quote.F75Mileage * DayCharge(2)) + (Quote.AddMileage * DayCharge(3)) + (Quote.DrivingHrs * DayCharge(0)) + (Quote.WaitingCount * DayCharge(1))



Or does it give more errors? If it works, then the problem is either the variable OverCount, or the array OverCharge since they are the only differences between the 2 lines.

This post has been edited by Bort: 11 December 2009 - 04:19 AM

Was This Post Helpful? 1
  • +
  • -

#11 dominion_vortar  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 52
  • Joined: 07-November 09

Re: Conversion from string to double is not valid..

Posted 11 December 2009 - 04:29 AM

Tested the day charges, and it works OK. Will try to see what the problem with overcount / charge is. Thanks.

This post has been edited by dominion_vortar: 11 December 2009 - 04:47 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1