Decimator

I need VB to add a series of values which are stored in text boxes. The code which i am using is:
txtTotalQ.Text = txtQuantity1.Text + txtQuantity2.Text + txtQuantity3.Text + txtQuantity4.Text + txtQuantity5.Text + txtQuantity6.Text

However it doesn't actually add the numbers up, it just combines them together

For example instead of 10+10 = 20
I get 10+10 = 1010

I know their is a basic solution to this, i just cant seem to come up with one

(the values in the txtQuanitiy's text boxes, are variables that have been declared as integers)



Re: Visual Basic Express Edition Adding Number in VB

Omnicoder

use:

Code Snippet

txtTotalQ.Text = (txtQuantity1.Text + txtQuantity2.Text + txtQuantity3.Text + txtQuantity4.Text + txtQuantity5.Text + txtQuantity6.Text)






Re: Visual Basic Express Edition Adding Number in VB

spotty

The + with text will be use for appending text

ie.

"A" + "B" = "AB"

However you want to treat this as numeric data, so type is especially important.

Example

Code Snippet
txtTotalQ.Text = Ctype( Val(txtQuantity1.Text) + Val(txtQuantity2.Text )+ Val(txtQuantity3.Text ), String)

So the correct way would be to take the value of each of the quantity field and add them together. and the resulting value you can convert to a string - which is what the textbox text property should be.

If you turn Option Strict On at the top of your source code then this will ensure that you have the correct types and will not do the implied conversions.

You can use the VB specific Conversion methods Val, Cstr etc. or you can use the .NET Ctype method to cast to a specific type. Or mix and match them as I did above.

Either way its ensuring that you using numeric types with the + operator and ensuring your converting to a string type to set the text property.






Re: Visual Basic Express Edition Adding Number in VB

PsychUK

LOL, thats because the + operand is concatenate for string types, u need to convert them first, use Cint(textquantity1.text) etc etc (assuming your textboxes do not cross the integer threshhold, if so, use Clng or Cdbl).




Re: Visual Basic Express Edition Adding Number in VB

SP_021

If it's numbers you're adding, use 2 plusses(++) instead of one.




Re: Visual Basic Express Edition Adding Number in VB

spotty

Please dont, simply use the corect data types and the correct operator for VB. Turning Option Strict On will identify issues.

If your adding numbers a simple + will be perfectly sufficent, If you concatenating string then use the & operator





Re: Visual Basic Express Edition Adding Number in VB

Solitaire

All inputs and outputs in the computer MUST be strings. If a number value is needed for math processing, the string representation must be converted into a number type. After processing, the result must then be converted back into a string for display.

All textboxes accept only a string/text input. You can use the TryParse method to convert a numeric string into a number type. For example:

Integer.TryParse(TextBox1.Text, inum)
or
Double.TryParse(TextBox2.Text, dnum)

Then do your math processing with the number values (which should all be of the same type) and convert the answer to a string for display in another textbox. For example, assuming answer, num1, and num2 are all integers (or all doubles):

answer = num1 + num2
TextBox3.Text = answer.ToString()

Note, if the text input was non-numeric or blank, the TryParse method will return 0 without generating a run-time error.







Re: Visual Basic Express Edition Adding Number in VB

SJWhiteley

I really wish they had done away with using + to concatenate strings....






Re: Visual Basic Express Edition Adding Number in VB

spotty

Legacy issues

Unfortunately this seems to cause problems to beginners frequently who dont seem to consider data type.





Re: Visual Basic Express Edition Adding Number in VB

js06

I know there are quite a few posts here already but i thought i would put my 2 cents in. It may be helpful.

If you have the variables declared already then just use the variable names

txttotalq.text = variable1 + variable2 +variable3 +variable4 + variable5 + variable6

I just noticed that you specifically said the values are already declared so why not use them.

if you want to use this many times you could combine all the variables into a single variable

dim totalvariables as integer = variable1 + variable2 +variable3 +variable4 + variable5 + variable6

Then call it when you want it

txttotalq.text = totalvariables

Anyway, take it for what it's worth, if anything it may save you a lot of code clutter

I hope it helps in some way






Re: Visual Basic Express Edition Adding Number in VB

spotty

The answer by Js06 is unfortunately incorrect, as an answer the result does not depend upon whether a variable is declared already or not but is absolutely related to the type that the value (whether declared or not) is and the operator being used.

Here's some examples to demonstrate the problem.

With Option Strict Off (which is default)

Code Snippet

Dim x1 As String = "10"

Dim y1 As String = "10"

Dim z As Integer = x1 + y1

Results in Z being 1010

Code Snippet

Dim x1 As Integer = 10

Dim y1 As String = "10"

Dim z As Integer = x1 + y1

Results in Z being 20

Why, because of the types used with the + operator and an implied conversion that takes place.

In order to eliminate the problem,

1. Use Option Strict On

2. Ensure you use the correct data types

3. Ensure you use the correct operators with those types.

So in the above example if I wanted to do numeric addition

Code Snippet

Dim x1 As Integer = 10

Dim y1 As Integer = 10

Dim z As Integer = x1 + y1

Will Result in 20

If I wanted to do string concatenation

Code Snippet

Dim x1 As Integer = 10

Dim y1 As Integer = 10

Dim z As String = Ctype(x1,string) & ctype(y1, string)

Will Result in "1010"

You can try the above examples using literals, constants, enums or variables and the same behaviour is evident such as

Code Snippet

Dim z As Integer = "1" + "10"

Will Result in "110"

In the original code sample provided, they are using the + operator with the Textbox text property which returns a string value. This in conjunction with the + operator will result in string concatenation and not numeric addition as is desired. Casting the text properties to a numeric type would result in numeric addition occuring and the numeric result would need to be cast back to a string to rest the result textbox text property.

Using Option Strict will highlight this issue.





Re: Visual Basic Express Edition Adding Number in VB

js06

I can't quite see how my answer was any different than the example you gave.

Dim x1 As Integer = 10

Dim y1 As Integer = 10

Dim z As Integer = x1 + y1

Will Result in 20

This would be the same since the values in each of the textboxes were already declared as integers. You wouldn't be using a string (text) you would be using an integer.

Your example states declared integer1 + declared integer2 = the sum of the 2 declared integers.

Isn't that what my example shows

You don't have to use the text from the boxes, you directly use the variables.

Sorry you got me going on this one when you said i was wrong. look at my example and i don't think you can say that it is any different than what you posted using integers. It bothers me when I give an example, then I'm told my example is wrong and then the same basic example is given. I can't quite figure this one out. The only reason i gave this example is because i have used it and it worked. And apparently you agree. You typed out the code that is already in decimator's app, he/she was trying to use "text" instead of the values. No disrespect to anyone, i took this one a little personal.

If anyone else can see a difference between this let me know. I think I need to take a break from posting to questions for a while.






Re: Visual Basic Express Edition Adding Number in VB

spotty

How is the answer different.

Because in the above example I have explicitly stated the type to be a numeric type. Therefore the + operator would cause a numeric operation. However the text property of the textbox returns a string an integer. It doesnt matter if textbox contains a integer value as when you use the text property you are using a string. So if you do

Code Snippet

DIm x as integer = Textbox1.Text + Textbox2.Text

Declaration is NOT the key concept here, it is the data type and operator. If you have specifically declared a type as a numeric type then the operator would result in numeric operator. The concept is specific to data type / operator . In the original posting the person was talking about using the Textbox Text property.

In your example


txttotalq.text = variable1 + variable2 +variable3 +variable4 + variable5 + variable6

If I was to apply this to the original posting then the types are

String = String + String + String

The values populated into the textbox may be integers but they implicitly use a tostring method to populate the text property and this property has a string value.

So your response was incorrect as it makes no reference to the specific types of the variables. If they are all string then the + operator will result in concatenation. If they are numeric types they will result in numeric addition - this was the original problem.

The difference in my example is that

declared integer1 + declared integer2 = the sum of the 2 declared integers.

which is specifically stating the types. This IS the key concept.

This was why the original examples use Val, Cint, Ctype, TryParse in order to cast the string type to a numeric type so the + operator carries out numeric addition.

So you answer is in fact wrong as its not addressing the key issue which is the data types. It does not matter if you are using a declared variable or not, its the type that matters. You can try the concept with Literals, Enums, Constants or variables and the same concept will still apply - it doesnt matter it is a declared variable.

"DATA TYPE AND OPERATOR USAGE IS KEY TO UNDERSTANDING THIS BEHAVIOUR, NOT WHETHER ITS DECLARED VARIABLE OR NOT"





Re: Visual Basic Express Edition Adding Number in VB

spotty

Use the following example

Code Snippet

Option Strict On

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim i As Integer = 1
Dim j As Integer = 2

Me.TextBox1.Text = i + j

End Sub
End Class

Your answer depends upon implied conversion which occurs when you dont have option strict on. Option Strict On is highlighting you data type issues showing you that your code may work because of implied conversion if you have option strict off.

Also if you are using textboxes then its likely the user is going to change some values, otherwise you may use a label. The fact that a textbox textbox returns a string, and sure I can put some code in to validate the text can be converted to a numeric value. Using the text property always returns a string, irrespective of if you populate it from integer, single, double.

This occurs with option strict off as it will use implicitly call the tostring method.





Re: Visual Basic Express Edition Adding Number in VB

ReneeC

Actually I like the "+" concatentation operator because it's not as forgiving as the "&" and you have to use correct datatypes. It's a little like option Strict.