如何在不删除其他类的情况下访问另一个类中的变量?
我负责将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
现在您可以将公共方法和函数与 YourobjName 一起使用。这里的
YourClassName
将是你想要访问公共对象的类。Now you can use public methods and functions with YourobjName. Here
YourClassName
will be the class you want to access the public objects.您正在寻找
shared
关键字。这使得其他类可以使用该成员,而无需拥有您的类的实例。有关详细信息,请参阅 MSDNYour'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对于端口,只需像在 vb6 中一样使用公共模块,
这不是一个好的做法,但它将帮助您在端口上完成第一次传递。理想情况下,您将重构模块/共享函数,因为能够从系统中的任何部分访问变量将产生更难以维护的代码
For the port just use Public module like you would in vb6
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