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?
Re: Why isent this code working?
What do each of the TextBoxes contain?
Re: Why isent this code working?
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.
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.
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 :p .
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.
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.
Re: Why isent this code working?
just found out.
Conversion from string "" to type 'Double' is not valid.
what does that mean ?
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:
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:
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.
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?!
Re: Why isent this code working?
well.. i have numbers in (ex1, ex2, ex3, ex4) txtboxes. so I dont see the problem.
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.