Can't access my class from code-behind. Class is App_Code folder
Asked Answered
S

5

6

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#. Perhaps 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
Susann answered 15/2, 2011 at 16:57 Comment(2)
Build the solution and retry it may help.Empirin
@Empirin - I tried this and no luck. It still says my class is un recognized. I thought by placing them in the App_Code folder any classes would be available. I did have to manually create the App_Code folder. Does that matter?Susann
S
12

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.

Susann answered 15/2, 2011 at 18:4 Comment(3)
Thanks! I didn't have to delete a folder, I just had to set the .vb class files Buld Action to 'Compile'. Quite why VS set it to 'Content' when it's in App_Code I don't know...Jolynnjon
I've never seen this before, especially as a C# dev, but I ran into this at a company I did a little consulting for. They use VB...this answer solved my issue...for some reason all new classes were set to "content" instead of "compile" for build-action.Astute
I just burned a couple of hours of time trying to create extension methods in a module added to the project with Project > New Item > Module. I tried explicit namespace, explicit 'Public', but nothing worked. I normally work in C#, but have some VB experience, so I was utterly flabbergasted at why this wasn't working. Turns out the problem was as the answer indicates: the module file (a code file) was added by VS with Build Action = Content. WTF?!Touber
N
1
  1. you should change Return this.UserID to Return Me.UserID (VB.Net ;-))
  2. 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?

Noland answered 15/2, 2011 at 17:47 Comment(0)
D
1

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

Disenfranchise answered 17/12, 2012 at 0:51 Comment(0)
G
0

Agree with Gustyn (sort of). Add a "using namespace;" line to the web form code behind

Dave

Glynnis answered 18/2, 2014 at 15:19 Comment(0)
I
0

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).

Isooctane answered 10/2, 2016 at 8:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.