Zebra0.com

csharp arrays


Text of video
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Season
{
    public partial class Form1 : Form
    {
        String localDirectory = System.IO.Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]);

        public Form1()
        {
            InitializeComponent();
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            DateTime date = dateTimePicker1.Value;
            findseason(date);
            //picSeason.Image = Image.FromFile(localDirectory + "/spring.png");
        }
        private void findseason(DateTime date )
        {
            //Find the season from the date;
            //The first day of spring is March 19
            //The first day of summer is June 20
            //The first day of fall is September 22
            //The first day of winter is December 21
            //We represent these as integers by month*100+day
            //Thus we have 1st day of spring is 319, summer is 620, fall is 922, winter is 1221
            int[] seasons = { 319, 620, 922, 1221, 9999 };
            String[] seasonNames = { "winter", "spring", "summer", "fall","winter" };
            //We need winter twice: the 1st is for dates before March 19, the 2nd is for dates after December 21
            int lookupDate = date.Month * 100 + date.Day; //326
            //lblSeason.Text = lookupDate.ToString();
            int seasonIndex = -1;
            do
            {
                seasonIndex++;
            } while (lookupDate >= seasons[seasonIndex]);
            String season = seasonNames[seasonIndex];
            //lblSeason.Text += " " + seasonIndex+" "+season;
            lblSeason.Text = season;
            //Show the picture
            picSeason.Image = Image.FromFile(localDirectory + "/"+season+".png");
            //Change the backcolor
            switch(season)
            {
                case "winter": this.BackColor = Color.AliceBlue; break;
                case "spring": this.BackColor = Color.PeachPuff; break;
                case "summer": this.BackColor = Color.LawnGreen; break;
                case "fall": this.BackColor = Color.Yellow; break;
            }
            //Better:
            Color[] seasonColor = { Color.AliceBlue, Color.PeachPuff, Color.LawnGreen, Color.Yellow, Color.AliceBlue };
            this.BackColor = seasonColor[seasonIndex];
        }
    }
}

End of lesson, Next lesson: Dialogs in C#