Ad space (banner)
๐Ÿ”ทVB.NET Lessons
Lesson 31 / 68

The basics of type conversion (casting)

This lesson covers type conversion in VB.NET, with the aim of converting between strings and numbers.

CInt() converts a string to an integer and CStr() a number to a string. Combining data of different types calls for explicit conversion.

The sample code converts the string "123" to the integer 123 with CInt("123") and uses it in a calculation, then converts the number 45 to a string with CStr(45) to join it to other text. Note how each conversion suits its purpose.

A common early stumble is passing CInt() a string that is not a number, which is a run-time error. Something like "abc" fails, so a check beforehand is sometimes needed.

In professional work, type conversion is a daily occurrence — using text typed into a form as a number, or turning a calculated result into text for display.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run VB.NET directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)