Reputation: 11469
I am learning user controls in C# so basically I have a class called Books
. In this class I have a static method public static string[] GetBooks()
, this method returns the booknames.
public static string[] GetBooks()
{
return myBookList.ToArray();
}
Now I have a UserControl
where I placed a DataGrid
, and I want to be able to do something like dataGrid.DataSource = Books.GetBooks();
.
The problem is that the intellisense is not getting the .GetBooks()
and I think it is because it is static and I want to keep it static. How can I do this and also, where would the best place to make this call? I assume in the behing code file of ascx right?
Upvotes: 1
Views: 137
Reputation: 761
If it is static and you're doing it as posted, i.e. Books.GetBooks(), check if the class and the code behind are in the same namespace, or if you're declaring using it. Also be sure the class Books is public. If it is in App_Code, set Build Action = Compile in the Properties.
Upvotes: 1
Reputation: 124770
The problem is that the intellisense is not getting the .GetBooks() and I think it is because it is static and I want to keep it static.
Why? Does it make sense for this method to be static, i.e., would it be reasonable to assume that all instances of Books
would share the same underlying Book data? If not then it shouldn't be static.
If it should be static then you need to reference it like so (as opposed to using an instance reference)
var books = Books.GetBooks();
I assume you have something like this right now:
var b = new Books();
var books = b.GetBooks(); // Won't work, as GetBooks is static, not an instance method
Upvotes: 4