c sharp-listbox kullanimi

Upload: mona-lisa

Post on 05-Jul-2018

232 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/16/2019 C Sharp-listbox Kullanimi

    1/26

      Page 1

     

    Visual C# – Displaying Objects in a ListBoxJorge Vallejos – Instructor

    Columbus State Community College

    Mulmedia Integrated Technology – Computer Informaon Technology

    April – 2009 

    Abstract: This paper describes a technique for displaying references to objects in a listbox, as opposed

    to presenng paral informaon about the objects in the listbox. The benefit of the proposed approach is

    that the graphical component of the applicaon – the code in a window form class – becomes decoupledfrom the data associated with the objects, as well as from the operaons applied to the data. The window

    form class is elegantly simple, because the objects stored in the listbox are constructed from a class

    derivaon – the code that manipulates the items in the listbox is unaware of the diff erent types of objects

    maintained in the container. Aer evaluang three design alternaves, the two-er model is chosen to

    build a window form applicaon that simulates a grocery list – the code in the presentaon er and the

    business er can be extended, independent of each other. The technique uses the listbox’s

    DisplayMember property to display informaon about the objects.

    Keywords: class derivaon, code extensibility, event handler, inheritance diagram, listbox, object-

    oriented programming, reference, two-er applicaon, Visual C#.

    Introduction

     

    This paper describes a technique to display references to objects in a window form’s

    listbox, using one of the public properes of a C# class. The technique is illustrated with

    the Visual C# programming language; the same technique is also applicable to window

    forms built with the Visual Basic programming language. Both languages are supported

    in the Microso Visual Studio 2008 Integrated Development Environment.

    A listbox is an object contained in a window form, and is used to display a list of values.

    The listbox Items collecon property displays the informaon stored in the listbox. The

    items listed in the collecon are of data type string.

    Normally a programmer develops code that responds to the clicking of values displayed

    in the listbox, by implemenng the SelectedIndexChanged () event handler method.

    Very oen the data that should be stored in a listbox is much more complex than just a

    set of string values; in most of the cases, the listbox should store a set of objects

    (actually, store a set of references to objects, to be more precise). Storing objects in a

    listbox is advantageous because:

  • 8/16/2019 C Sharp-listbox Kullanimi

    2/26

      Page 2 

    1. 

    The objects are maintained in the container (the listbox itself). There is no need 

    to access other code, artificially built into the window form application, to 

    manufacture object data, and use it for processing. 

    2.  The class used for creating the objects provides a public interface to manipulate 

    the objects. The window form application only needs to know and use the public 

    interface of 

     the

     class

      –

     that

     is,

     invoke

     the

     public

     methods

     and

     properties

     of 

     the

     

    objects to display information on the controls of  the Graphical User Interface 

    (a.k.a., GUI). 

  • 8/16/2019 C Sharp-listbox Kullanimi

    3/26

      Page 3 

    Implementation Alternatives

    An example of  an object that could be stored in a listbox is a food item, say a gallon of  

    milk purchased at a grocery store. In a simple manner, this food item could be 

    characterized as follows: 

    Food Item  Type  Unit  Calories/Serving Price/Unit 

    Milk  Dairy  Gallon  90  $2.75 

    Because the listbox displays string values, programmers may use at least three 

    approaches to present object information in a listbox: 

    1.  Populate the listbox with the names of  the objects (i.e., “Carrot”, “Milk”, 

    “Onion”, etc.) 

    a.  In the window form class, create named constants to identify the 

    attributes 

    of  

    each 

    object 

    (e.g., 

    MILK_TYPE 

    “Dairy”, 

    MILK_UNIT 

    “Gallon”, etc.). Similar code would have to be created for the object 

    Carrot, Onion, and any other objects. 

    b.  In the listbox’s SelectedIndexChanged  () event handler method, write 

    selection statements to: 

    i. 

    First, determine the food item name (e.g., is the selected item 

    “Milk”?). 

    ii.  Second, access the named constants associated with the food 

    item, and perform any required processing  – e. g., given a 

    quantity, calculate the cost of  the purchased food item. 

    c.  The code in the window form class needs to know the business rules to 

    process the

     object

     data.

     

    This design approach has serious disadvantages in terms of  code maintainability. 

    The data is tightly engraved in the window form class. The code in the event 

    handler method becomes complex due to the multiple selection statements. It is 

    easy to introduce logical errors when adding, updating, or deleting object data. 

  • 8/16/2019 C Sharp-listbox Kullanimi

    4/26

      Page 4 

    2. 

    Populate the listbox with the names of  the objects (e.g., “Carrot”, “Milk”, 

    “Onion”, etc.) 

    a.  In the window form class, create parallel arrays (with their intrinsic fixed‐

    size limitations), or create parallel ArrayLists, and populate each one of  

    the containers with the attribute values of  each object. For the above 

    example, the

     window

     form

     class

     would

     include

     four

     additional

     

    containers; one to store the Type values, one to store the Unit values, 

    etc. 

    b.  In the listbox’s SelectedIndexChanged  () event handler method, use the 

    value of  the listbox’s SelectedIndex  property as the index into the 

    corresponding containers, extract the values, and perform any required 

    processing. 

    c.  The code in the window form class needs to know the business rules to 

    process the object data. 

    This second design approach is an enhancement over the first one; nevertheless, 

    the partition of  the object data into separate containers causes anomalies in the 

    code: 

      First, data has to be synchronized between the listbox and its associated 

    parallel relatives (the array(s) or the ArrayList(s))  – meaning that, addition 

    or deletion of  objects requires to maintain synchronized multiple 

    containers. 

      Second, displaying the object’s data on the window form also forces to 

    access multiple containers. 

      The code in the window form class is still tightly coupled with the object 

    data. 

    3. 

    Populate the listbox with references to objects.  The listbox, like any C# 

    container (array, ArrayList, ComboBox, CheckListBox, etc.), is capable of  storing 

    references to objects. Storing references to objects implies that the design is 

    implemented in a series of  classes  – one class for each type of  object identified in 

    the system. In this case, each food item is implemented in its own class (i.e., 

    class Carrot, class Milk, class Onion, etc.). In addition, different objects that 

    share common attributes and behaviors can be implemented in a class 

    derivation, with a common base class at the root of  the inheritance hierarchy. 

    After storing the references to the objects in the listbox, event handler methods 

    that need

     to

     access

     the

     objects

     in

     the

     container,

     simply

     obtain

     a reference

     to

     the

     base class, ignoring the actual data type (i.e., class) of  the object. The code in the 

    window form class can then treat all the objects in the listbox as if  their data 

    types were the base class, and invoke the public methods and properties of  the 

    object. For example, objects of  type Carrot, Milk, or Onion can all be treated as 

    Food, the base class of  all food items. The following benefits result from this 

    third design approach: 

  • 8/16/2019 C Sharp-listbox Kullanimi

    5/26

      Page 5 

    a. 

    The code in the window form class becomes decoupled from the data, as 

    well as decoupled from the operations that should be performed on the 

    data. The food class derivation provides public properties and methods to 

    manipulate each object. The window form class only needs to know the 

    public interface of  the food class derivation. 

    b. 

    The code

     in

     the

     window

     form

     class

     is

     structured

     in

     a sequence

     (one

     

    instruction statement after another); there are no selection statements, 

    the code is simpler. 

    c.  All the objects are in one place, in the listbox itself   – there is no need to 

    access other constructs. 

    d.  All the references to objects maintained in the listbox are treated in a 

    homogenous way, regardless of  the specific food item  – all the objects 

    are treated as if  they were of  type Food. 

    e.  The overall design of  the system is divided into two independent 

    components or tiers: 

    i.  The presentation tier  – encapsulated in the window form class. 

    The code is responsible for serving the user requests, through the 

    implementation of  a set of  event handler methods. 

      The window form class is unaware of  the kind of  data 

    stored in the listbox  – different kinds of  foods can be 

    added to or deleted from the listbox; the code continues 

    to work, without any changes. The window form class 

    simply uses the public interface of  the food class 

    derivation. 

    ii.  The business tier  – implemented in the food class derivation. The 

    classes encapsulate the data values into attributes and expose a 

    public interface

     to

     manipulate

     the

     objects.

     

      New classes (e.g., Watermelon) can be added to the 

    system, or existing classes can be updated or deleted from 

    the system  – these changes do not affect the presentation 

    tier, or have a minimum effect in it. Changes in the class 

    derivation, as well as changes in the food items data (say 

    the price changes from $2.58 to $1.49) are managed 

    within the food class derivation. 

    iii.  Each tier component can be extended with new functionality, 

    independent of  each other. For example, the GUI component can 

    be enhanced

     to

     present

     object

     data

     in

     a different

     manner

     (e.g.,

     have a separate list displaying the grocery items currently 

    purchased), or the data in the business tier could be obtained 

    from a database (instead of  being encoded in the classes 

    themselves). That is, add a third database‐tier. The changes 

    derived from the proposed code extensions are managed within 

    the tier itself. 

  • 8/16/2019 C Sharp-listbox Kullanimi

    6/26

      Page 6 

    This paper uses the two‐tier model to demonstrate storing of  references to objects in a 

    listbox. 

  • 8/16/2019 C Sharp-listbox Kullanimi

    7/26

      Page 7 

    Methodology

    When references to objects are stored in a listbox, the Items collection property 

    displays the string representation of  the objects, through a call to their class’ ToString() 

    method. If 

     the

     class

     does

     not

     implement

     its

     own

     version

     of 

     method

     

    ToString(), the

     string

     

    representation of  the objects is automatically obtained through a call to the inherited 

    version of  the ToString() method, from class Object   – the default implementation of  

    method ToString() in class Object  returns the name of  the class. 

    There are two alternatives to display object information in the listbox’s Items collection: 

     

    Override the class’ ToString() method, or 

      Set the listbox property DisplayMember 1, in the properties window, during the 

    design of  the form. 

    This paper uses the listbox property DisplayMember  to display object information in the 

    Items collection. During design time, the listbox’s DisplayMember  property has to be set 

    to the name of  one of  the  public  properties of  the class whose objects will be 

    maintained in the listbox. The technique is simple and does not require adding extra 

    code to the class (i.e., the class does not implement the ToString() method). For 

    example, it is possible to set the property DisplayMember  to a  public  property 

    FirstName, but if  such option is not satisfactory, the property DisplayMember  can be 

    set to a  public  property LastName, without changing the class code. The property 

    DisplayMember  can be set to the name of  a single  public  property of  a class. (Property 

    DisplayMember  cannot be set to multiple public properties of  a class). 

    Usage of  the listbox’s DisplayMember  property is illustrated through a window form 

    application that simulates a grocery list. The user of  the form clicks one of  the grocery 

    items to view all its details. In this case, the listbox stores references to a set of  objects 

    of  class Food , and the listbox property DisplayMember  displays the values of  Food ’s 

     public  property Name. 

    1 CheckListBoxes do not support the DisplayMember property, but ListBoxes and ComboBoxes do support 

    it. 

  • 8/16/2019 C Sharp-listbox Kullanimi

    8/26

      Page 8 

    The grocery list two‐tier application is composed of: 

    1.  Class ObjectInListForm: The window form class that displays grocery data and 

    provides operations for: adding to, removing from, and clearing the grocery list. 

    Class ObjectInListForm invokes the public interface of  class Food  to present data 

    on the

     GUI.

     

    2.  The Food class derivation: Food  is the base class for the different types of  foods 

    available at the store. The Food  class derivation encodes the business tier of  the 

    grocery list application. 

    Appendix A contains:   The GUI design: 

    o  Figure 1 identifies each one of  the controls on the window form. o  Figure 2 illustrates the listbox’s DisplayMember  property set to the value 

    Name  – a public property of  class Food   – see also Figure 4. 

    Figure 3 shows

     the

     Error

     Provider

     component

     added

     to

     the

     window

     form.

     This component is used in the event handler methods associated with the 

    clicking of  the addButton and deleteButton objects, respectively. 

      The Food hierarchy derivation: 

    o  The following data was used in the grocery list application: 

    Food Item  Type  Unit  Calories/Serving Price/Unit 

    Banana  Fruit  Pound  121  $0.50 

    Carrot  Vegetable  Pound  27  $0.98 

    Cheese  Dairy  8‐ounce  200  $4.49 

    Egg  Protein  Dozen  54  $1.59 

    Lettuce  Vegetable  Pound  4  $0.75 

    Milk  Dairy  Gallon  90  $2.75 

    New Cola  Beverage  8 fl. Oz  120  $1.25 

    Orange  Fruit  Pound  85  $0.80 

    Roast Beef   Protein  Pound  210  $10.89 

    Salmon  Fish  Pound  233  $15.19 

    Tomato  Fruit  Pound  27  $2.70 

    Water  Beverage  Gallon  0  $0.88 

    Table 1:  Characterization of  the food items 

  • 8/16/2019 C Sharp-listbox Kullanimi

    9/26

      Page 9 

    o  By inspecting the characteristics of  the data, it can be seen that all of  the food items have common characteristics (a.k.a., attributes); for example, 

    Table 1 shows that all items have a name, type, unit, calories, etc. Only 

    the values associated with the characteristics are different. It is possible 

    then to treat all the specific food items in a general manner; that is, each 

    item is

     some

     sort

     of 

     food.

     Consequently,

     Tomato

     is

     a Food;

     Orange

     is

     a 

    Food, etc. Applying class inheritance, the food item classes can all be 

    derived from the base class Food. In the Appendix A, Figure 4 shows the 

    class hierarchy diagram of  the different types of  foods. The listbox is 

    populated with references to objects of  type Food , the base class for all 

    the classes of  food items sold at  Al’s Friendly  Market . 

      In the diagram, base class Food  exposes the public property 

    Name, whose values are displayed in the listbox. Figure 2 shows 

    the listbox’s DisplayMember  property being set to the value 

    Name  – the public property of  class Food. 

     

    The 

    Implementation 

    of  

    the 

    Grocery  

    List  

     Application. 

    Appendix 

    includes 

    the 

    code for: 

    o  Class ObjectInListForm  – The Presentation Tier o  Food Class Derivation  – The Business Tier 

  • 8/16/2019 C Sharp-listbox Kullanimi

    10/26

      Page 10 

    Conclusions

      This paper described a technique for displaying references to objects in a listbox. 

    The application used the listbox’s DisplayMember  property to display (in the 

    Items collection property of  the listbox) values associated with one of  the 

    object’s public properties. o  When using the property DisplayMember  there is no need to add extra 

    code to the class whose objects are displayed in the listbox. 

    o  Overriding the default implementation of  the class’ ToString() method is a coding alternative for controlling which object values should be 

    displayed in the listbox. 

      Out of  the three design alternatives presented in this paper, the implementation 

    of  the application was based on a two‐tier model.  There are important code 

    maintenance benefits associated with the model: 

    The model separates the functional responsibilities of  the presentation 

    tier (the

     GUI

     form)

     from

     the

     business

     tier

     (the

     derivation

     involving

     the

     

    food item classes). 

     

    The presentation tier is mainly responsible for presenting data on 

    the GUI, in response to the user interactions with the controls 

    displayed on the window form. The code in the window form class 

    is disassociated from the business knowledge (i.e., the rules 

    applicable to the processing of  the data). Access to the data and 

    operations applicable to the data is accomplished through the 

    invocation of  the public methods and properties of  the objects. 

     

    The business tier is responsible for the processing of  the data. The 

    business knowledge

     is

     encapsulated

     is

     a set

     of 

     classes

      –

     in

     the

     

    code example, the food class derivation encapsulates the business 

    knowledge. 

      Changes in either one of  the two tiers has no (or minimal) impact 

    on the other tier. 

    o  Displaying references to objects in the listbox yields to the implementation of  a truly object‐oriented system.  In the window form class, the code 

    improvements become evident by inspecting some of  the methods that 

    deal with the items stored in the listbox  – see the code in Appendix A. For 

    example: 

     

    The 

    event 

    handler 

    method 

    SelectedIndexChanged() 

     – 

    which 

    is 

    responsible for displaying the details of  the selected food item  – 

    simply obtains a reference to an object of  type Food and uses its 

    public properties to populate with values the GUI controls. The 

    code in unaware of  the actual type of  object for which it displays 

    the details  – the object type could be Water, or Orange, or any 

    other type from the food class derivation; regardless of  the type, 

    the displayed values always match those ones presented in Table 1. 

  • 8/16/2019 C Sharp-listbox Kullanimi

    11/26

      Page 11 

      The event handler method associated with the Add to Groceries 

    List button (method addButton_Click())  – which is responsible for 

    incrementing both the total cost for the purchased item and the 

    total cost of  the overall purchase  – also obtains a reference to an 

    object of  type Food. The code is linear  – devoid of  selection 

    statements  –

     and

     simple;

     method

     addButton_Click()

     uses

     the

     public

     

    methods of  the object to perform the calculations. The code in the 

    window form class is disassociated from the business rules that 

    calculate the cost of  a given food item. 

  • 8/16/2019 C Sharp-listbox Kullanimi

    12/26

      Page 12 

    Appendix A  – The Grocery List Application 

    Figure 1: ObjectInListForm window form  – The GUI class 

    Figure 2: At design time, the listbox DisplayMember  property being set to the class 

    Food’s public property Name. 

    caloriesTextBox

     priceTextBox

    costTextBox

    quantityTextBoxaddButton

    deleteButton

    clearButton

    totalTextBox

    closeButton

    typeTextBox

    unitTextBox

  • 8/16/2019 C Sharp-listbox Kullanimi

    13/26

      Page 13 

    Figure 3: The Error Provider Component added to the form. 

  • 8/16/2019 C Sharp-listbox Kullanimi

    14/26

      Page 14 

    Figure 4: Inheritance Diagram for the Food Item Classes  – Food is the base class. 

    (Due to

     the

     lack

     of 

     space

     on

     the

     top

     layer,

     the

     inheritance

     diagram

     is

     continued

     on

     the

     second half  of  Figure 4.) 

  • 8/16/2019 C Sharp-listbox Kullanimi

    15/26

      Page 15 

    Class ObjectInListForm  – Presentation Tier 

    / ** Cl ass Obj ecI nLi st For m: Code f or t he GUI*/

    usi ng  Syst em;usi ng  Syst em. Wi ndows. For ms;

    namespace  Li st Wi t hObj ect s{

    publ i c  par t i al   c l ass  Obj ectI nLi st For m : Form  {

    doubl e  t otal Pur chase = 0;

    publ i c  Obj ect I nLi st For m( ){

    I ni t i al i zeComponent ( ) ;}

    pr i vat e  voi d  cl oseBut t on_Cl i ck( obj ect   sender, Event Ar gs  e){

    Cl ose( ) ;}

    pr i vat e  voi d  Obj ect I nLi st For m_Load( obj ect   sender, Event Ar gs  e){

    / / Popul at e t he gr ocer i es l i stI ni t i al i zeLi stBox( ) ;

    }

    / / Di spl ay t he det ai l s of t he gr ocer y- i t empr i vat e  voi d  gr ocer i esLi st Box_Sel ect edI ndexChanged(

    obj ect   sender , Event Ar gs  e){

    er r or Pr ovi der 1. Cl ear ( ) ;Di spl ay( ( Food) gr ocer i esLi st Box. I t ems[

    gr ocer i esLi st Box. Sel ect edI ndex] ) ;}

    pr i vat e  voi d  addBut t on_Cl i ck( obj ect   sender , Event Ar gs  e){

    er r or Pr ovi der 1. Cl ear ( ) ;t ry{

    doubl e  quant i t y =Convert . ToDoubl e( quant i t yTextBox. Text) ;

    Food  i t em =( Food) gr ocer i esLi st Box. I t ems[gr ocer i esLi st Box. Sel ect edI ndex] ;

    i t em. Set Cost ( quant i t y) ;cost Text Box. Text = i t em. Cost . ToSt r i ng( "C") ;quant i t yText Box. Cl ear ( ) ;t otal Pur chase += i t em. Get Pur chaseCost ( quant i t y) ;t ot al TextBox. Text = t otal Pur chase. ToSt r i ng( "C") ;

    }cat ch  ( FormatExcept i on)

  • 8/16/2019 C Sharp-listbox Kullanimi

    16/26

      Page 16 

    {/ / Handl e t he except i on associ ated wi t h t he/ / quant i t y f i el der r or Pr ovi der 1. Set Er r or ( quant i t yText Box,

    "Ent er a number . ") ;quant i t yTextBox. Focus( ) ;quant i t yText Box. Sel ectAl l ( ) ;

    }cat ch  ( Ar gumentOut Of RangeExcept i on){

    / / Handl e t he except i on associ ated wi t h t he/ / unsel ected gr ocer y l i st- i t em

      er r or Pr ovi der 1. Set Er r or ( gr ocer i esLi st Box,"Sel ect a gr ocer y i t em. ") ;

    }}

    pr i vat e  voi d  del et eBut t on_Cl i ck( obj ect   sender , Event Ar gs  e){

    er r or Pr ovi der 1. Cl ear ( ) ;

    t ry{

    Food  i t em = ( Food) gr ocer i esLi st Box. I t ems[gr ocer i esLi st Box. Sel ect edI ndex] ;

    t ot al Pur chase - = i t em. Cost ;i t em. Reset Cost ( ) ;cost Text Box. Cl ear ( ) ;quant i t yText Box. Cl ear ( ) ;t ot al Text Box. Cl ear ( ) ;i f   ( t ot al Pur chase != 0)

    t ot al TextBox. Text = t ot al Pur chase. ToSt r i ng( "C") ;}cat ch  ( Ar gumentOut Of RangeExcept i on){

    / / Handl e t he except i on associ ated wi t h t he/ / unsel ected gr ocer y l i st- i t em

      er r or Pr ovi der 1. Set Er r or ( gr ocer i esLi st Box,"Sel ect a gr ocer y i t em. ") ;

    }}

    pr i vat e  voi d  cl ear But t on_Cl i ck( obj ect   sender, Event Ar gs  e){

    Cl ear Det ai l s() ;I ni t i al i zeLi stBox( ) ;

    }

    pr i vat e  voi d  I ni t i al i zeLi s tBox( ){

    / / Reset t he l i s tgr ocer i esLi st Box. I t ems. Cl ear ( ) ;/ / Add obj ect r ef er ences f or t he f r ui t sgr ocer i esLi st Box. I t ems. AddRange(new  Food[ ]

    { new  Banana( ) , new  Tomat o( ) , new  Or ange( ) }) ;/ / Add obj ect r ef er ences f or t he dai r y pr oduct sgr ocer i esLi st Box. I t ems. AddRange(new  Food[ ]

    { new  Mi l k( ) , new  Cheese( ) } ) ;

  • 8/16/2019 C Sharp-listbox Kullanimi

    17/26

      Page 17 

    / / Add obj ect r ef er ences f or t he pr ot ei nsgr ocer i esLi st Box. I t ems. AddRange(new  Food[ ]

    { new  Egg( ) , new  Roast Beef ( ) , new  Sal mon( ) }) ;/ / Add obj ect r ef er ences f or t he veget abl esgr ocer i esLi st Box. I t ems. AddRange(new  Food[ ]

    { new  Car r ot( ) , new  Let t uce( ) }) ;/ / Add obj ect r ef er ences f or t he bever agesgr ocer i esLi st Box. I t ems. AddRange(new  Food[ ]

    { new  NewCol a( ) , new  Wat er ( ) }) ;/ / r eset t he t ot al pur chase var i abl et otal Pur chase = 0;

    }

    / / Di spl ay t he obj ect det ai l spr i vat e  voi d  Di spl ay(Food  i t em){

    cal or i esText Box. Text = i t em. Cal or i es. ToSt r i ng( ) ;cost TextBox. Cl ear ( ) ;t ypeText Box. Text = i t em. Type;uni t TextBox. Text = i t em. Uni t s;

    pr i ceTextBox. Text = i t em. Pri ce. ToSt r i ng( "C" ) ;i f   ( i t em. Cost ! = 0)

    cost Text Box. Text = i t em. Cost . ToSt r i ng( "C") ;}

    pr i vat e  voi d  Cl ear Det ai l s(){

    cal or i esText Box. Cl ear ( ) ;cost TextBox. Cl ear ( ) ;pr i ceText Box. Cl ear ( ) ;quant i t yText Box. Cl ear ( ) ;t ot al Text Box. Cl ear ( ) ;t ypeTextBox. Cl ear ( ) ;uni t Text Box. Cl ear ( ) ;

    }}

    }

  • 8/16/2019 C Sharp-listbox Kullanimi

    18/26

      Page 18 

    Food Class Derivation  – Business Tier 

    / ** Cl ass Food: Base cl ass f or t he di f f er ent ki nds of f oods.*/

    usi ng  Syst em;

    namespace  Li st Wi t hObj ect s{

    cl ass  Food{

    publ i c  Food( st r i ng  name, st r i ng  t ype, doubl e  cal or i es ,doubl e  pr i ce, s t r i ng  uni t s)

    {Name = name;

     Type = t ype;Cal or i es = cal or i es;Pr i ce = pr i ce;Uni t s = uni t s;

    }

    publ i c  doubl e  Cal or i es { get ; pr i vat e  set ; }publ i c  doubl e  Cost { get ; pr i vat e  set ; }publ i c  st r i ng  Name { get ; pr i vat e  set ; }publ i c  doubl e  Pr i ce { get ; pr i vat e  set ; }publ i c  st r i ng  Type { get ; pr i vat e  set ; }publ i c  st r i ng  Uni t s { get ; pr i vat e  set ; }

    publ i c  voi d  SetCost ( doubl e  quant i t y){

    Cost += Pri ce * quant i t y;}

    publ i c  doubl e  GetPur chaseCost ( doubl e  quant i t y){

    return  quant i t y * Pr i ce;}

    publ i c  voi d  Reset Cost ( ){

    Cost = 0;}

    }}

  • 8/16/2019 C Sharp-listbox Kullanimi

    19/26

      Page 19 

    / ** Cl ass Bever age: Base cl ass f or t he di f f er ent ki nds of* bever ages. Cl ass Bever age i s der i ved f r om cl ass Food.*/

    usi ng  Syst em;

    namespace  Li st Wi t hObj ect s{

    cl ass  Beverage  : Food{

    pr i vat e  const   s t r i ng  TYPE = "Bever age";publ i c  Bever age( st r i ng  name, doubl e  cal or i es , doubl e  pr i ce,

    s t r i ng  uni t s): base( name, TYPE, cal or i es, pr i ce, uni t s)

    {}

    }} 

    / ** Cl ass Dai r y: Base cl ass f or t he di f f er ent ki nds of dai r y f oods.* Cl ass Dai r y i s der i ved f r om cl ass Food.*/

    usi ng  Syst em;

    namespace  Li st Wi t hObj ect s{

    cl ass  Dai r y  : Food{

    pr i vat e  const   s t r i ng  TYPE = "Dai r y";publ i c  Dai r y(st r i ng  name, doubl e  cal or i es , doubl e  pr i ce,

    st r i ng  uni t s): base( name, TYPE, cal or i es, pr i ce, uni t s)

    {}

    }}

    / ** Cl ass Fi sh: Base cl ass f or t he di f f er ent ki nds of* f i sh. Cl ass Fi sh i s der i ved f r om cl ass Food.*/

    usi ng  Syst em;

    namespace  Li st Wi t hObj ect s{

    cl ass  Fi sh  : Food{

    pr i vat e  const   s t r i ng  TYPE = "Fi sh" ;pr i vat e  const   s t r i ng  UNI TS = "Pound";publ i c  Fi sh( st r i ng  name, doubl e  cal or i es , doubl e  pr i ce)

    : base( name, TYPE, cal or i es, pr i ce, UNI TS){}

  • 8/16/2019 C Sharp-listbox Kullanimi

    20/26

      Page 20 

    }}

    / ** Cl ass Fr ui t : Base cl ass f or t he di f f er ent ki nds of* f r ui t s. Cl ass Fr ui t i s der i ved f r om cl ass Food.*/

    usi ng  Syst em;

    namespace  Li st Wi t hObj ect s{

    cl ass  Frui t   : Food{

    pr i vat e  const   s t r i ng  TYPE = "Frui t " ;pr i vat e  const   s t r i ng  UNI TS = "Pound";publ i c  Frui t ( st r i ng  name, doubl e  cal or i es , doubl e  pr i ce)

    : base( name, TYPE, cal or i es, pr i ce, UNI TS){}

    }}

    / ** Cl ass Pr ot ei n: Base cl ass f or t he di f f er ent ki nds of* pr otei n- based f oods.* Cl ass Pr ot ei n i s der i ved f r om cl ass Food.*/

    usi ng  Syst em;

    namespace  Li st Wi t hObj ect s{

    cl ass  Pr ot ei n  : Food{

    pr i vat e  const   s t r i ng  TYPE = "Protei n";publ i c  Protei n( st r i ng  name, doubl e  cal or i es , doubl e  pr i ce,

    st r i ng  uni t s): base( name, TYPE, cal or i es, pr i ce, uni t s)

    {}

    }}

  • 8/16/2019 C Sharp-listbox Kullanimi

    21/26

      Page 21 

    / ** Cl ass Veget abl e: Base cl ass f or t he di f f er ent ki nds of* veget abl e f oods. Cl ass Veget abl e i s der i ved f r om cl ass Food.*/

    usi ng  Syst em;

    namespace  Li st Wi t hObj ect s{

    cl ass  Vegetabl e  : Food{

    pr i vat e  const   s t r i ng  TYPE = "Vegetabl e";publ i c  Vegetabl e( st r i ng  name, doubl e  cal or i es , doubl e  pr i ce,

    s t r i ng  uni t s): base( name, TYPE, cal or i es, pr i ce, uni t s)

    {}

    }}

    / ** Cl ass Banana: Repr esent s a speci f i c f r ui t .* Cl ass Banana i s der i ved f r om cl ass Frui t .*/

    usi ng  Syst em;

    namespace  Li st Wi t hObj ect s{

    cl ass  Banana  : Frui t{

    pr i vat e  const   s t r i ng  NAME = "Banana" ;pr i vat e  const   doubl e  CALORI ES = 121;pr i vat e  const   doubl e  PRI CE = 0. 5;publ i c  Banana( )

    : base( NAME, CALORI ES, PRI CE){}

    }}

  • 8/16/2019 C Sharp-listbox Kullanimi

    22/26

      Page 22 

    / ** Cl ass Car r ot: Repr esent s a speci f i c veget abl e.* Cl ass Car r ot i s der i ved f r om cl ass Veget abl e.*/

    usi ng  Syst em;

    namespace  Li st Wi t hObj ect s{

    cl ass  Car r ot  : Veget abl e{

    pr i vat e  const   s t r i ng  NAME = "Carrot" ;pr i vat e  const   doubl e  CALORI ES = 27;pr i vat e  const   doubl e  PRI CE = 0. 98;pr i vat e  const   s t r i ng  UNI TS = "Pound";

    publ i c  Car r ot ( ): base( NAME, CALORI ES, PRI CE, UNI TS)

    {}

    }}

    / ** Cl ass Cheese: Repr esent s a speci f i c dai r y f ood.* Cl ass Cheese i s der i ved f r om cl ass Dai r y.*/

    usi ng  Syst em;

    namespace  Li st Wi t hObj ect s{

    cl ass  Cheese  : Dai r y{

    pr i vat e  const   s t r i ng  NAME = "Cheese" ;pr i vat e  const   s t r i ng  UNI TS = "8- Ounce" ;pr i vat e  const   doubl e  CALORI ES = 200;pr i vat e  const   doubl e  PRI CE = 4. 49;

    publ i c  Cheese( ): base( NAME, CALORI ES, PRI CE, UNI TS)

    {}

    }}

  • 8/16/2019 C Sharp-listbox Kullanimi

    23/26

      Page 23 

    / ** Cl ass Egg: Repr esent s a speci f i c pr ot ei n f ood.* Cl ass Egg i s der i ved f r om cl ass Pr ot ei n.*/

    usi ng  Syst em;

    namespace  Li st Wi t hObj ect s{

    cl ass  Egg  : Pr ot ei n{

    pr i vat e  const   s t r i ng  NAME = "Egg";pr i vat e  const   s t r i ng  UNI TS = "Dozen" ;pr i vat e  const   doubl e  CALORI ES = 54;pr i vat e  const   doubl e  PRI CE = 1. 58;

    publ i c  Egg( ): base( NAME, CALORI ES, PRI CE, UNI TS)

    {}

    }}

    / ** Cl ass Let t uce: Repr esent s a speci f i c veget abl e f ood.* Cl ass Let t uce i s der i ved f r om cl ass Veget abl e.*/

    usi ng  Syst em;

    namespace  Li st Wi t hObj ect s{

    cl ass  Let t uce  : Vegetabl e{

    pr i vat e  const   s t r i ng  NAME = "Let t uce" ;pr i vat e  const   doubl e  CALORI ES = 4;pr i vat e  const   doubl e  PRI CE = 0. 75;pr i vat e  const   s t r i ng  UNI TS = "Pound";

    publ i c  Lettuce(): base( NAME, CALORI ES, PRI CE, UNI TS)

    {}

    }}

  • 8/16/2019 C Sharp-listbox Kullanimi

    24/26

      Page 24 

    / ** Cl ass Mi l k: Repr esent s a speci f i c dai r y f ood.* Cl ass Mi l k i s der i ved f r om cl ass Dai r y.*/

    usi ng  Syst em;

    namespace  Li st Wi t hObj ect s{

    cl ass  Mi l k  : Dai r y{

    pr i vat e  const   s t r i ng  NAME = "Mi l k" ;pr i vat e  const   s t r i ng  UNI TS = "Gal l on" ;pr i vat e  const   doubl e  CALORI ES = 90;pr i vat e  const   doubl e  PRI CE = 2. 75;

    publ i c  Mi l k( ): base( NAME, CALORI ES, PRI CE, UNI TS)

    {}

    }}

    / ** Cl ass NewCol a: Repr esent s a speci f i c beverage.* Cl ass NewCol a i s der i ved f r omcl ass Bever age.*/

    usi ng  Syst em;

    namespace  Li st Wi t hObj ect s{

    cl ass  NewCol a  : Beverage{

    pr i vat e  const   s t r i ng  NAME = "New Col a" ;pr i vat e  const   doubl e  CALORI ES = 120;pr i vat e  const   doubl e  PRI CE = 1. 25;pr i vat e  const   s t r i ng  UNI TS = " 8 f l . oz. " ;

    publ i c  NewCol a( ): base( NAME, CALORI ES, PRI CE, UNI TS)

    {}

    }}

  • 8/16/2019 C Sharp-listbox Kullanimi

    25/26

      Page 25 

    / ** Cl ass Or ange: Repr esent s a speci f i c f r ui t .* Cl ass Or ange i s der i ved f r om cl ass Fr ui t .*/

    usi ng  Syst em;

    namespace  Li st Wi t hObj ect s{

    cl ass  Or ange  : Frui t{

    pr i vat e  const   s t r i ng  NAME = "Or ange";pr i vat e  const   doubl e  CALORI ES = 85;pr i vat e  const   doubl e  PRI CE = 0. 8;publ i c  Or ange( )

    : base( NAME, CALORI ES, PRI CE){}

    }}

    / ** Cl ass Roast Beef : Repr esent s a speci f i c pr ot ei n f ood.* Cl ass Roast Beef i s der i ved f r om cl ass Pr ot ei n.*/

    usi ng  Syst em;

    namespace  Li st Wi t hObj ect s{

    cl ass  Roast Beef   : Pr ot ei n{

    pr i vat e  const   s t r i ng  NAME = "Roast Beef " ;pr i vat e  const   s t r i ng  UNI TS = "Pound";pr i vat e  const   doubl e  CALORI ES = 210;pr i vat e  const   doubl e  PRI CE = 10. 89;

    publ i c  Roast Beef ( ): base( NAME, CALORI ES, PRI CE, UNI TS)

    {}

    }}

  • 8/16/2019 C Sharp-listbox Kullanimi

    26/26

    / ** Cl ass Sal mon: Repr esent s a speci f i c f i sh.* Cl ass Sal mon i s der i ved f r om cl ass Fi sh.*/

    usi ng  Syst em;

    namespace  Li st Wi t hObj ect s{

    cl ass  Sal mon  : Fi sh{

    pr i vat e  const   s t r i ng  NAME = "Sal mon";pr i vat e  const   doubl e  CALORI ES = 233;pr i vat e  const   doubl e  PRI CE = 15. 19;publ i c  Sal mon( ) : base( NAME, CALORI ES, PRI CE){}

    }}

    / ** Cl ass Tomat o: Repr esent s a speci f i c f r ui t .* Cl ass Tomat o i s der i ved f r om cl ass Frui t .*/

    usi ng  Syst em;

    namespace  Li st Wi t hObj ect s{

    cl ass  Tomat o  : Frui t{

    pr i vat e  const   s t r i ng  NAME = "Tomat o" ;pr i vat e  const   doubl e  CALORI ES = 27;pr i vat e  const   doubl e  PRI CE = 2. 7;publ i c  Tomato( )

    : base( NAME, CALORI ES, PRI CE){}

    }}