VB Tutor VB2022 VB2019 VB6 VB Sample Code About Us
Maximum Number Calculator

Maximum Number Calculator

Find the largest number among three values with VB6 and VB.NET implementations


This program allows the user to enter three numbers and calculates the maximum number among them. The solution demonstrates how to implement this functionality in both Visual Basic 6 and Visual Basic .NET.

Key programming concepts demonstrated:

  • Function creation with parameters
  • Conditional logic using If-Then-Else statements
  • Comparison operators
  • Data type conversion
  • Event handling for button clicks

Interactive Calculator

Enter three numbers to find the maximum value:

Result will appear here
15
27
8

Code Implementation

Below are implementations in both Visual Basic 6 and Visual Basic .NET:

Visual Basic 6 Implementation

' Function to calculate maximum of three numbers
Function calMax(x As Variant, y As Variant, z As Variant) As String
    If x > y And x > z Then
        calMax = Str(x)
    ElseIf y > x And y > z Then
        calMax = Str(y)
    ElseIf z > x And z > y Then
        calMax = Str(z)
    Else
        ' If all numbers are equal
        calMax = Str(x)
    End If
End Function

' Button click event handler
Private Sub Command1_Click()
    Dim a, b, c As Double
    
    ' Get values from text boxes
    a = Val(Txt_Num1.Text)
    b = Val(Txt_Num2.Text)
    c = Val(Txt_Num3.Text)
    
    ' Display result in label
    Lbl_Display.Caption = calMax(a, b, c)
End Sub

Visual Basic .NET Implementation

' Function to calculate maximum of three numbers
Private Function CalculateMax(ByVal x As Double, 
                          ByVal y As Double, 
                          ByVal z As Double) As String
    
    If x > y AndAlso x > z Then
        Return x.ToString()
    ElseIf y > x AndAlso y > z Then
        Return y.ToString()
    ElseIf z > x AndAlso z > y Then
        Return z.ToString()
    Else
        ' If all numbers are equal
        Return x.ToString()
    End If
End Function

' Button click event handler
Private Sub btnCalculate_Click(sender As Object, 
                             e As EventArgs) Handles btnCalculate.Click
    Dim num1, num2, num3 As Double
    
    ' Parse input values with validation
    If Not Double.TryParse(txtNum1.Text, num1) Then
        MessageBox.Show("Please enter a valid number in field 1")
        Exit Sub
    End If
    
    If Not Double.TryParse(txtNum2.Text, num2) Then
        MessageBox.Show("Please enter a valid number in field 2")
        Exit Sub
    End If
    
    If Not Double.TryParse(txtNum3.Text, num3) Then
        MessageBox.Show("Please enter a valid number in field 3")
        Exit Sub
    End If
    
    ' Calculate and display result
    lblResult.Text = "Maximum number: " & CalculateMax(num1, num2, num3)
End Sub

Note: The VB.NET version includes input validation and uses type-safe methods, demonstrating modern best practices.

Key Differences Between VB6 and VB.NET

Syntax

VB.NET uses AndAlso/OrElse for short-circuit evaluation instead of And/Or

Type Safety

VB.NET requires explicit data types and uses TryParse for safe conversion

Event Handling

VB.NET uses Handles clause instead of object-specific event procedures

Error Handling

VB.NET encourages structured exception handling with Try/Catch blocks