3.2.1, 3.2.7 - Data types, input and output (VB.NET)
Choose suitable types for data, read keyboard input and display useful results in VB.NET. Native VB.NET answers are written here; execution is unavailable. The optional Python demonstration has its own Run control.
Choosing a data type
A value's data type determines what sort of information it represents and which operations make sense.
Data type
A classification of data, such as integer, real, Boolean, character or string, that determines its possible values and permitted operations.
An integer represents a whole-number count, such as 24 tickets. A real represents a number that may have a fractional part, such as a temperature of 18.5. A Boolean holds True or False, useful for whether a door is open. A character holds one character; a string holds a sequence of characters, including the empty string.
| General exam name | VB.NET type | Suitable declaration |
|---|---|---|
| Integer | Integer | Dim tickets As Integer = 24 |
| Real | Double | Dim temperature As Double = 18.5 |
| Boolean | Boolean | Dim doorOpen As Boolean = False |
| Character | Char | Dim grade As Char = "B"c |
| String | String | Dim postcode As String = "AB1 2CD" |
Dim declares a variable, and As specifies its type. The c suffix makes a one-character literal a Char. Without it, "B" is a string. The specification uses the general name real, although VB.NET offers names such as Double and Single.
A telephone number should usually be text: its leading zero matters and adding two telephone numbers is meaningless. A quantity used in a calculation needs a numeric type. The characters "24" and the integer 24 may look alike when displayed but represent different types. Choose by the meaning and required operations, not appearance alone.
Reading keyboard input
Keyboard input enters a console program as text, so numeric input needs a conversion before arithmetic.
Console.Write displays a prompt without moving to a new line. Console.ReadLine() waits for an entered line and returns a string. In this example assume the user enters a whole number; data-type checks are not required for these exam inputs.
Console.Write("Tickets: ")
Dim tickets As Integer = CInt(Console.ReadLine())
CInt converts the entered text to an integer. CDbl can convert suitable numeric text to a Double. Reading a name requires no numeric conversion: Dim name As String = Console.ReadLine() preserves the text. Assignment stores the returned value on the left; Console.ReadLine() = tickets reverses the flow and is incorrect.
This complete console program shows where executable statements belong. Sub Main is the starting routine, inside a Module; later lessons show fragments to place inside Main unless stated otherwise.
Module Program
Sub Main()
Console.Write("Name: ")
Dim name As String = Console.ReadLine()
Console.WriteLine("Welcome, " & name)
End Sub
End Module
Entering Ravi produces Welcome, Ravi. The prompt, input, then output occur in that order.
Displaying results
Output turns stored data or calculated results into information the user can understand.
Console.WriteLine displays a value and then ends the line. The string operator & joins text. In Console.WriteLine("Tickets: " & tickets), VB.NET converts the number for display; a label explains what the number means. Two calls to WriteLine produce two lines. Write is useful when another value should appear on the same line.
Optional Python demonstration — input and output. Predict the display after entering 3, then run. Modify the label Tickets: to Seats: and run with the same input. The input conversion remains needed; changing a label does not change the stored number. This is Python, not the required VB.NET answer.
Optional Python demonstration: labelled output
{"exercise_id":"aqa8525-vbnet-types-io-demo","task_version":"1","runtime_profile":"python-console-v1","original_filename":"types_io_demo.py","file_mode":"fresh","code":"tickets = int(input("Tickets: "))\nprint("Tickets:", tickets)\n","instructions":"Predict and run with input 3. Replace the output label Tickets: with Seats: (leave the input prompt unchanged), then run again with 3."}