Misunderstanding Objects in C #

This post refers 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 is an entry in the table. This would be easy to do in a static function.

Using the class, I was able to do this as follows:

Call Procedure:

ListOfBusinesses l = new ListOfBusinesses (); List<Business> b = l.listBusinesses(); 

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; } } 

The class could not be rewritten so that this could be done with a single line:

 ListOfBusinesses l = new ListOfBusinesses(); 

It seems to me that the ListofBusinesses class above is nothing more than a static function wrapped in a class that has no properties and exists only 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 the compiler error received: "member names cannot be the same as for the type of application." For example, I tried to use the constructor, but something is missing for me.

Any help will enlighten me in an area that I misunderstood for some time.

Mike Thomas

+4
source share
10 answers

I think you are confusing the concepts of static function, constructor and factory method.

Static function

Definition

This is a method that does not have access (and is not associated) with an instance of this class.

Example

 public class BusinessHelper { public static 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; } } 

Using

Call a static method with the name of the class, not an instance of the class.

 List<Business> businesses = BusinessHelper.ListBusinesses(); 

Constructor . This is the method that instantiates this class. It has no return value and is called when the object is created.

Example

 public class BusinessList { public List<Business> TheList; public BusinessList() { TheList = new List<Business>(); TheList.Add(new Business("1", "Business Name 1")); TheList.Add(new Business("2", "Business Name 2")); } } 

Using

Create a new instance of the object.

 BusinessList myBusinessList = new BusinessList(); businesses = myBusinessList.TheList; 

Factory Method

Definition

This is a method that creates an instance of an object, somehow creates it and returns a link to it.

Example

 public class BusinessList { public List<Business> TheList; public static BusinessList BusinessListWithTwoCompanies() { BusinessList instance = new BusinessList(); businesses = new List<Business>(); businesses.Add(new Business("1", "Business Name 1")); businesses.Add(new Business("2", "Business Name 2")); return instance; } } 

Using

Call the factory method instead of creating a new object.

 BusinessList myBusinessList = BusinessList.BusinessListWithTwoCompanies(); businesses = myBusinessList.TheList; 

Two things to note additionally:

  • You declare the businesses field, but proceed to create another variable named businesses in your ListOfBusinesses() method and return it. Nothing will happen to the businesses field. Be careful with variable coverage.

  • You cannot have a member (field, property, or method) with the same name as the class. This is reserved for a constructor that does not have a return type (see above). This is why you get a compiler error.

+9
source

Sure. Instead of encapsulating a List<Business> , continue with it. Then you just need 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(); 
+5
source

The object implementation is basically the same for all OO languages. Using classes rather than static functions provides much more flexibility and, ultimately, less coding, especially when tracking many similar elements.

Think of books and libraries.

If you have a book as an object class, you can create an instance to create many books and save them in your library. Each book you have created is unique. If you have not made any changes, then each copy 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, the number of pages and content, BUT you can easily write your name in one copy, which makes it different from the rest.

If you made a book static, although you cannot create separate copies, instead you look at the same book, but from different angles. If you write your name in it, your name will appear in all representations of the book.

I won’t post any code until I typed this many others, posted code samples for your business objects.

+2
source

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

+1
source

Your compiler error because your class name is "ListOfBusinesses" and the method name is also "ListOfBusinesses". That would be nice if it were a constructor, but since you have a return type, C # thinks you mean it as a method, not a constructor.

As for getting a list of companies, why not create such a class:

 public class BusinessService { public List<Business> GetBusinesses() { // Build and return your list of objects here. } } 

Then, to use it:

 BusinessService service = new BusinessService(); List<Business> businesses = service.GetBusinesses(); 
+1
source

Are you trying to return something other than a class in the constructor

 public class ListOfBusinesses { ... public List<Business> ListOfBusinesses() { ... 

You cannot specify the type of the return value in the constructor, you need:

 public ListOfBusinesses() { ... 

As Mike Thomas said, you should use a factory if that is what you want.

+1
source

The problem is that you are using the reserved word (new) used to call the class constructor .

The semantics of the constructor is to return an instance of the created class and follow the rule of no return value with the same class name.

If that were not the case, then if you make any new MyObject ... how would you (or the compiler, for that matter) need to know the return type?

0
source

Generally speaking, you would not have created your ListOfBusinesses class, unless ListOfBusinesses has some properties that are not available in List <Business>. The simplest way to handle this is a static method in a business class, for example:

 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; } } 

Although this is a straightforward path, the end result of descending this road is the reorganization of this method from the Business class and into a factory object, which is usually provided through an ORM infrastructure such as NHibernate or Castle Windsor.

0
source

As people have already said, your syntax is wrong here. ListOfBusiness() is a constructor and does not explicitly return an object. Instead, it works with a new instance of ListOfBusiness and should take care of any creation.

However, if your method of creating a List<Business> is simple, there is no reason for it to be part of the static method. You can even define an extension method in List that takes an empty list of it and populates it. This is called the Factory pattern, in which a method or instance of an object takes responsibility for creating new instances.

If you want to think about expanding ListOfBusiness in an instance class, this is if there are properties or status that you need to monitor. IF the List<Business> changed every time it is called, or if you need a place to add new enterprises, the ListOfBusiness class can be useful for this.

0
source

If I'm not mistaken, you want to access db and get a list of objects with a single call.

First of all, you need a DAO class that encapsulates access to db and exposes the List method. Inside DAO, you can use NHibernate, Linq2XXX or whatever (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 can just call

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

returns IQueryable, and List can help if you included Linq.

0
source

All Articles