如何在不删除其他类的情况下访问另一个类中的变量?

发布于 2024-12-12 19:57:01 字数 387 浏览 2 评论 0原文

我负责将 VB6 项目移植到 VB.NET。在 vb6 中,如果您位于与特定变量分开的类中,则可以轻松访问该变量:

Public Class Foo
    Public k As Integer
End Class

Public Class Bar
    k = 12
End Class

在 VB.NET 中,我的理解是,在另一个类中使用变量之前,必须声明它的新实例:

Dim foobar As New Foo

这很好,但我必须从不同的类访问这些变量,每次声明一个新实例时,它都会擦除我需要的变量中的所有旧值。有人可以帮忙吗?我尝试使用 Inherits 语句,但它们提出了很多问题。

谢谢。 缺口

I have been charged with porting a VB6 project into VB.NET. In vb6, if you were in a class separate to a particular variable, you could access that variable easily:

Public Class Foo
    Public k As Integer
End Class

Public Class Bar
    k = 12
End Class

In VB.NET, my understanding is that before you can use a variable in another class, you must declare a new instance of it:

Dim foobar As New Foo

This would be fine, but I have to access these variables from different classes and every time I declare a new instance, it wipes all old values from the variables, which I need. Can anybody help? I tried using Inherits statements but they presented many problems.

Thanks.
Nick

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

八巷 2024-12-19 19:57:02
Dim YourobjName As YourClassName = Me.DataContext

现在您可以将公共方法和函数与 YourobjName 一起使用。这里的YourClassName将是你想要访问公共对象的类。

Dim YourobjName As YourClassName = Me.DataContext

Now you can use public methods and functions with YourobjName. Here YourClassName will be the class you want to access the public objects.

余厌 2024-12-19 19:57:01

您正在寻找 shared 关键字。这使得其他类可以使用该成员,而无需拥有您​​的类的实例。有关详细信息,请参阅 MSDN

Your're looking for the shared keyword. This makes the member available to other classes without having to have an instance of your class. See MSDN for more info

心欲静而疯不止 2024-12-19 19:57:01

对于端口,只需像在 vb6 中一样使用公共模块,

Public Module Foo
   Public k As Integer
End Module

Public Module Bar
   Foo.k = 12
End Module

这不是一个好的做法,但它将帮助您在端口上完成第一次传递。理想情况下,您将重构模块/共享函数,因为能够从系统中的任何部分访问变量将产生更难以维护的代码

For the port just use Public module like you would in vb6

Public Module Foo
   Public k As Integer
End Module

Public Module Bar
   Foo.k = 12
End Module

Its not good practice but it will help you do your first pass at the port. Ideally you would refactor out modules/shared functions as being able to access variable from any part in the system will produce code that is harder to maintain

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文