Kacper
Kacper

Reputation: 5

How to declare global variables and use them in functions [Visual Basic]

I am trying to create a program (in Visual Basic using Microsoft Visual Studio) where I have a terminal menu with different options for the user to select. Depending on what the user selects, the menu takes them to a particular function to carry out a specific task.

Now one of the options I want to make is to have the user select the accuracy levels they want results to be displayed. For example, they can select 1 decimal place, 2 decimal places, etc. After they have selected their option, I want the program to save that option and then use it when they select another function that involves some maths.

To help you understand, here are some photos:

Menu

Accuracy selection

Quadratic function option

So I want to be able to have the accuracy option saved to a global variable (this will be a simple format code) and then be able to apply that to the result of any maths function in my program.

My question is, how I do create one and implement it?

I tried creating a public class but I keep getting error messages.

Public class

Trying to implement it to the accuracy option

My code:

Imports System.Math 'imports maths fuctions so that I can use them throughout this program.
Module Module1
    Public Class globalvariables

        Dim formoption

    End Class

    Sub Main()
        Console.Title = "CS0002 CourseWork -"

        Dim linebreak As String = "*************************************************" 'this is to seperate text and make the program visually cleaner.
        Dim useroption As Integer 'declaring the variable as a integer datatype


        Console.WriteLine("Please select any of the options below:")

        Console.WriteLine(linebreak)

        'A list of different options appear for the user to select from.
        Console.WriteLine("1 - Accuracy Option")

        Console.WriteLine("2 - Quadratic Equation")

        Console.WriteLine("3 - Protein Sequence Segmentation")

        Console.WriteLine("4 - Monte-carlo Integration of a Function")

        Console.WriteLine("5 - Exit Program")

        Console.WriteLine(linebreak)

        useroption = Val(Console.ReadLine()) 'users input will be taken and stored into the variable useroption.


        'depending on what the user entered, the if-statement below will dertermine what will be the next step. 
        If useroption = 1 Then

            Console.WriteLine("You have selected: Accuracy Option")
            'then call up function that deals with accuracy

            Console.WriteLine(accuracy()) 'calling a function that will bring up more information and data.



        ElseIf useroption = 2 Then

            Console.WriteLine("You have selected: Quadratic Equation")
            Console.WriteLine("Loading...")
            'calls up the quadratic equation function
            Console.WriteLine(quadraticequation())



        ElseIf useroption = 3 Then

            Console.WriteLine("You have selected: Protein Sequence Segmentation")
            'calls up protein function


        ElseIf useroption = 4 Then

            Console.WriteLine("You have selected: Monte-carlo Integration of a Function")
            'calls up monte-carlo function


        ElseIf useroption = 5 Then

            Console.WriteLine("Goodbye...")
            'Message dissplayed to the user saying goodbye and once they click any button, the terminal will close.


        Else

            Console.WriteLine("Invalid option, please try again.")
            Console.WriteLine(" ")
            Main() 'runs from the top again as the user has inputted an invalid option

        End If


    End Sub

    Public Function accuracy()

        Dim linebreak As String = "*************************************************"
        Console.WriteLine(linebreak)
        Console.WriteLine("Would you like to continue?")
        Console.WriteLine(" ")

        Console.WriteLine("1 - Yes, continue")
        Console.WriteLine("2 - No, return to main menu")

        Dim accuracyoption As Integer


        accuracyoption = Val(Console.ReadLine())

        If accuracyoption = 1 Then
            Console.WriteLine("Loading...")
            Console.WriteLine(" ")

            Console.WriteLine("Please select how you would like to have your results displayed:")

            Console.WriteLine("From 1 to 5 decimal places:")

            Dim accuracyinput As Integer

            accuracyinput = Val(Console.ReadLine())

            If accuracyinput = 1 Then
                Console.WriteLine("Thank you. Your results will now be displayed in 1 decimal place.")
                formoption = Format("'##.0") 'work In progress, need To make this Global variable To use In other functions?
                Console.WriteLine(" ")
                Main()

            ElseIf accuracyinput = 2 Then
                Console.WriteLine("Thank you. Your results will now be displayed in 2 decimal places.")
                Console.WriteLine(" ")
                Main()

            ElseIf accuracyinput = 3 Then
                Console.WriteLine("Thank you. Your results will now be displayed in 3 decimal places.")
                Console.WriteLine(" ")
                Main()

            ElseIf accuracyinput = 4 Then
                Console.WriteLine("Thank you. Your results will now be displayed in 4 decimal places.")
                Console.WriteLine(" ")
                Main()

            ElseIf accuracyinput = 5 Then
                Console.WriteLine("Thank you. Your results will now be displayed in 5 decimal places.")
                Console.WriteLine(" ")
                Main()

            Else
                Console.WriteLine("Please select a valid option")
                accuracy()

            End If




        ElseIf accuracyoption = 2 Then
            Main() 'takes user back to the top aka main menu.

        Else
            Console.WriteLine("Invalid choice, please try again")
            accuracy()
        End If

    End Function

Upvotes: 0

Views: 2095

Answers (1)

jmcilhinney
jmcilhinney

Reputation: 54417

In VB, a global variable generally means a Public field in a module. You could also use a Public Shared field in a class but a module is better in this case because it cannot be instantiated.

Friend Module GlobalVariables

    Public decimalPlaces As Integer

End Module

You can then get and set it anywhere in your code, e.g.

decimalPlaces = CInt(decimalPlacesTextBox.Text)

and:

number = Math.Round(number, decimalPlaces)

Upvotes: 1

Related Questions