Results 1 to 13 of 13

Thread: Why isent this code working?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2006
    Posts
    17

    Why isent this code working?

    (...)
    Dim ex1 As Double
    Dim ex2 As Double
    Dim ex3 As Double
    Dim ex4 As Double
    Dim Sum As Double
    Sum = txtResult.Text
    ex1 = txtex1.Text
    ex2 = txtex2.Text
    ex3 = txtex3.Text
    ex4 = txtex4.Text
    Sum = ex1 + ex2 + ex3 + ex4
    txtResult.Text = (Sum)
    (...)

    For some reason this one dont work..what am i doing wrong?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Why isent this code working?

    What do each of the TextBoxes contain?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Aug 2006
    Posts
    17

    Re: Why isent this code working?

    they contain numbers

  4. #4
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Re: Why isent this code working?

    mabye provide an error? also mabye make a better title to describe ur problem so more people will help.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Aug 2006
    Posts
    17

    Re: Why isent this code working?

    there are no errors. does it matter if the txtboxes are read only? because the txtboxes has been used as answers to a math problem. and I just want to add them together. which VB wont let me do.
    User CP

  6. #6
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Re: Why isent this code working?

    Quote Originally Posted by m33pm33p
    there are no errors. does it matter if the txtboxes are read only? because the txtboxes has been used as answers to a math problem. and I just want to add them together. which VB wont let me do.

    well wat part is doing something wrong? and wat is happenning when u try? i dont c anything wrong really although i did just wake up .

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Aug 2006
    Posts
    17

    Re: Why isent this code working?

    oh.. there is no result showing up when I press the btn. nothing happens at all.
    all this code is in one btn.
    Last edited by m33pm33p; Sep 10th, 2006 at 08:42 AM.
    User CP

  8. #8
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Re: Why isent this code working?

    Quote Originally Posted by m33pm33p
    oh.. there is no result showing up when I press the btn. nothing happens at all.
    all this code is in one btn.
    add some breakpoints and debug. mabye the textboxes arent being read right. idk not much i can do with just that code seeing as it looks completely fine.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Aug 2006
    Posts
    17

    Re: Why isent this code working?

    just found out.

    Conversion from string "" to type 'Double' is not valid.

    what does that mean ?
    User CP

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Why isent this code working?

    That means that a TextBox's Text property contains a String object, not a number. If you want to assing it to a Double variable then you should be converting it to a Double object, e.g.
    VB Code:
    1. Dim myDouble As Double = Convert.ToDouble(myTextBox.Text)
    That will convert a valid string to a Double. It doesn't account for the fact that the user might not enter a valid string. If you need to account for that then you should use the Double.TryParse method. I'll leave it up to you to research that yourself if you need it. Similarly, when you performed your calculations you should convert the number back to a string before assigning it to the TextBox, e.g.
    VB Code:
    1. myTextBox.Text = myDouble.ToString()

    Edit: Actually, I've read your last post again and I realised that the issue is because one of the TextBoxes contains an empty string, which is exactly why I asked you what the TextBoxes contained in the first reply. Obviously at least one of your TextBoxes doesn't contain a number as you said.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11
    Fanatic Member
    Join Date
    May 2001
    Posts
    837

    Re: Why isent this code working?

    string "" means there are no characters there, and Double is a type of number. It's like if I gave you a blank sheet of paper and asked you what number is written on it. One of those textboxes doesn't contain a number. Your code indicates they all have to contain a number in order for it to execute without error.

    EDIT: It took me at least 3 minutes to write this?!
    The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Aug 2006
    Posts
    17

    Re: Why isent this code working?

    well.. i have numbers in (ex1, ex2, ex3, ex4) txtboxes. so I dont see the problem.
    User CP

  13. #13
    Fanatic Member
    Join Date
    May 2001
    Posts
    837

    Re: Why isent this code working?

    all the textboxes means ALL the textboxes. If you examine the code you've written it tries to get a value from the txtResult textbox before getting values from the other textboxes.
    The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width