从另一个文件 ASP.NET VB.NET 调用类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2691402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:50:39  来源:igfitidea点击:

Calling Class from another File ASP.NET VB.NET

vb.netimport

提问by Dave Mackey

Lets say I have a class like this in class1.vb:

假设我在 class1.vb 中有一个这样的类:

Public Class my_class
  Public Sub my_sub()
   Dim myvar as String
   myvar = 10
   Session("myvar") = myvar
  End Sub
End Class

Then I have a ASP.NET page with a code-behind file, default.aspx and default.aspx.vb and I want to call my_class. I'm doing the following, but it doesn't work:

然后我有一个带有代码隐藏文件 default.aspx 和 default.aspx.vb 的 ASP.NET 页面,我想调用 my_class。我正在执行以下操作,但不起作用:

Imports my_app.my_class
Partial Public Class _default
   Inherits System.Web.UI.Page
 Protected Sub Page_Load(ByVal sender as Object, ByVal e as System.EventArgs) Handles Me.Load
   my_class()
 End Sub
End Class

I get a "Reference to a non-shared member requires an object reference"

我收到“对非共享成员的引用需要对象引用”

采纳答案by derek

Imports my_app.my_class
Partial Public Class _default
   Inherits System.Web.UI.Page
 Protected Sub Page_Load(ByVal sender as Object, ByVal e as System.EventArgs) Handles Me.Load
       Dim myClass as new my_class()
    myClass.my_sub()
 End Sub
End Class

回答by Andrew Hare

Try importing the namespace that contains the class, not the class itself.

尝试导入包含类的命名空间,而不是类本身。

So instead of this:

所以而不是这个:

Imports my_app.my_class

do this:

做这个:

Imports my_app

VB.NET imports namespaces into the file scope to help the compiler resolve type names that aren't fully qualified. This means you are free to use all types declared in the my_appnamespace in this code file without prefixing the type name with my_app.

VB.NET 将命名空间导入文件范围,以帮助编译器解析非完全限定的类型名称。这意味着您可以自由使用my_app在此代码文件的命名空间中声明的所有类型,而无需在类型名称前加上my_app.

Okay, once you have done that you will need to do switch up the contents of Page_Loadto create an instance of my_classlike this:

好的,一旦你完成了,你需要切换 的内容Page_Load来创建一个这样的实例my_class

Dim foo As New my_class
my_class.my_sub()

Now you have an instance of my_classcalled fooand you can call instance methods on it.

现在你有一个my_classfoo调用的实例,你可以在它上面调用实例方法。

The other thing you could do is make my_suba Sharedmethod so you don't have to create an instance:

您可以做的另一件事是创建my_sub一个Shared方法,这样您就不必创建实例:

Public Shared Sub my_sub()

If you do this then you do not need to create an instance of my_classto call my_sub- you can call my_subdirectly:

如果你这样做,那么你不需要创建一个my_class要调用的实例my_sub- 你可以my_sub直接调用:

my_class.my_sub()

回答by Chris W

Do you mean you want to call my_sub() on my_class? You can either mark it as a shared methog so that it can be called as my_class.my_sub()

你的意思是你想在 my_class 上调用 my_sub() 吗?您可以将其标记为共享的methog,以便可以将其称为my_class.my_sub()

or

或者

instantiate an instance of it:

实例化它的一个实例:

Dim myclass as new my_class()
myclass.my_sub()