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