Reputation: 10297
I have created folders in my project named Classes, Forms, and Models.
Let's say my project is named ABC, so the folder hierarchy is:
ABC
Classes
Forms
Models
In \Models\, I have a class named ApplicationModel.cs, which contains a public method named GetApplications().
However, when I call that method from elsewhere in the same ABC project, I get, "The name 'GetApplications' does not exist in the current context"
I've added:
using ABC.Models;
to the calling class, but it makes no difference. I right-clicked GetApplications() to hopefully see "Resolve" there, but no go.
What must I do to access my own public method?
Upvotes: 1
Views: 149
Reputation: 3793
Looks like the class is not marked as public.
You class should be
namespace ABC
{
namespace Models
{
public class ApplicationModel //class needs to be public is accessed outside the namespace
{
}
}
}
Upvotes: 1
Reputation: 28772
It is hard to give definitive advice without some code on how you are trying to call that method. I can think of two possible ways:
ApplicationModel
class(ApplicationModel.GetApplications()
), in which case you need to declare the method static
ApplicationModel
and call the method on that object; (e.g. ApplicationModel model = new ApplicationModel(); model.GetApplications();
)Upvotes: 1
Reputation: 16718
It would be helpful to see the definition of GetApplications()
and the code that's attempting to call it, but I assume it's either a static or an instance method of the ApplicationModel
class. In either case, you may have made your code aware of the namespace of the ApplicationModel
class with the using statement, but the method must either be called on the class or an instance of the class, like so:
If GetApplications
is a static method,
var applications = ApplicationModel.GetApplications();
If it's an instance method:
var appModel = new ApplicationModel(); // or, retrieve the instance from elsewhere...
var applications = appModel.GetApplications();
One way or another, you must refer to the class containing GetApplications
in order to call it. If this doesn't help you solve the problem, please edit your question to contain the definition of the method, and the calling code.
Upvotes: 5
Reputation: 16697
Sounds like you're using a static function. Did you forget the static keyword?
A static function "runs" from a class, not an object:
public static string[] GetApplications()
Upvotes: 1