Calling a class from another ASP.NET VB.NET file

Let's say that I have a class similar to 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 an ASP.NET page with a file with the code, default.aspx and default.aspx.vb, and I want to call my_class. I am doing the following, but this does not 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 the link "Link to non-shared item requires link to object"

+3
source share
3 answers
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
+2
source

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

So instead:

Imports my_app.my_class

do the following:

Imports my_app

VB.NET , , . , , my_app , my_app.

, , Page_Load, my_class :

Dim foo As New my_class
my_class.my_sub()

my_class, foo, .

, , my_sub a Shared , :

Public Shared Sub my_sub()

, my_class my_sub - my_sub:

my_class.my_sub()
+3

Do you want to call my_sub () on my_class? You can either mark it as a generic metod so that it can be calledmy_class.my_sub()

or

create an instance of the instance:

Dim myclass as new my_class()
myclass.my_sub()
+1
source

Source: https://habr.com/ru/post/1742289/


All Articles