0

I'm very new to programming so this probably is a silly question for the most of you, but still and since I have already tried to look in Google, here it goes :

I created a class with some properties and 2 methods, some of the properties should be for the first method and the other for the second method.

both of the methods returns a list and my problem is that both of the lists returns all of the properties. Don't know why since I'am not using all of them in both methods... here is a example:

class orders { public string invoiceID { get; set; } public string employee { get; set; } public string store { get; set; } public string client { get; set; } public string invoiceDate { get; set; } public string total { get; set; } public string totalYear { get; set; } public string year { get; set; } public static List<orders> getOrders() { string sSQL = "bla bla bla huge query " DBConnect connect = new DBConnect(); DataTable dt = new DataTable(); dt = connect.getBD(sSQL); List<orders> o = new List<orders>(); for (int i = 0; i < dt.Rows.Count; i++) { orders ord = new orders(); ord.invoiceID = dt.Rows[i]["invoiceid"].ToString(); ord.employee = dt.Rows[i]["empname"].ToString(); ord.store = dt.Rows[i]["storename"].ToString(); ord.client = dt.Rows[i]["clientname"].ToString(); ord.invoiceDate = ((DateTime)dt.Rows[i]["invoicedate"]).ToString("dd-MM-yyyy"); ord.total = dt.Rows[i]["total"].ToString(); o.Add(ord); } return o; 

so in this method I am not using the public properties year and totalYear, but they still appear in the list :(

What am I doing wrong ?

Thank in advance and sorry for the noob question.

UPDATE 1 (second method)

 public static List<orders> getTotalYearInvoices() { DateTime date = DateTime.Now; int year = date.Year; List<orders> o = new List<orders>(); for (int i = 2009; i < year; i++) { string sSQL = " another huge query" DBConnect connect = new DBConnect(); DataTable dt = new DataTable(); dt = connect.getBD(sSQL); orders ord = new orders(); ord.year = i.ToString(); for (int j = 0; j < dt.Rows.Count; j++) { ord.totalYear = dt.Rows[j]["total"].ToString(); } o.Add(ord); } return o; } 
8
  • Can you confirm you posted all of the code for the class? I only see one method in your class. Commented May 17, 2013 at 11:47
  • @Matthew in this case it doesn't matter because the issue is his understanding of OOP Commented May 17, 2013 at 11:49
  • @Matthew I have only posted one method yes, i thought it was enough since the first method doesn't have all the properties set and I'am still getting them all in the list. Commented May 17, 2013 at 11:55
  • what list is it that they appear in? Commented May 17, 2013 at 11:58
  • 1
    As a side note it's customary that classes start with an upper case letter and be CamelCased. The same also goes for properties and method names too Commented May 17, 2013 at 12:00

5 Answers 5

3

I do not really understand what you mean but.

You properties:

public string totalYear { get; set; } public string year { get; set; } 

Are set to public, so they appear in the list of:

 List<orders> getOrders() 

If you do not want them to appear just make them private.

Also, If you do not want all the Orders properties to appear, you can create several classes and inherit from them.

Think that, you are retuning a List of Orders who have all properties set to public, so they will always appear, althought they don't have being initialized or have a value.

You should create two different classes. One for the Orders, and another one for the orders date if you do not want them to appear always.

Sign up to request clarification or add additional context in comments.

8 Comments

Try that already.. if I make them private, the list comes empty... "no data to display"
Show us the second method to understand what you are trying to do
Read again my answer. You have to understand how Oriented Object programming works. You are using to methods, but the class that you are using have all properties set to public, so they will always be listed. You can create two classes and use inheritance
I don't even think he has to use inheritance... He has one class called orders, but he's treating it like a shopping cart (or invoice system) that returns lists of orders. He should have two completely separate classes here.
Yeah I agree with you @Steve
|
3

I would have two classes for this considering you are trying to treat the orders class like some sort of invoice system or shopping cart.

public class InvoiceSystem { private List<Invoice> currentOrders; public InvoiceSystem() { currentOrders = new List<Invoice>(); } public void Populate() { //fill your list from the database } public void Save() { //Save list back to database } public List<Invoice> GetInvoices(/*perhaps something to filter */ ) { // return filtered list, or entire list. } public decimal GetTotalForYear(int year) { // iterate through list (or use linq) to get total and return it } } public class Invoice { public int InvoiceID { get; set; } public string Employee { get; set; } public string Store { get; set; } public string Client { get; set; } public DateTime InvoiceDate { get; set; } public decimal Total { get; set; } } 

1 Comment

Thank you for the explanation I'll change my class :)
1

your problem isn't your code your problem is what you think you are doing :)
you code OO code so you should learn what OO is and how it works

But now to your "problem" sure you will see your properties because they are all public so let's digg in to the OO based on your code

Let's begin with your class name orders

it should be Order because each object of this Type will be an single order.

Now lets see which Properties only relevant for an Order

public string invoiceID { get; set; } public string employee { get; set; } public string store { get; set; } public string client { get; set; } public string invoiceDate { get; set; } public string total { get; set; } 

good now we have all relevant properties.

After that we can goon with your methods

your first method public static List<orders> getOrders()
i would rename it to public static List<Order> getAllOrders()
getAll because i can't see an limitation in your provided code so your list will properly contain all Orders from your database else you should add the limitation to your method name ( sample getOpenOrders() )

ok now your second method

here it comes now we need the 2 Properties !?!

public string totalYear { get; set; } public string year { get; set; } 

do we? not really now you have multiple choices

  • create a separate class which will contains this Properties
  • use anonymous Type
  • use a Dictionary
  • use a DataTable

that's now your own decision ...

2 Comments

Thank you for this explanation, as I said, still learning. appreciate all the comments and answers.
@n3bi0s glad i could help you. Maybe you should dig a bit more in Object Oriented Programming based on some tutorials :)
1

What the getOrders method returns is a list of orders. orders class has the properties year and totalYear even if you do not set them, the required space for them are allocated when you instantiate an instance of this class.

BTW, i suggest you to use Pascal Casing for type names and also use the singular noun for them. (i.e., Order instead of orders).

Comments

1

You could split the class orders up, into two classes: (Is there a reason you didn't?)

public class BaseOrders { public string invoiceID { get; set; } public string employee { get; set; } public string store { get; set; } public string client { get; set; } public string invoiceDate { get; set; } public string total { get; set; } public static List<BaseOrders> getOrders() { //your implementation } } 

and then you could have your date orders

public class DateOrders { public string totalYear { get; set; } public string year { get; set; } public static List<DateOrders> getTotalYearInvoices() { //your implementation } } 

1 Comment

Thank you. I'll change my class then :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.