Pages

Tuesday, November 20, 2007

Day 5: Arrays

The website of the day is here.

And here is the example code...


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace Arrays
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            /*
             * Arrays Demo
             */
            richTextBox1.AppendText("FIXED ARRAYS"+Environment.NewLine);
            //a fixed array of 4 strings (0-3), initialized on create.
            String[] myfamily = new String[]
               {"Bryan", "Ruth", "Rebekah", "Rachel"};
            
            //a fixed array initialized later
            String[] myPets = new String[3];
            myPets[0] = "Sasha";
            myPets[1] = "Ross";
            myPets[2] = "Nightstar";
            
            //reading fixed arrays
            foreach (string s in myfamily) 
            {
                richTextBox1.AppendText(s + Environment.NewLine);
            }
            foreach (string s in myPets)
            {
                richTextBox1.AppendText(s + Environment.NewLine);
            }

            Random R = new Random();
            

            int P = (int)R.Next(3);
            int F = (int)R.Next(4);

            string playtime = 
              string.Format("{0} is playing with {1}", myPets[P], myfamily[F]);
            richTextBox1.AppendText(playtime + Environment.NewLine);

            //variable Arrays
            richTextBox1.AppendText(Environment.NewLine + 
              "VARIABLE ARRAYS" + 
              Environment.NewLine);
            //untyped array
            ArrayList RandomStuff = new ArrayList();
            //you can put anything into this array            
            RandomStuff.Add(new Font("Arial",12));
            RandomStuff.Add(7.1);
            RandomStuff.Add("The Castle Anthrax");

            //now... how can we work with this collection of trash?

            foreach (object O in RandomStuff)
            {
                richTextBox1.AppendText("I see a(n) " + 
                  O.GetType()+Environment.NewLine);
                if (O is System.Drawing.Font)
                    richTextBox1.AppendText("   ->" + 
                      (Font).FontFamily+Environment.NewLine);
                if (O is Double)
                    richTextBox1.AppendText("   ->" + O.ToString() +  
                      Environment.NewLine);
                if (O is String)
                    richTextBox1.AppendText("   ->" + O.ToString() + 
                      Environment.NewLine);                    
            }

            //typed vararrays
            richTextBox1.AppendText(Environment.NewLine + 
             "TYPED VARIABLE ARRAYS" + 
             Environment.NewLine);
            List<String> myStrings = new List<String>();
            List<Double> myNumbers = new List<Double>();
            List<Button> myButtons = new List<Button>();


            myStrings.Add("Bread");
            myStrings.Add("Mayo"); 
            myStrings.Add("Turkey");
            myStrings.Add("Cheese.Provolone");
            myStrings.Add("Bread");

            foreach (string ingredient in myStrings)
            {
                richTextBox1.AppendText(ingredient + " ");
            }
            richTextBox1.AppendText(Environment.NewLine);

            /*
             * Since these arrays are stongly typed, you can't put a 
             * string into the button array nor a number into the 
             * string array.  you *can* however put the descendant
             * of a any object into a typed array.
             */
        }
    }
}



Todays Assignment: make a list of your favorite things.

No comments:

Share This!

Contact Us

Name

Email *

Message *