CDec, CInt, CStr and CDate in Visual
Basic
CInt: built-in function used to Convert an object
to an Integer (a whole number)
Dim Qty As Integer Dim Price, Total As Decimal Qty = CInt(txtQuantity.Text) Price = CDec(txtPrice.Text) ' or: lblPrice.Text = CDec(Val(txtPrice.Text))
Total = Price * Qty lblTotal.Text = FormatCurrency(Total, 2) ' The above is helpful when you cannot calculate on a text field that is formatted ' as currency due to the fact that the $ is not numeric, and a comma is not numeric. ' Although it is formatted as currency and cannot be used in a calculation, the original value is preserved and can be used later as a variable.
Other conversion Functions (there are many other conversion functions): Dim Name As String Name = CStr(txtName.Text) lblName.Text = CStr(txtName.Text)
Dim TodaysDate As Date TodaysDate = CDate(txtDate.Text)
Some reasons to Convert: Option Strict On “does not allow implicit conversions from String to Integer” If the expression passed to the function is outside the range of the data type to which it is to be converted, an OverflowException occurs. CDate recognizes date literals and time literals but not numeric values. |