如何声明全局变量并在函数中使用它们 [Visual Basic]

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

我正在尝试创建一个程序(在 Visual Basic 中使用 Microsoft Visual Studio),其中我有一个终端菜单,其中包含供用户 select 使用的不同选项。根据用户 select 的内容,菜单会将他们带到特定功能以执行特定任务。

现在我想做出的选择之一是让用户select达到他们希望显示结果的准确度级别。例如,他们可以 select 1 位小数、2 位小数等。在他们 select 编辑了他们的选项后,我希望程序保存该选项,然后在他们 select 另一个涉及一些数学的函数。

为了帮助您理解,这里有一些照片:

Menu

Accuracy selection

Quadratic function option

所以我希望能够将精度选项保存到全局变量(这将是一个简单的格式代码),然后能够将其应用于我程序中任何数学函数的结果。

我的问题是,如何创建并实施它?

我尝试创建 public class,但我不断收到错误消息。

Public class

Trying to implement it to the accuracy option

我的代码:

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

在VB中,全局变量一般表示模块中的Public字段。您也可以在 class 中使用 Public Shared 字段,但在这种情况下使用模块更好,因为它无法实例化。

Friend Module GlobalVariables

    Public decimalPlaces As Integer

End Module

然后您可以在代码中的任何位置获取和设置它,例如

decimalPlaces = CInt(decimalPlacesTextBox.Text)

和:

number = Math.Round(number, decimalPlaces)