首页 \ 问答 \ 从另一个文件ASP.NET VB.NET调用类(Calling Class from another File ASP.NET VB.NET)

从另一个文件ASP.NET VB.NET调用类(Calling Class from another File ASP.NET VB.NET)

可以说我在class1.vb中有这样的类:

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

然后我有一个带有代码隐藏文件的ASP.NET页面,default.aspx和default.aspx.vb,我想调用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

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


Lets say I have a class like this in 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:

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"

更新时间:2022-06-26 16:06

最满意答案

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

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

相关问答

更多
  • 直接去添加引用~然后去调用就可以了
  • 取自.... 如何在asp.net中发送带附件的邮件 //Attach file using FileUpload Control and put the file in memory stream If fileUpload1.HasFile Then message.Attachments.Add(new Attachment(fileUpload1.PostedFile.InputStream, fileUpload1.FileName)) End If 在mailclient.Host = m ...
  • 您可以使用Request实例轻松获取相关文件路径,然后使用它,使用Path类应该可以提供帮助: Dim relativePath = Request.AppRelativeCurrentExecutionFilePath Dim relativeDirectoryPath = System.IO.Path.GetDirectoryName(relativePath) 值得注意的是, GetDirectoryName可能会改变你的斜杠,所以你可以扩展路径: Dim mappedPath = HttpCont ...
  • 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 ...
  • 您是否尝试在搜索中添加“VB”一词? http://www.myvbprof.com/2007_Version/MVC_Intro_Tutorial.aspx http://www.asp.net/learn/mvc/tutorial-07-vb.aspx http://geekswithblogs.net/jwhitehorn/archive/2007/12/10/117569.aspx Have you tried adding the word "VB" to your searches?? http ...
  • 当页面第一次运行时,