10 Replies - 2310 Views - Last Post: 07 July 2011 - 04:09 AM Rate Topic: -----

#1 Ahleki  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 01-June 10

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

Posted 05 July 2011 - 03:30 AM

Quote

Hi am trying to convert a string using the function below but i can't seem to figure out the problem
    Function calTotalPrice(ByRef u As Double, ByRef q As Double, ByRef v As Double) As Object
        Return txtTotalPrice.Text = CStr(u * q)
    End Function



The error is occuring at this point:
    Private Sub txtUnitPrice_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtUnitPrice.TextChanged
Dim X as String
        If (txtUnitPrice.Text = "") Then
            txtUnitPrice.Text = 0
            txtUnitPrice.Refresh()
            txtUnitPrice.Focus()
            System.Windows.Forms.SendKeys.Send(("{Home}+{End}"))
        End If

        If (txtVat.Text = "") Then
            txtVat.Text = 0
            txtVat.Refresh()
        End If

        If (IsNumeric(txtVat.Text) = False) Then
            Exit Sub
        End If

        If (IsNumeric(txtUnitPrice.Text) = False) Then
            MsgBox("Numeric value please", vbExclamation)
            txtQuantity.Focus()
            System.Windows.Forms.SendKeys.Send(("{Home}+{End}"))

            Exit Sub
        End If
        X = calTotalPrice(CDbl(txtUnitPrice.Text), CDbl(txtQuantity.Text), CDbl(txtVat.Text)) : exception being thrown at this point (Conversion from string "" to type 'Double' is not valid)
        txtTotalPrice.Refresh()

    End Sub




Thanks in advance


Is This A Good Question/Topic? 0
  • +

Replies To: Conversion from string "" to type 'Double' is not vali

#2 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,057
  • Joined: 02-July 08

Re: Conversion from string "" to type 'Double' is not vali

Posted 05 July 2011 - 03:40 AM

Your function should be a String type since that is what it is setting, and you don't put an expression like that in the return statement. And those ByRef should be ByVal.
Function calTotalPrice(ByVal u As Double, ByVal q As Double, ByVal v As Double) As String
    Return (u * q).ToString
End Function



txtTotalPrice.Text = calTotalPrice(CDbl(txtUnitPrice.Text), CDbl(txtQuantity.Text), CDbl(txtVat.Text))

Was This Post Helpful? 1
  • +
  • -

#3 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,501
  • Joined: 16-March 11

Re: Conversion from string "" to type 'Double' is not vali

Posted 05 July 2011 - 08:33 AM

When you use a function you are getting something back, that means it's not necessarily supposed to perform like a subroutine. You can give the cashier 2 dollars for a bag of chips, but the cashier won't put your chips in your car for you. :)
Was This Post Helpful? 0
  • +
  • -

#4 dbasnett  Icon User is offline

  • D.I.C Addict

Reputation: 108
  • View blog
  • Posts: 601
  • Joined: 01-October 08

Re: Conversion from string "" to type 'Double' is not vali

Posted 05 July 2011 - 08:36 AM

A couple of things that would help in the long run.

Make these two lines the first in your code(preferably change the options permanently):

Option Strict On
Option Explicit On


Also look at the Parse, TryParse numeric methods.
Was This Post Helpful? 2
  • +
  • -

#5 Btu  Icon User is offline

  • D.I.C Regular

Reputation: 36
  • View blog
  • Posts: 250
  • Joined: 16-May 11

Re: Conversion from string "" to type 'Double' is not vali

Posted 05 July 2011 - 08:40 AM

Private Function calTotalPrice(ByVal u As Double, ByVal q As Double, ByVal v As Double) As Double
dim result as double = r * q
return result  
End Function

dim total as double = calTotalPrice(CDbl(txtUnitPrice.Text), CDbl(txtQuantity.Text), CDbl(txtVat.Text))
txtTotalPrice.Text = total.tostring


I also don't recommend using CDbl as it's deprecated.

use double.parse('data to parse') or double.tryparse(data, variable)
Was This Post Helpful? 0
  • +
  • -

#6 Psyguy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 69
  • View blog
  • Posts: 314
  • Joined: 12-January 11

Re: Conversion from string "" to type 'Double' is not vali

Posted 06 July 2011 - 07:45 AM

I continue to wonder how some of you guys know that certain methods/functions are deprecated...I have been using Cdbl/Cint for months now (been a VB.NET programmer for 6 months) and this is the first I've heard that.
Was This Post Helpful? 0
  • +
  • -

#7 Btu  Icon User is offline

  • D.I.C Regular

Reputation: 36
  • View blog
  • Posts: 250
  • Joined: 16-May 11

Re: Conversion from string "" to type 'Double' is not vali

Posted 06 July 2011 - 08:17 AM

View PostPsyguy, on 06 July 2011 - 07:45 AM, said:

I continue to wonder how some of you guys know that certain methods/functions are deprecated...I have been using Cdbl/Cint for months now (been a VB.NET programmer for 6 months) and this is the first I've heard that.

I think it's just because we're in university, and our teachers are trying to teach us the best practices. I prefer CDbl to be perfectly honest, but our VB instructor was amazing and I'm just doing what I'm told.
Was This Post Helpful? 0
  • +
  • -

#8 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,057
  • Joined: 02-July 08

Re: Conversion from string "" to type 'Double' is not vali

Posted 06 July 2011 - 08:54 AM

They aren't depreciated, yet. They do come from VB6 era. So as long as we have a reference to the Microsoft.VisualBasic namespace we are good.
Was This Post Helpful? 1
  • +
  • -

#9 Btu  Icon User is offline

  • D.I.C Regular

Reputation: 36
  • View blog
  • Posts: 250
  • Joined: 16-May 11

Re: Conversion from string "" to type 'Double' is not vali

Posted 06 July 2011 - 09:03 AM

True enough. I was just pointing it out B)
Was This Post Helpful? 0
  • +
  • -

#10 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,501
  • Joined: 16-March 11

Re: Conversion from string "" to type 'Double' is not vali

Posted 06 July 2011 - 10:25 AM

One thing I love about .Net, and Visual Basic is that there is always a way. Even though methods are deprecated, they are still available, and sometimes they do just the right tricks. :) They are pretty good about telling you what methods, and functions are deprecated though.
Was This Post Helpful? 0
  • +
  • -

#11 Ahleki  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 01-June 10

Re: Conversion from string "" to type 'Double' is not vali

Posted 07 July 2011 - 04:09 AM

Thanks y'll for the help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1