"ryan" <ry**@discussions.microsoft.com> wrote in message
news:93**********************************@microsof t.com...
From the looks of the post this is going to be a remedial question. I am
currently working on my CIS degree and I am in VB.net this semester, so I
am
extremely new to all this. I'm trying create a program that will allow me
to
average 5 numbers. Right now my calculation code reads as follows
Private Sub btncalculate_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btncalculate.Click
Label7.Text = Val(txtscore1.Text) + (txtscore2.Text) _
+ (txtscore3.Text) + (txtscore4.Text) _
+ (txtscore5.Text) \ 5
However it will come out with the wrong average. If I take out the \ 5 ;
it
will add everything perfect, so my question is how do I get it to divide.
Any
help will be appreciated.
Thanks,
Ryan
ryan,
What data type are the numbers? Double, Int??? In the case of double, just
replace Integer with Double :)
Label7.Text = (Integer.Parse(txtscore1.Text) + Integer.Parse(txtscore2.Text)
+ _
Integer.Parse(txtscore3.Text) + Integer.Parse(txtscore4.Text) + _
Integer.Parse(txtscore5.Text)) / 5
Basically, the order in which the compiler calculates this expression is
different than how I believe you think it is. The compiler, just as it
should, doesn't add all of the numbers together then divides...the division
operator is of higher precedence than the addition operator, therefore it
does txtscore5.Text \ 5 before the additions of the numbers.
By wrapping all of the additions together using parenthesis, we are forcing
the compiler to calculate the expression(s) inside the parenthesis prior to
calculating the expression(s) outside the parenthesis.
Integer.Parse is a more .Net'ish way of converting a string to an integer :)
Hope this helps :)
Mythran