Programming Fundamentals/Conditions/VB.NET

From Wikiversity
Jump to navigation Jump to search

conditions.vb[edit]

' This program asks the user to select Fahrenheit or Celsius conversion
' and input a given temperature. Then the program converts the given 
' temperature and displays the result.
'
' References:
'     https://www.mathsisfun.com/temperature-conversion.html
'     https://en.wikibooks.org/wiki/Visual_Basic_.NET

Imports System

Public Module MyProgram
    Sub Main
        ' Main could either be an If-Else structure or a Select-Case structure
        IfElse()
        SelectCase()
    End Sub

    Private Sub IfElse()
        Dim Choice As String
        
        Console.WriteLine("Enter F to convert to Fahrenheit or C to convert to Celsius:")
        Choice = Console.ReadLine()
        If Choice = "C" Or Choice = "c" Then
            ProcessCelsius()
        ElseIf Choice = "F" Or Choice = "f" Then
            ProcessFahrenheit()
        Else
            Console.WriteLine("You must enter C to convert to Celsius or F to convert to Fahrenheit!")
        End If
    End Sub
    
    Private Sub SelectCase()
        Dim Choice As String
        
        Console.WriteLine("Enter F to convert to Fahrenheit or C to convert to Celsius:")
        Choice = Console.ReadLine()
        Select Choice
            Case "C", "c"
                ProcessCelsius()
            Case "F", "f"
                ProcessFahrenheit()
            Case Else
                Console.WriteLine("You must enter C to convert to Celsius or F to convert to Fahrenheit!")
        End Select
    End Sub

    Private Sub ProcessCelsius()
        Dim Temperature As Double
        Dim Result As Double
        
        Temperature = GetTemperature("Fahrenheit")
        Result = CalculateCelsius(Temperature)
        DisplayResult(Temperature, "Fahrenheit", Result, "Celsius")
    End Sub

    Private Sub ProcessFahrenheit()
        Dim Temperature As Double
        Dim Result As Double
        
        Temperature = GetTemperature("Celsius")
        Result = CalculateFahrenheit(Temperature)
        DisplayResult(Temperature, "Celsius", Result, "Fahrenheit")
    End Sub
    
    Function GetTemperature(Scale as String) As Double
        Dim Input As String
        Dim Temperature As Double
        
        Console.WriteLine("Enter " + Scale + " temperature:")
        Input = Console.ReadLine()
        Temperature = Convert.ToDouble(Input)
        Return Temperature
    End Function

    Function CalculateCelsius(Fahrenheit As Double) As Double
        Dim Celsius As Double
        
        Celsius = (Fahrenheit - 32) * 5 / 9
        Return Celsius
    End Function

    Function CalculateFahrenheit(Celsius As Double) As Double
        Dim Fahrenheit As Double
        
        Fahrenheit = Celsius * 9 / 5 + 32
        Return Fahrenheit
    End Function

    Sub DisplayResult(Temperature As Double, FromScale As String, Result As Double, ToScale As String)
        Console.WriteLine(Temperature.ToString() + "° " + FromScale + " is " + Result.ToString() + "° " + ToScale)
    End Sub
End Module

Try It[edit]

Copy and paste the code above into one of the following free online development environments or use your own VB.NET compiler / interpreter / IDE.

See Also[edit]