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
AI Thread Summary
The discussion revolves around a programming issue in Visual Basic 2008 related to converting string inputs from text boxes into double values for arithmetic operations. The initial error encountered was due to attempting to assign an empty string to a variable defined as a Double, resulting in a conversion error. The user later attempted to convert text box values to Double using the CDbl function but faced issues when trying to divide these values, receiving an error indicating that the operator '/' was not defined for text box types. The solution highlighted was to ensure that arithmetic operations are performed on numeric values rather than strings or UI elements. Ultimately, the user resolved their issues by switching to C#, where they found more accessible examples and resources. The discussion emphasizes the importance of proper type conversion in programming and the challenges faced when learning a new language.
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top