声明全局,静态变量 [英] Declaring global, static variables

查看:125
本文介绍了声明全局,静态变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Visual Studio中设置全局变量,但不能将其设置为静态.我有什么办法可以将变量设置为静态变量并在不同方法之间共享,还是可以通过某种方式在每次更改时保存变量?

I'm trying to set a global variable in Visual Studio, but I can't make it static. Is there any way for me to set the variable as static and share it across different methods, or some way to save the variable each time it changes?

推荐答案

您有两个选择:

1-创建一个包含Shared变量的类(与C#中的静态变量相同)

1 - Create a class that contains a Shared variable (this is the same as a static variable in C#)

    Public Class GlobalVariables
        Public Shared Bar As String
    End Class

然后您可以使用类名来访问它:

You can then access this using the class name:

GlobalVariables.Bar = "Hello world"

2-创建一个模块(类似于C#中的静态类)

2 - Create a module (this is akin to a static class in C#)

    Public Module GlobalVariables
        Public Bar As String
    End Module

然后您可以通过以下代码访问此值:

You can then access this value in code like this:

Bar = "Goodbye cruel world"

这篇关于声明全局,静态变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆