Avatar of josephdaviskcrm
josephdaviskcrm
Flag for United States of America asked on

Cannot access class in a vb.net web application

I'm building a vb.net web application and I've added a class to the project in the App_Code directory.  Now I am not able to access the class to call any of it's methods.  I know this is something very simple, I just can't seem to figure it out.
ASP.NET

Avatar of undefined
Last Comment
sreerajrs

8/22/2022 - Mon
anoyes

Are you calling them by Class.Method, or just Method?  If you're just calling Method, then you need to import the class using Imports Class at the top of your code-behind file.
josephdaviskcrm

ASKER
Yes I am calling it by class.method.

How do I import it at the top?

Imports ClassName doesn't work.
nmarun

Please post the namespace of the class you have added. I think you'll have to provide the complete namespace:

Imports [project's namespace].App_Code
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
nmarun

Or you can call this class as:

Dim myClass as App_Code.MyClass
myClass = new AppCode.MyClass
ASKER CERTIFIED SOLUTION
anoyes

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
sreerajrs

Hi josephdaviskcrm,
Make sure that you have declared your class as public.
You can access only shared members with out instanciation.
Case 1:
Public Class Class1
    Shared Function calclulate(ByVal x As Integer, ByVal y As Integer) As Integer
        Return x + y

    End Function
Public Function HelloWorld() As String
        Return "HelloWorld"

    End Function
End Class
You may call the first method as
Class1.calculate(10,10)
and the second one as
dim objClass as new Class1
objClass.HelloWorld()
sreerajrs

Just adding up to the above solution. Also check whether both you class and your webform are in same namespace else you need to use an import statement
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.