Collection Initialization

Just like the previous post. These are more of my notes as I’m trying different things. Not really any type of full fledged program. This is about collection initialization. The previous entry was how initialize objects and put them into some sort of list. So I got to reading further on how to take those objects, put the objects into a list collection, and initialize them and the list collection all at once. Again no live version of this program, no need really. You can download the source code to look at.

Default.aspx HTML Code File Contents

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CollectionInitializers.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:Label ID="resultLabel" runat="server"></asp:Label>

    </div>
    </form>
</body>
</html>

Not much to see here but a resultLabel that will be changed as the program runs.

Default.aspx Code File Contents

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CollectionInitializers
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // Examples of using a collection instead of an array.
            string result = "";

            // Here I will demonstrate examples of collection initialization.
            // Taking the concept of object initialization further into cleaner code.
            List<Car> cars = new List<Car>()
            {
                new Car { Make = "BMW", Model = "528i", Year = 2010, Color = "Black" },
                new Car { Make = "BMW", Model = "745li", Year = 2005, Color = "Black" },
                new Car { Make = "Ford", Model = "Escape", Year = 2008, Color = "White" }
            };

            /*
            // Using a typical for loop to go thru the collection.
            for (int i = 0; i < cars.Count; i++)
            {
                result += cars.ElementAt(i).FormatDetailsForDisplay();
            }
             */

            // Or use a foreach loop to loop thru the collection. Might be cleaner in some cases.
            foreach (Car car in cars)
            {
                result += car.FormatDetailsForDisplay();
            }

            resultLabel.Text = result;
        }
    }

    public class Car
    {
        // Public properties
        //
        public string Make { get; set; }
        public string Model { get; set; }
        public int Year { get; set; }
        public string Color { get; set; }

        // Constructors
        //
        public Car()
        {
            this.Make = "Undefined";
            this.Model = "Undefined";
            this.Year = 1980;
            this.Color = "Undefined";
        }

        /*
        public Car(string make, string model, int year, string color)
        {
            this.Make = make;
            this.Model = model;
            this.Year = year;
            this.Color = color;
        }
         */

        // Public methods.
        //
        public string FormatDetailsForDisplay()
        {
            return String.Format("Make: {0} - Model: {1} - Year: {2} - Color: {3}</br>",
                Make, Model, Year, Color);
        }

    }
}

Again you will see the same Car class as I’ve used before for this. You will see that I’ve initialized my list, created my objects and initialized those objects in this list. Then I’m showing a couple of different ways to iterate thru the list to get the information and display it.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s