Console App Part 1 in Visual Basic 2022
Lesson 37 covers console app part 1 using the same proven VBTutor lesson layout, now adapted for Visual Basic 2022 and Visual Studio 2022.
Lesson Overview
Key Takeaway
This lesson focuses on console app part 1 in Visual Basic 2022.
Lesson 37: Console App Part 1
In Visual Basic 2022, console applications run in a command-line interface without a graphical user interface. They are perfect for:
Learning Fundamentals
Master programming concepts without UI distractions
Command-line Tools
Create utilities for automation and scripting
Server Applications
Build background services and daemons
Rapid Prototyping
Quickly test ideas and algorithms
37.1 Creating a Console Project
To create a new console application in Visual Studio 2022:
- Launch Visual Studio 2022
- Select "Create a new project"
- Search for "Console App" and select "Console App (.NET Framework)"
- Name your project and choose a location
- Click "Create"
Figure 37.1: Creating a new console application project
37.2 Console I/O Fundamentals
Console applications primarily use these methods for input and output:
| Method | Description | Example |
|---|---|---|
| Console.Write() | Outputs text without a newline | Console.Write("Enter name: ") |
| Console.WriteLine() | Outputs text with a newline | Console.WriteLine("Hello World") |
| Console.ReadLine() | Reads a line of text input | Dim name = Console.ReadLine() |
| Console.ReadKey() | Reads a single key press | Console.ReadKey(True) |
Module Module1 Sub Main() ' Display a welcome message Console.WriteLine("=== Welcome to VB2022 Console ===") Console.WriteLine() ' Get user input Console.Write("Please enter your name: ") Dim userName As String = Console.ReadLine() ' Display personalized message Console.WriteLine($"Hello, {userName}! Welcome to console programming.") Console.WriteLine() ' Wait for key press before exiting Console.WriteLine("Press any key to exit...") Console.ReadKey(True) End Sub End Module
37.3 Simple Calculator
This example demonstrates a basic calculator with arithmetic operations:
Module Calculator Sub Main() Console.WriteLine("===== SIMPLE CALCULATOR =====") Console.WriteLine() ' Get first number Console.Write("Enter first number: ") Dim num1 As Double = Console.ReadLine() ' Get second number Console.Write("Enter second number: ") Dim num2 As Double = Console.ReadLine() ' Perform calculations Console.WriteLine() Console.WriteLine("RESULTS:") Console.WriteLine($"{num1} + {num2} = {num1 + num2}") Console.WriteLine($"{num1} - {num2} = {num1 - num2}") Console.WriteLine($"{num1} * {num2} = {num1 * num2}") ' Check for division by zero If num2 <> 0 Then Console.WriteLine($"{num1} / {num2} = {num1 / num2}") Else Console.WriteLine("Cannot divide by zero!") End If Console.WriteLine() Console.WriteLine("Press Enter to exit...") Console.ReadLine() End Sub End Module
37.4 Number Guessing Game
A classic number guessing game that demonstrates loops and conditional logic:
Module GuessingGame Sub Main() ' Initialize random number generator Dim rand As New Random() Dim secretNumber As Integer = rand.Next(1, 101) Dim guess As Integer Dim attempts As Integer = 0 Console.WriteLine("=== NUMBER GUESSING GAME ===") Console.WriteLine("I'm thinking of a number between 1 and 100.") Console.WriteLine() ' Game loop Do Console.Write("Enter your guess: ") ' Validate input If Not Integer.TryParse(Console.ReadLine(), guess) Then Console.WriteLine("Please enter a valid number!") Continue Do End If attempts += 1 ' Check guess If guess < secretNumber Then Console.WriteLine("Too low! Try again.") ElseIf guess > secretNumber Then Console.WriteLine("Too high! Try again.") Else Console.WriteLine() Console.WriteLine($"Correct! You guessed it in {attempts} attempts.") End If Loop Until guess = secretNumber Console.WriteLine("Thanks for playing!") Console.ReadLine() End Sub End Module
37.5 File Processing
This example demonstrates reading and writing files in a console application:
Imports System.IO Module FileProcessor Sub Main() Console.WriteLine("=== FILE PROCESSING DEMO ===") Console.WriteLine() ' Create a new file Dim filePath As String = "sample.txt" Console.WriteLine("Writing to file...") File.WriteAllText(filePath, "Hello, VB2022 Console!" & Environment.NewLine) File.AppendAllText(filePath, "This is a second line." & Environment.NewLine) Console.WriteLine("File created successfully.") Console.WriteLine() ' Read and display file content Console.WriteLine("Reading file content:") Console.WriteLine("----------------------") Dim fileContent As String = File.ReadAllText(filePath) Console.WriteLine(fileContent) Console.WriteLine() Console.WriteLine("Press Enter to exit...") Console.ReadLine() End Sub End Module
Console Application Summary
Key concepts for console applications in VB2022:
| Concept | Description | Key Methods |
|---|---|---|
| Input/Output | Communicating with the user | Write, WriteLine, ReadLine, ReadKey |
| Control Flow | Managing program execution | If, Select Case, For, Do While |
| Data Types | Storing and manipulating data | Integer, String, Double, Boolean |
| File I/O | Reading and writing files | File.ReadAllText, File.WriteAllText |
Entry Point
All console applications start execution in the Sub Main() method
Simplicity
Console apps are lightweight and ideal for learning core programming concepts
Practicality
Great for creating command-line tools, scripts, and utilities
Practical Exercises
Apply your console application knowledge with these hands-on exercises:
Exercise 1: Temperature Converter
Create a program that converts between Celsius and Fahrenheit. The user should choose the conversion direction.
Exercise 2: Multiplication Table Generator
Generate a multiplication table for a given number (from 1 to 10) and display it in a formatted way.
Exercise 3: Simple ATM Simulator
Create an ATM simulation with options to check balance, deposit, and withdraw funds.
Exercise 4: Text File Analyzer
Create a program that reads a text file and reports the number of lines, words, and characters.
Exercise 5: Password Generator
Generate random passwords based on user-specified length and complexity requirements.
Challenge Exercise: Contact Manager
Develop a console-based contact manager that can add, list, search, and save contacts to a file.
Next Lesson
Learn advanced console techniques in Lesson 38: Console Applications Part 2.
Related Resources
Visual Basic 2022 Made Easy
The ultimate beginner-friendly guide for mastering Windows-based application development using Visual Basic in Visual Studio 2022. Whether you're a student, teacher, hobbyist, or self-learner, this book offers a clear, step-by-step approach to help you get started with ease.
What You'll Learn:
- Control structures and procedures
- Decision-making techniques
- Efficient code organization
- Practical application development
- Best practices in VB2022
Mastering Excel VBA 365
Your ultimate step-by-step guide to automating tasks, building macros, and creating powerful applications within Microsoft Excel 365. Whether you're a student, business professional, or aspiring programmer, this comprehensive handbook will help you unlock the full potential of Excel's VBA.
What You'll Learn:
- Control structures in VBA
- Decision-making techniques
- Data processing and analysis
- Report generation
- Automated workflows
Lesson Recap
Lesson Summary
You have completed Lesson 37: Console App Part 1.
Core concept
You studied console app part 1 in VB2022.
Practical focus
The lesson follows the same classic VBTutor learning path and structure.
Modern tools
Examples are positioned around Visual Studio 2022 and VB.NET workflows.
Next step
Continue to Lesson 38 to keep progressing through the course.
Continue with the next lesson to build your VB2022 skills step by step.
Next Lesson
Ready to continue?
Lesson 38: Console App Part 2Related Resources