VB Help: Fix "Conversion from string to type 'Double' is not valid" Error

  • Thread starter Thread starter youngsc
  • Start date Start date
  • Tags Tags
    Visual
Click For Summary
SUMMARY

The forum discussion addresses a common error in Visual Basic 2008: "Conversion from string to type 'Double' is not valid." The user encountered this issue while attempting to convert text box inputs into Double data types for arithmetic operations. The solution involves using the CDbl function to convert string inputs to Double before performing calculations. Additionally, the user switched to C# for further learning, citing a preference for its resources over those available for VB.

PREREQUISITES
  • Understanding of Visual Basic 2008 syntax and data types
  • Familiarity with the CDbl function for type conversion
  • Basic knowledge of event handling in Windows Forms applications
  • Concept of arithmetic operations on numeric types
NEXT STEPS
  • Learn about error handling in Visual Basic 2008 to manage runtime exceptions
  • Explore data validation techniques for user input in Windows Forms
  • Study the differences between Visual Basic and C# for better cross-language understanding
  • Investigate best practices for managing UI elements and data types in .NET applications
USEFUL FOR

Beginner programmers, Visual Basic developers, and anyone interested in transitioning from VB to C# or improving their understanding of type conversion and event handling in .NET applications.

youngsc
Messages
3
Reaction score
0
Ok I'm just starting to teach myself and I thought I could have at least gotten this part but I'm getting a Conversion from string "" to type 'Double' is not valid. error message.

Code:
Private Sub StockPricetxt_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StockPricetxt.TextChanged
        Dim StockPricetxt As Double
        StockPricetxt = ""
    End Sub

What's happening when I test it is the second you put anything in the text box it freezes and return the error message above. The larger code will be to take this value and another and divide them. I'm assuming it has to do with the use of double but I'm lost. I would appreciate any help you all could give me. Using VB 2008 btw.


Also, great forum I've been surfing it for a little while now and it's a lot of good info to be had.
 
Technology news on Phys.org
Ok that problem is solved now I got a new one.

Code:
    Private Sub StockPricetxt_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StockPricetxt.TextChanged
        Dim StockPricetxt As Double
        StockPricetxt = CDbl(StockPricetxt)

    End Sub

    Private Sub EPStxt_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EPStxt.TextChanged
        Dim EPStxt As Double
        EPStxt = CDbl(EPStxt)
    End Sub

    [B]Private Sub PEtxt_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PEtxt.TextChanged
        Dim PEtxt As Double 
        PEtxt = StockPricetxt / EPStxt[/B]

    End Sub

I'm trying to divide these values and the last sub bolded I'm having trouble with. It's telling me "Operator '/' is not defined for types 'System.Windows.Forms.TextBox'". Any ideas? I am searching this stuff on the web and can't come up with exact results to give me what I need.

TIA
 
Are you attempting to do arithmetic on text? (i.e. a string?).

Convert the text to a double before attempting the divide.
 
zeitghost said:
Are you attempting to do arithmetic on text? (i.e. a string?).

Convert the text to a double before attempting the divide.

Thanks for the reply. I got it figured out, I used C# instead. I'm trying to teach myself quite a bit of this and I'm doing as much of it "example" based as possible. I couldn't find any good examples in VB but found a lot in C#. Thanks again.
 
C# is better than Visual Basic, to be sure. Just be sure to do math on numbers, not strings or UI elements.
 

Similar threads

Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K