multiplying two text boxes

crazymarvin

Member
Joined
Nov 22, 2006
Messages
18
Location
Belfast, UK
Programming Experience
Beginner
I need to multiply two integers in two different text boxes, i was useing the following code:
VB.NET:
Me.txtboxCost = (frmOrder.txtboxSize.Text * txtboxPsqm)
and vs 2005 says:
Error 1 Operator '*' is not defined for types 'String' and 'System.Windows.Forms.TextBox'.
I'm sure this is something simple lol, i know the method I'm useing works in vs 2003, is this just a difference between the two versions of the langauge or am i missing something?

thanks, martyn
 
If i were to write..

10 * 10

Would you say the output would be 100. Well a peoples world where we can tell that the 10 is ment to be a number you probably would. However a textbox.text is a string not a number value. You can't multiply a string, you have to multiply a number type i.e.. Integer etc.. So you need to convert the values in the textbox to integers.

VB.NET:
Dim Number1 as Integer = Convert.Toint32(txtboxsize.text)

With me?
 
Try this

VB.NET:
Me.txtboxCost = val(frmOrder.txtboxSize.Text) * val(txtboxPsqm)

actually it's recamended that we stay away from the val() function, i would personally use a decimal or double variable type for this:

VB.NET:
Me.txtboxCost.Text = (CDec(frmOrder.txtboxSize.Text) * CDec(Me.txtboxPsqm.Text)).ToString()
 
actually it's recamended that we stay away from the val() function, i would personally use a decimal or double variable type for this:


Why is it that it is recommended to not use val()? I am asking becaue I have an application that does a bunch of calculations and I have either used val() or CType.
 
the Val() function is legacy code located in the VisualBasic namespace (which there is rumor that future versions of VB will not support it) therefor it's recommended that we don't use it when building applications. Also avoiding the Val() function helps better utilize computer resources by just converting the value to a specific variable (eg. use CDec() to convert to a decimal, CInt() for integer, Cstr() and .ToString() for strings, CDbl() for double etc) or CType() specifying the variable type there too
 
Back
Top