none
structures or classes RRS feed

  • Question

  • <!-- @page { margin: 0.79in } P { margin-bottom: 0.08in } -->

    Hello every one

    i have a situation where i use

    a static array of a structure objects

    that has two integers and a collection of another structure objects

    that has three integers and a string

     

    let me explain more

     

    Let's say I have an orders_array created like that:

     

    Public static order[] orders_array;

    ________________________________________

     

    Every order object is a structure defined like that:

     

    public struct order

    {

    public List<order_item> order_items_collection;

    public int order_number;

    public double order_sum_val;

    }

    _________________________________________

     

    And every order_item object is a structure defined like that:

     

    public struct order_item

    {

    public string item_name;

    public double item_price;

    public int item_count;

    public double item_val;

     

    public item_data_row(string _item_name, double _item_price,

    int _item_count, double _item_val)

    {

    item_name = _item_name;

    item_price = _item_price;

    item_count = _item_count;

    item_val = _item_val;

    }

    }

    _________________________________________

     

    Now the question is :

    Should I use an order_item class and an order class instead of the structures?

    Please tell me If yes why? And if no why?


    Tuesday, May 10, 2011 1:26 AM

Answers

  • How are you using this static array in your code? Do you create copies of it or pass it around in method calls? If the answer is yes, you want to use classes.

    To expand on Calle's example, this gets really confusing because of the mixture of class members (the List) within a struct. Consider this: 

    // Because itemList is a reference to a class, this WILL update the 
    // item_price of item 0 in the order_items_collection object
    List<order_item> itemList = order.order_items_collection;
    itemList[0].item_price = 25.0;
    
    // Because item is a copy of order_items_collection[0], this WILL 
    // NOT update the item price of item 0 in the 
    // order_items_collection object
    order_item item = order.order_items_collection[0];
    item.item_price = 25.0;
    
    For the sake of avoiding confusion like this, I would declare them as classes. You should use structs only if you want to store a simple value that won't change. If you have a collection of different pieces of data, and that data may conceivably change, use a class.
    • Proposed as answer by Paul Zhou Saturday, May 21, 2011 3:53 AM
    • Marked as answer by Paul Zhou Tuesday, May 24, 2011 6:04 AM
    Wednesday, May 11, 2011 1:42 PM

All replies

  • Here's my take on this - If all that you have is data in your data structure, then you could use a structure. Instead if you plan to add functionality ( methods ) that manipulate and work on the data that gets stored within the data structure then class is the right way.

    As far as the differences are concerned the only one is that by default the access specifier for a class is private and that for a structure is public.

    Wednesday, May 11, 2011 8:59 AM
  • You should definitely use classes. Structures are value types, it means they would end up on the stack. It also means that if you copy an instance to another you would end up with with a full copy, not a reference.
    Let me give you an example:

    order_item item = order.order_items_collection[0];
    item.item_price = 25.0;

    The above code would only change the price for the local item, not the item in the collection.

    There are plenty of more reasons why you should use classes instead of structures, but in your case I think the most obvious would be the above.

    /Calle


    - Still confused, but on a higher level -
    Wednesday, May 11, 2011 9:58 AM
  • How are you using this static array in your code? Do you create copies of it or pass it around in method calls? If the answer is yes, you want to use classes.

    To expand on Calle's example, this gets really confusing because of the mixture of class members (the List) within a struct. Consider this: 

    // Because itemList is a reference to a class, this WILL update the 
    // item_price of item 0 in the order_items_collection object
    List<order_item> itemList = order.order_items_collection;
    itemList[0].item_price = 25.0;
    
    // Because item is a copy of order_items_collection[0], this WILL 
    // NOT update the item price of item 0 in the 
    // order_items_collection object
    order_item item = order.order_items_collection[0];
    item.item_price = 25.0;
    
    For the sake of avoiding confusion like this, I would declare them as classes. You should use structs only if you want to store a simple value that won't change. If you have a collection of different pieces of data, and that data may conceivably change, use a class.
    • Proposed as answer by Paul Zhou Saturday, May 21, 2011 3:53 AM
    • Marked as answer by Paul Zhou Tuesday, May 24, 2011 6:04 AM
    Wednesday, May 11, 2011 1:42 PM
  • You have objects with state...

    You should use class

     

    Wednesday, May 11, 2011 5:56 PM