首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何声明全局变量并在函数中使用它们[Visual Basic]

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

Stack Overflow用户
提问于 2021-03-13 20:40:30
回答 1查看 126关注 0票数 0

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

现在,我想要做的一个选项是让用户选择他们想要显示的结果的精度级别。例如,他们可以选择1个小数位,2个小数位,等等。在他们选择了他们的选项后,我希望程序保存该选项,然后当他们选择另一个涉及一些数学的函数时使用它。

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

Menu

Accuracy selection

Quadratic function option

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

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

我尝试创建一个公共类,但总是收到错误消息。

Public class

Trying to implement it to the accuracy option

我的代码:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-13 22:21:00

在VB中,全局变量通常表示模块中的Public字段。您也可以在类中使用Public Shared字段,但在这种情况下使用模块更好,因为它不能被实例化。

代码语言:javascript
复制
Friend Module GlobalVariables

    Public decimalPlaces As Integer

End Module

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

代码语言:javascript
复制
decimalPlaces = CInt(decimalPlacesTextBox.Text)

和:

代码语言:javascript
复制
number = Math.Round(number, decimalPlaces)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66613605

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档