Can't access my class from code-behind. Class is App_Code folder
I have a very simple class that is located within my App_Code folder in my VS2008 web application project. I am trying to instantiate an instance of this class from my code-behind file. Intellisense does not seem to be seeing my class and I am not sure why. I am using VB.NET which I am admittedly not that familiar with as compared to C#. Perha开发者_StackOverflowps I am missing something. I would bet it has something to do with something I am missing in VB.NET.
Here is my simple class (for testing):
Public Class mySimpleClass
'Private member variables whose data is obtained from user input
Private mUserID as String
'Class Properties
Public Property UserID() as Integer
Get
Return mUserID
End Get
Set(ByVal Value as Integer)
mUserID = Value
End Set
End Property
'Class Methods
Public Function DisplayUserID() as String
Return this.UserID
End Function
End Class
Here is how I try an instantiate it from the codebehind ...
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 obj As New mySimpleClass()
End Sub
End Class
What I ended up doing was deleting my App_Code folder and creating a new "AppCode" folder. I also selected properties for the class file and set the Build Action property to "Compile". Once I did that and recompiled the project my class showed up.
- you should change
Return this.UserID
toReturn Me.UserID
(VB.Net ;-)) - rebuild the solution and see if it works
I'm not as familiar with the app_code folder and Websites in general, i'm always using WebApplications. I would suggest to convert it to a WebApplication too, here are further informations why: ASP.NET Web Site or ASP.NET Web Application?
Actually, I think if you add namespace to your project, it should work as well. I seem to remember having that problem every so often in C# asp.net as well. I could be way wrong though
Agree with Gustyn (sort of). Add a "using namespace;" line to the web form code behind
Dave
Going to each file holding the "public" class(es) in the "App_Code" folder and setting Build Action from Content to Compile will do the trick. It works for Web Application type projects. All the stuff on whether to use a module(VB) or namespace or static(C#) won't help until you set your class files to compile (no matter what folder they are in).
精彩评论