VB.NET Fundamentals

Welcome to the core concepts of Visual Basic .NET (VB.NET). This section covers the essential building blocks you need to understand to develop applications with VB.NET.

Variables and Data Types

Variables are used to store data. In VB.NET, you must declare a variable's data type before using it. This helps the compiler ensure type safety and optimize memory usage.

Common data types include:

Declaring and Assigning Variables

Dim userName As String = "Alice"
Dim userAge As Integer = 30
Dim isStudent As Boolean = True
Dim price As Double = 19.99

Operators

Operators are symbols that perform operations on operands (variables and values).

Arithmetic Operators

Comparison Operators

Logical Operators

Control Flow Statements

Control flow statements allow you to dictate the order in which your code is executed.

Conditional Statements

If...Then...Else

If score > 90 Then
    Console.WriteLine("Excellent!")
ElseIf score > 70 Then
    Console.WriteLine("Good job.")
Else
    Console.WriteLine("Needs improvement.")
End If

Select Case

Dim day As Integer = 3
Select Case day
    Case 1
        Console.WriteLine("Monday")
    Case 2
        Console.WriteLine("Tuesday")
    Case 3
        Console.WriteLine("Wednesday")
    Case Else
        Console.WriteLine("Another day")
End Select

Looping Statements

For...Next

For i As Integer = 1 To 5
    Console.WriteLine("Count: " & i)
Next

While...End While

Dim counter As Integer = 0
While counter < 3
    Console.WriteLine("Looping...")
    counter += 1
End While

Do...Loop

Dim num As Integer = 1
Do
    Console.WriteLine("Processing " & num)
    num += 1
Loop While num <= 4

Procedures (Subroutines and Functions)

Procedures are blocks of code that perform a specific task. Subroutines (Sub) perform an action, while Functions (Function) perform an action and return a value.

Sub Example

Sub GreetUser(name As String)
    Console.WriteLine("Hello, " & name & "!")
End Sub

' Calling the Sub
GreetUser("Bob")

Function Example

Function AddNumbers(num1 As Integer, num2 As Integer) As Integer
    Return num1 + num2
End Function

' Calling the Function
Dim sum As Integer = AddNumbers(10, 5)
Console.WriteLine("The sum is: " & sum)

Key Takeaway

Mastering these fundamental concepts is crucial for building robust and efficient VB.NET applications. Practice writing code snippets to solidify your understanding.