To convert a string to a number in Visual Basic, we use this:
It's as simple as that!
NOTE: This can be dangerous. Some strings may not convert properly, causing a runtime error.
If you find this risky, you can force safe conversions by adding "Option Explicit On to the top of your VB page.
To convert correctly, we can use CType. Some other examples of CType are:
If you are using .NET, you can use some of the built in functions, as well. Since .NET rightly views all variables as objects, any object (or variable) has its own premade functions. For example, you could convert a number to a string by using the ToString() function. Here is an example:
After this code has compiled, MyString would have the value "4"
Next we will continue to discuss the built in functions in VB.NET!
Previous Next