15 December 2011

C# Lesson 15 : Understanding and Creating Classes

Classes are integral to the .NET Framework, particularly the .NET Framework Class Library. Learn how classes are defined and new instances are created, how to define Properties, and how to both set values and get values for a given instance of the class. 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SimpleClasses
{
    class Program
    {
        static void Main(string[] args)
        {
            Car myNewCar = new Car();

            myNewCar.Make = "Oldsmobile";
            myNewCar.Model = "Cutlas Supreme";
            myNewCar.Year = 1986;
            myNewCar.Color = "Silver";

            Console.WriteLine("{0} - {1} - {2}",
                myNewCar.Make,
                myNewCar.Model,
                myNewCar.Color);

            //double marketValueOfCar = determineMarketValue(myNewCar);

            Console.WriteLine("Car's value: {0:C}", myNewCar.DetermineMarketValue());

            Console.ReadLine();

        }

        private static double determineMarketValue(Car _car)
        {
            double carValue = 100.0;
           
            // Somedat white come to go online and look up the car's value
            // and trieve its value into the carValue variable
            return carValue;
        }

    }

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

        public double DetermineMarketValue()
        {
            double carValue = 100.0;

            if (this.Year > 1990)
                carValue = 10000.0;
            else
                carValue = 2000.0;

            return carValue;
        }

    }


}
Source : MS Virtual Academy
Post a Comment