Not Understanding Object Instantiation in C#

2019-05-04 17:27发布

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

10条回答
兄弟一词,经得起流年.
2楼-- · 2019-05-04 18:06

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)

public class BusinessItem
{
    public string Code { get; set; }
    public string Description { get; set; }
}

public class BusinessItemsDAO
{
...
    public List<BusinessItem>   List()
    {
        // fake... should retrieve from db
        var list = new List<BusinessItem>();

        // returs the list
        return list;
    }
...
}

your client code could just call

var listOfBusinessItems = new BusinessItemsDAO().List();

returning an IQueryable instead a List could help if you are Linq enabled.

查看更多
女痞
3楼-- · 2019-05-04 18:08

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?

查看更多
男人必须洒脱
4楼-- · 2019-05-04 18:08

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:

public class Business
{
    //Business class methods/properties/fields

    static List<Business> GetList()
    {
        List<Business> businesses = new List<Business>();
        businesses.Add(new Business("1", "Business Name 1"));
        businesses.Add(new Business("2", "Business Name 2"));
        return businesses;
    }
}

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.

查看更多
叼着烟拽天下
5楼-- · 2019-05-04 18:15

Sure. Instead of encapsulating List<Business>, extend it. Then you just have to add things to it in the constructor.

public class ListOfBusinesses : List<Business> {
    public ListOfBusinesses() : base() {
        Add(new Business("1", "Business Name 1"));
        Add(new Business("2", "Business Name 2"));
    }
}

To use it:

List<Business> l = new ListOfBusinesses();
查看更多
时光不老,我们不散
6楼-- · 2019-05-04 18:15

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.

查看更多
相关推荐>>
7楼-- · 2019-05-04 18:16

You are looking for a factory method instead of a constructor.

查看更多
登录 后发表回答