Difference between revisions of "OOP Class12"

esse quam videri
Jump to: navigation, search
(Saveing the Arraylist in the Seesion)
(Saveing the Arraylist in the Session)
Line 85: Line 85:
 
[http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class12/CheeseObject.aspx - source]
 
[http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class12/CheeseObject.aspx - source]
  
===Saveing the Arraylist in the Session===
+
===Saving the Arraylist in the Session===
 
Wouldn't it be nice if we make our own cheese and add them to the arraylist. Here is an exmaple where you can dynamically add cheeses to the arraylist. The arraylist is preserved by saving it in a session variable between page hits. The first cheese 'Cheddar' is added to the Session in the initial page hit.
 
Wouldn't it be nice if we make our own cheese and add them to the arraylist. Here is an exmaple where you can dynamically add cheeses to the arraylist. The arraylist is preserved by saving it in a session variable between page hits. The first cheese 'Cheddar' is added to the Session in the initial page hit.
  

Revision as of 15:36, 25 April 2006

Dymnically Adding Elements

Array

http://www.c-sharpcorner.com/UploadFile/Saurabh.Mishra/GenericsInC2PartI04122006074306AM/GenericsInC2PartI.aspx

Say we have an empty drop down list.

<asp:DropDownList id="ddlCheese" autopostback="True" runat="server"  />

Since we know that all Lists in the framework contain ListItems we can dymaically add a bunch of cheeses by using a for loop that creates and add ListItems.

ListItem has several constuctors

Visibility Constructor Parameters
public ListItem ( )
public ListItem ( String text )
public ListItem ( String text , String value )
public ListItem ( String text , String value , Boolean enabled )

<csharp>

       //an array of names of cheese
       string [] aryNames = new string[5];
         
       aryNames [0] = "Feta";
       aryNames [1] = "Colby";
       aryNames [2] = "Gruyere";
       aryNames [3] = "Edam";
       aryNames [4] = "Colby";
       foreach (string cheeseName in aryNames)
       {
           ListItem newItem = new ListItem(cheeseName, cheeseName);   //get a new list item 
                                                   //with i for string text and i for value
           ddlCheese.Items.Add(newItem);   
       }</csharp>


CheeseArray.aspx - source

ArrayList of Objects

<csharp>//an arraylist of cheeses

       Cheese Feta = new Cheese("Feta");
       Cheese Colby = new Cheese("Colby");
       Cheese Gruyere = new Cheese("Gruyere");
       Cheese Edam = new Cheese("Edam");
       
       
       ArrayList alCheeses = new ArrayList();
       alCheeses.Add(Feta);
       alCheeses.Add(Colby);
       alCheeses.Add(Gruyere);
       alCheeses.Add(Edam);</csharp>

and the cheese class <csharp>public class Cheese {

   public string Name;
   public  Milks MilkType;
   public string Region;
   
   public Cheese()
   {
   }
   
   public Cheese(string name)
   {
       this.Name = name;
   }
   
   public enum Milks { Cows, Goats, Mix, Vegetarian }

}</csharp>

Then you can bind the new cheese arraylist to a dropdown box.

CheeseObject.aspx - source

Saving the Arraylist in the Session

Wouldn't it be nice if we make our own cheese and add them to the arraylist. Here is an exmaple where you can dynamically add cheeses to the arraylist. The arraylist is preserved by saving it in a session variable between page hits. The first cheese 'Cheddar' is added to the Session in the initial page hit.

http://iam.colum.edu/oop/classsource/class12/CheeseObjectSession.aspx - source

Final verson of the cheese saved in a session. This one added the MIlkType Enum and Uses a more abstracted method to add the arraylist to the ListControl.

http://iam.colum.edu/oop/classsource/class12/CheeseObjectSession2.aspx - source

Observer

Observer

Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

http://www.dofactory.com/Patterns/PatternObserver.aspx

Observer is a deisgn pattern that is often used to publish (push) or subcribe (pull) changes in the stae of one object to other intersted object. It encourgaes loose coupling of objects.

The object the get the data often called the subject then notifies the observing objects of the change. The observing object will often repond to the subject.


Observer.gif

Here is a real example in c#. In this exmaple a stock (the subject) notifies and investor (the observer) of a change in price.

ObserverStocks.png


http://iam.colum.edu/oop/gbrowser.php?file=/classsource/class12/Observer_j.cs

http://iam.colum.edu/oop/classsource/class12/Observer.aspx - source