This post goes to a gap in my understanding of C# classes and why they are preferable to static functions.
I am trying to get a List of objects. Each object in the list represents a record in a table. This would be easy to do in a static function.
Using a class, I've been able to do it as follows:
Calling routine:
ListOfBusinesses l = new ListOfBusinesses ();
List<Business> b = l.listBusinesses();
The classes:
public class Business
{
public string Bupk { get; set; }
public string Bu_name { get; set; }
}
public class ListOfBusinesses
{
public List<Business> listBusinesses()
{
List<Business> businesses = new List<Business>();
businesses.Add(new Business("1", "Business Name 1"));
businesses.Add(new Business("2", "Business Name 2"));
return businesses;
}
}
Couldn't I rewrite the class so that this could be done with one line:
ListOfBusinesses l = new ListOfBusinesses();
It seems to me like the ListofBusinesses class above is nothing but a static function wrapped in a class which has no properties and is only there for the sake of having a class.
I tried:
public class ListOfBusinesses
{
List<Business> businesses;
public List<Business> ListOfBusinesses()
{
List<Business> businesses = new List<Business>();
businesses.Add(new Business("1", "Business Name 1"));
businesses.Add(new Business("2", "Business Name 2"));
return businesses;
}
}
But received the compiler error "member names cannot be the same as there enclosing type". Eg, I tried to use a constructor, but am missing something.
Any help would enlighten me in an area I have misunderstood for some time.
Mike Thomas
If i'm not wrong you want to access a db and retrieve a list of objects with a single line call.
First of all you need a DAO class that encapsulates the db access and expose a List method. Inside the DAO you can use NHibernate, Linq2XXX or whatever you want (sample)
your client code could just call
returning an IQueryable instead a List could help if you are Linq enabled.
The problem is you're using a reserved word (new) used to invoke the constructor of the class.
The semantic of the constructor is to return an instance of the class which is being created and it follows the rule of having no return value and having the same name of the class.
If it wasn't that way, then, if you do any
new MyObject
... how would you (or the compiler for that matter) be supposed to know the returning type?Generally speaking, you wouldn't create your ListOfBusinesses class, unless a ListOfBusinesses has some properties that aren't available in a List<Business>. The simplest way of handling this is a static method on the Business class, like so:
While this is the straightforward way, the end result of going down this road is to refactor this method out of the Business class and into an object factory, commonly provided through an ORM framework like NHibernate or Castle Windsor.
Sure. Instead of encapsulating
List<Business>
, extend it. Then you just have to add things to it in the constructor.To use it:
Object instantiation is fundamentally the same for all OO languages. Use of classes rather than static functions allows for much more flexibility and ultimately less coding, especially when keeping track of many similar items.
Think of books and libraries.
If you have the book as an object class then you can instantiate it to create lots of books and store them in your library. Every book that you have instatiated is unique. If you have made no changes to it then each instantiated book appears to be a copy of the original (although at a low level each book has a unique serial number). It has the same cover, number of pages and content, BUT you can easily write your name in one copy thus making it different to the rest.
If you made the book a static then although you can't create individual copies, instead you are looking at the same book, but from different points of view. If you write your name in it then your name appears in every view of the book.
I wont post any code as whilst I've been typing this plenty of others have posted code samples for your business objects.
You are looking for a factory method instead of a constructor.