Tuesday, March 29, 2016

Useful Links

http://visualstudioshortcuts.com Keyboard shortcuts supported in different versions of visual studio.

https://www.autohotkey.com/ AutoHotkey (AHK) is a free, open-source macro-creation and automation software for Windows that allows users to automate repetitive tasks.

Visual Studio Express Visual Studio Express editions provide free tools to develop applications for a specific platform, such as Windows Universal Platform applications, web sites, and Windows desktop applications

SQL Server Express Take advantage of the same powerful database engine in a version tailored for redistribution and embedding. SQL Server Express includes 10GB of storage per database, easy backup and restore to Microsoft Azure functionality, and compatibility with all editions of SQL Server and Microsoft Azure SQL Database so you can develop and deploy with confidence. Note: I recommend getting the version with the advanced tools. It contains both reporting and management studio.




Lesson 11

Chapter 12 
 
Concepts:  
Interface classes
Multiple interfaces 
Interface vs Abstraction 
 
 
Interfaces
Interfaces are contracts that define the minimum set of class capabilities. An interface looks like a class, but has no implementation. The only thing it contains are declarations of events, indexers, methods and/or properties. The reason interfaces only provide declarations is because they are inherited by classes, which must provide an implementation for each interface member declared.
 
 

     interface iVehicle
    {
        int Doors { get; set; }

        string Style { get; set; }

        void SetSpeed();
    }

 
The interface class above "iVehicle" outlines what a class that implements the interface MUST support. This is the contract.
 
    class Car : iVehicle
    {
        public int Doors
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
        public string Style
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
 
        public void SetSpeed()
        {
            throw new NotImplementedException();
        }
    }
 
The Car class must satisfy the requirements of the interface but is not limited to only those requirements. The Car class could implement other methods or properties not outlined in the interface. Note: When visual studio implements an interface for you it adds a NotimplementedException() exception to each code snippet it created. This is useful in preventing us from access a generated method or property before we are ready.
 
Multiple Interfaces
 
Unlike inheritance a class can implement more that one interface.
 
Lets' add an engine interface to the solution.
 
    interface iEngine
    {
        int Cylinders { get; set; }
        string FuelType { get; set; }
    }
 
We can then add this interface to the Car class by adding the iEngine interface to the car class the class must now support the engine requirements in addition to the vehicle interface.
 
    class Car : iVehicle, iEngine
    {
        public int Doors
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
        public string Style
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
        public void SetSpeed()
        {
            throw new NotImplementedException();
        }
        public int Cylinders
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
        public string FuelType
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
    }

Interfaces vs Abstraction
 
A class can only inherit from 1 abstract class at a time. A class can support (implement) many interfaces.
An abstract class can contain code as well as code requirements. An interface cannot contain any implementation
An abstract class can contain private variables and methods. An interface only contains public code. 
 
 
 
 
 

Lesson 10

Labs

Problem 1:



12.10 (Shape Hierarchy) Implement the Shape hierarchy of Fig. 11.3. Omit the Triangle and Tetrahedron classes. Each TwoDimensionalShape should contain read-only abstract property Area to calculate the area of the two-dimensional shape. Each ThreeDimensionalShape should have readonly abstract properties Area and Volume to calculate the surface area and volume, respectively, of the three-dimensional shape. Create an app that uses an array of Shape references to objects of each concrete class in the hierarchy. Display a text description of the object to which each array element refers. Also, in the loop that processes all the shapes in the array, determine whether each shape is a TwoDimensionalShape or a ThreeDimensionalShape. If a shape is a TwoDimensionalShape, display its area. If a shape is a ThreeDimensionalShape, display its area and volume.


Problem 2:

Build the following:



Tuesday, March 15, 2016

Lesson 9

Outline

Chapter 12

Abstract Class

A C# abstract class contains abstract members which define what a derived should contain. These abstract members only declare that a member of a particular type is required, it does not implement the member. Implementation of abstract members takes place within the derived class. If a subclass which derives from an abstract class and fails to implement abstract methods will fail to compile.

In an abstract class, the abstract methods and abstract procedures are described but not defined.

    abstract class Sample
    {
        public abstract int MyProperty { get; set; }
        public abstract void DoSomething();
    }

Note the addition of the keyword "abstract" in describing the class, property and method. It will be the responsibility of a derived class to implement the functionality described in the abstracted class.

    class ImplementedSample : Sample
    {
        public override int MyProperty
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
        public override void DoSomething()
        {
            throw new NotImplementedException();
        }
    }

When inheriting from an abstract class the concrete class must implement the design of the abstract class. A wizard can appear over the abstracted class name and stub out the abstraction requirements for us. Visual studio adds the NotImplementedException to remind us that the method or property code has not been defined.

An abstracted class can implement functionality as well that can be derived.

    abstract class Sample
    {
        public abstract int MyProperty { get; set; }
        public abstract void DoSomething();
        public void DoSomethingElse()
        {
            //This is where I would put code
        }
    }

The DoSomethingElse() method has code and is functional. Any derived class can call this method. The method functionality cannot be changed in the derived class.

Virtual

So far we have only looked at abstract class members. As discussed above an abstract member is not implemented in the base class and must be implemented in derived classes in order for the class to compile.
Another type of member is a virtual member. A member defined as virtual must be implemented in the base class, but may be optionally overridden in the derived class if different behavior is required.

Lets add a virtual method to the abstract class.

    abstract class Sample
    {
        public abstract int MyProperty { get; set; }
        public abstract void DoSomething();
        public void DoSomethingElse()
        {
            //This is where I would put code
        }
        public virtual void StillDoSomethingElse()
        {
            //This code can the overridden and changed in the derived class.
            //If not overridden, then this code will run when the method is called.
        }
    }

Introducing the virtual keyword allows us to change the behavior of the method downstream in a derived class.

Lets look at the implementation.

    class ImplementedSample : Sample
    {
        public override int MyProperty
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
        public override void DoSomething()
        {
            throw new NotImplementedException();
        }
        public override void StillDoSomethingElse()
        {
            //By using the override keyword on this method
            //the code here will run instead of the base abstract method code
        }
    }

By allowing us to change the behavior of the method through overriding each derived class can use its own implementation of the virtual method or default to the method as defined.

Implementing abstract classes allow us to define behavior in the derived classes and implement behavior change when necessary.

Tuesday, March 8, 2016

Lesson 8

Outline

Chapter 12

Polymorphism

Another primary concept of object-oriented programming is Polymorphism. It allows you to invoke derived class methods through a base class reference during run-time. This is handy when you need to assign a group of objects to an array and then invoke each of their methods. They won't necessarily have to be the same object type.



In a previous lesson we discussed the inheritance aspects of the diagram above. Below is the code that supports the diagram above with a few properties to illustrate polymorphic behavior.

    class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string FullName
        {
            get { return FirstName + " " + LastName; }
        }
    }
    class Staff : Person
    {
        public int PayGrade { get; set; }
    }
    class Student : Person
    {
        public string Major { get; set; }
    }
    class Teacher : Staff
    {
        public string OfficeLocation { get; set; }
    }
    class Cleaner : Staff
    {
        public int WeeklyHours { get; set; }
    }


Polymorphism allows one object to "behave" like another in its inheritance tree. So in the above hierarchy a "teacher" object could behave like a "staff" object.

Suppose we had a method that took a staff object:

     public class Utilities
     {
          public void WorkWithStaff(Staff staffMember)
          {
               PayGrade = 5;
          }
     }


We could do the following:

Staff myStaff = new Staff();
Utilities myUtil = new Utilities();

myUtil.WorkWithStaff(myStaff);

Since myStaff is of type Staff then we can pass that object in as a parameter to our method. But due to the rules of polymorphism we can do this too:

Teacher myTeacher = new Teacher();
Utilities myUtil = new Utilities();

myUtil.WorkWithStaff(myTeacher );

Because Teacher has Staff in the object inheritance tree we can do this. Note, this only works upstream so Teacher can be Staff but Staff cannot be Teacher.

Also, while in the method WorkWithStaff, all objects passed into the method will behave as a staff object. In other words, inside the method we would not be able to access any of the Teacher attributes.

Polymorphism is a big topic. We will revisit this topic in a future lesson..