Introduction to Vb.net
Introduction to Vb.net
NET
Programming Tools
To work with VB.NET, you need a few tools:
- Text Editor: You could write code in Notepad, but it’s not ideal.
- Compiler: Converts your VB.NET code into something the computer understands (machine
language). This is built into the VB.NET environment.
- IDE: The Integrated Development Environment (more on this below) is your all-in-one tool
for coding, testing, and debugging.
Practical Example
vb.net
Dim name As String = "Alice" ' Identifier: name, Literal: "Alice", Data type: String
Dim age As Integer = 20 ' Operator: = assigns value
Console.WriteLine(name & " is " & age) ' Expression with & (concatenation)
Procedures are reusable blocks of code—think of them as mini-recipes within your program.
Sub Procedures
A `Sub` does something but doesn’t return a value. It’s like a command.
```vb.net
Sub SayHello()
Console.WriteLine("Hello, world!")
End Sub
' Call it
SayHello() ' Outputs: Hello, world!
```
With parameters:
```vb.net
Sub Greet(ByVal name As String)
Console.WriteLine("Hi, " & name)
End Sub
Function Procedures
A `Function` does something and returns a value. It’s like a calculator.
```vb.net
Function Add(ByVal a As Integer, ByVal b As Integer) As Integer
Return a + b
End Function
' Call it
Dim result As Integer = Add(3, 4) ' result = 7
Console.WriteLine(result)
```
Modular Design
This is about breaking your program into smaller, manageable pieces (procedures). Why?
Easier to debug, reuse, and maintain. Example: Instead of one giant block of code to
calculate grades, split it into functions for input, calculation, and output.
Sequence
Code runs line by line, top to bottom. (Hierarchical)
```vb.net
Dim x As Integer = 5
Console.WriteLine(x) ' Outputs: 5
x=x+1
Console.WriteLine(x) ' Outputs: 6
Selection
Make choices with `If` or `Select Case`.
- If Statement:
vb.net
Dim score As Integer = 85
If score >= 60 Then
Console.WriteLine("Pass")
Else
Console.WriteLine("Fail")
End If
- Select Case:
vb.net
Dim day As Integer = 3
Select Case day
Case 1
Console.WriteLine("Monday")
Case 3
Console.WriteLine("Wednesday")
Case Else
Console.WriteLine("Other day")
End Select
Iteration
Loops repeat code.
- For Loop:
vb.net
For i As Integer = 1 To 5
Console.WriteLine("Count: " & i) ' Outputs 1 to 5
Next
- While Loop:
```vb.net
Dim count As Integer = 0
While count < 3
Console.WriteLine("Loop: " & count)
count += 1
End While
- Do Loop:
```vb.net
Dim num As Integer = 0
Do
Console.WriteLine("Number: " & num)
num += 1
Loop Until num = 4
Final example:
```vb.net
Module Module1
Sub Main()
Dim name As String = "Jane"
Dim age As Integer = 22