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

Using variables

This lesson covers the basics of variables in VB.NET, with the aim of understanding typed, safe declarations.

A variable is like a labelled box that stores a value under a name. You declare one with Dim name As Type = value, and join strings with &. Stating the type means a wrong kind of value is caught at compile time rather than at run time.

The sample code declares two variables, Dim name As String = "Alice" and Dim age As Integer = 14, and assembles a greeting by joining text and variables with the & operator. Note the explicit As String and As Integer.

A common early stumble is which operator joins strings. If you are used to + from JavaScript, VB.NET's & takes adjusting to. + works in places, but & is the default because it avoids unintended type conversion.

Naming a variable so that its purpose is obvious makes the code far easier to read later. The safety that comes with declaring types shows its value most on large programs.

๐Ÿ“– 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)