1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Help with Stacks and Heaps

Discussion in 'C#' started by dannyk17, Jan 12, 2010.

  1. dannyk17

    dannyk17 New Member

    Joined:
    Jan 12, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi, I am trying to learn about stacks and heaps but everywhere I look examples of this are really basic with maybe two integers and one class in the program. I want to understand how the stack and heap works with multiple classes and objects. I have this sample program could someone explain what goes in the stack and what goes in the heap? What would the stack and heap look like by point A?
    Code:
    using System;
    using System.Collections;
    
    
    namespace stackHeap {
    
    //--- Application entry class.
    class SelectionListExample {
    
    public static void Main () {
    Console.WriteLine ("SelectionList Examples. ");
    
    SelectionList list = new SelectionList ();
    list.Add ("February.", "June.", "July.", "October.", "November.");
    
    //>>> POINT A <<<
    
    int result = list.Display ();
    
    Console.WriteLine ("You selected item {0}.", result);
    Console.WriteLine ("=== end of program. ");
    } // end Main method
    
    } // end SelectionListExample class
    
    
    //--- A class for displaying a list of options and returning the user's selection.
    public class SelectionList {
    
    private ArrayList items;
    
    public SelectionList () 
    { this.items = new ArrayList (); }
    
    //--- Add one or more list items.
    public void Add (params String[] items) {
    this.items.AddRange (items);
    } // end Add method
    
    //--- Display the list to the user, get a valid list item selection
    //--- and return the number of the item selected.
    //--- Returns 0 if list is exited with no selection.
    public int Display () {
    // Display list of items to the user.
    Console.WriteLine ();
    Console.WriteLine (" 0. No selection.");
    for (int i=1; i<=items.Count; i++) {
    Console.WriteLine (" {0}. {1}", i, (String)items[i-1]);
    }
    
    // Get a valid list selection.
    int selection = 0;
    bool ok = false;
    while (!ok) try {
    Console.Write (" Enter selection : ");
    selection = Convert.ToInt32 (Console.ReadLine());
    if (selection>=0 && selection<=items.Count) 
    ok = true; // Valid input selection made.
    else
    Console.WriteLine ("*** Invalid selection, try again. ");
    } catch (Exception)
    { Console.WriteLine ("*** Invalid selection, try again. "); }
    // End of input selection loop.
    return selection;
    } // end display method
    
    } // end Selectionlist class
    
    } // end namespace.
    If you could help me understand this I would much appreciate this
     
    Last edited by a moderator: Jan 12, 2010

Share This Page