Step 1 of 5 · Reading · ~2 min
Learn
Basics
Declare with Dim:
Dim age As Integer = 36
Dim name As String = "Ada"
Dim pi As Double = 3.14159
Dim isActive As Boolean = True
Write the As Type every time. This course's compiler has type inference off, so a bare Dim total = 0 is an error, not a shortcut — and with Option Strict On, which is the setting you want in real projects, the annotation is required anyway.
Common types:
Integer(32-bit),Long(64-bit),Short(16-bit),ByteDouble,Single,DecimalBoolean(True/False)StringChar(a single character — a distinct type fromString; the literal is"h"c)
Joining strings
Dim name As String = "Ada"
Dim age As Integer = 36
Dim upper As String = name.ToUpper() ' "ADA"
Dim n As Integer = name.Length ' 3
Dim greeting As String = "Hello, " & name & "!" ' "Hello, Ada!"
Dim msg As String = String.Format("{0} is {1}", name, age)
' "Ada is 36"
& is the canonical concatenation operator. + also joins two strings, but it stays the addition operator too, so "5" + 3 is a conversion question rather than a concatenation — & never is. Newer VB (2015 and later) adds interpolated strings, $"{name} is {age}"; the compiler behind this course predates them and rejects $"..." outright, so use & or String.Format here.
Reading input
Dim n As Integer = Integer.Parse(Console.ReadLine())
Console.ReadLine always hands you a String, even when the user typed digits. Integer.Parse turns "42" into 42; it throws if the text is not a number, which is exactly the loud failure you want while learning.
Your exercise
Two numbers arrive on two separate lines, so you need Console.ReadLine() twice — the starter does that for you. Print the sum with Console.WriteLine(a + b). Note that a + b adds because both are Integer; had you skipped the Integer.Parse and kept them as strings, + would have concatenated 3 and 4 into 34.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…