By [Your Name/Institution]
Visual Basic .NET (VB.NET) remains a staple in many BCA curricula due to its rapid application development (RAD) capabilities and gentle learning curve. However, "It compiles on my machine" rarely survives the lab evaluator’s test data.
If you’re staring at a red squiggly line or a runtime crash, this guide helps you identify, fix, and perfect the five most common types of VB.NET lab programs.
Aim: Check if a given number is prime.
Module Module1 Sub Main() Dim num, i As Integer Dim isPrime As Boolean = TrueConsole.Write("Enter a number: ") num = Console.ReadLine() If num <= 1 Then isPrime = False Else For i = 2 To Math.Sqrt(num) ' Optimization: Check only up to square root If num Mod i = 0 Then isPrime = False Exit For End If Next End If If isPrime Then Console.WriteLine(num & " is a prime number.") Else Console.WriteLine(num & " is not a prime number.") End If Console.ReadLine() End Sub
End Module
🔧 Common Fix:
Objective: To check if a string reads the same forwards and backwards (e.g., "madam").
Code:
Public Class Form1
Private Sub btnCheck_Click(sender As Object, e As EventArgs) Handles btnCheck.Click
Dim inputStr As String = txtInput.Text
Dim reverseStr As String = StrReverse(inputStr)
If String.Compare(inputStr, reverseStr, False) = 0 Then
MessageBox.Show("The string is a Palindrome.")
Else
MessageBox.Show("The string is NOT a Palindrome.")
End If
End Sub
End Class
Note: StrReverse is a built-in VB.NET function. In other .NET languages, you might use Array.Reverse.
Aim: Find factorial using a recursive function. vb net lab programs for bca students fix
Module Module1 Function Factorial(ByVal n As Integer) As Long If n = 0 Or n = 1 Then Return 1 Else Return n * Factorial(n - 1) End If End FunctionSub Main() Dim num As Integer Console.Write("Enter a number: ") num = Console.ReadLine() Console.WriteLine("Factorial is: " & Factorial(num)) Console.ReadLine() End Sub
End Module
🔧 Critical Fix for BCA Exams:
Symptom: result = 7/2 gives 3.5, but result = 7\2 gives 3.
Fix: In VB.NET, / returns a Double. \ returns an Integer after rounding. For precise division in BCA lab reports, use CDec() or convert to double first.
Problem: Store 10 numbers in an array, display max, min, average. By [Your Name/Institution] Visual Basic
Common errors to fix:
Fixed code:
Dim numbers(9) As Integer ' 10 elements (0-9) ' Input loop For i As Integer = 0 To numbers.Length - 1 ' Fix: Length-1 numbers(i) = CInt(InputBox("Enter number " & (i+1))) NextDim maxVal As Integer = numbers(0) Dim minVal As Integer = numbers(0) Dim sum As Integer = 0
For i As Integer = 0 To numbers.Length - 1 If numbers(i) > maxVal Then maxVal = numbers(i) If numbers(i) < minVal Then minVal = numbers(i) sum += numbers(i) Next
Dim avg As Double = sum / numbers.LengthAim: Check if a given number is prime
| Symptom | Most Likely Fix |
|---------|----------------|
| "Object reference not set to an instance of an object" | You used a control or array before New or assignment. |
| "Conversion from string to type 'Double' is not valid" | Textbox contains letters or is empty. Use TryParse. |
| Form runs but nothing happens on button click | Missing Handles Button1.Click. |
| Infinite loop | Loop condition never becomes false. Add Step or i += 1. |
| Output is always zero or default | Variable scope issue – declared inside a block but used outside. |