Monday, April 23, 2007

Delegates, Reflections, INVOKE

Delegates:
-Similar to pointers in C++.
-Has something to do with Callback functions.
-Generic delegate copy is created at the run-time.

Reflections:
-Enables us to query the namespaces, methods...

foreach statement:
-Quite useful when there is an array of strings, or an array of ints, or an array of objects to talk about. Simply, an array of something.

Below program section makes use of the Reflections and foreach statement and used to query the methods in a .dll file. The methods are added to the listbox in the Windows form.

Notice the return types of below methods: They are arrays.
Type[] Assembly.GetTypes();
MethodInfo[] Type.GetMethods();

private void Discover_button_Click_1(object sender, EventArgs e)
{
System.Reflection.Assembly cx = System.Reflection.Assembly.LoadFile(textBox1.Text);

foreach (Type t in cx.GetTypes())
{

foreach( System.Reflection.MethodInfo mi in t.GetMethods())
{
listBox1.Items.Add(mi.Name);
}

}

}

Use the code below to easily select the .dll file you need. Don't forget to add a OpenFileDialog and an Open File button to your design.

private void OpenFile_button_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
string filename = openFileDialog1.FileName;
textBox1.Text = filename;
}

BONUS QUESTION:
-We have queried the methods in our CalculateInterest Class Library. The query produced two methods: CalculateSimpleInterest() & CalculateCompoundInterest().
-Now, how can we actually run the CalculateSimpleInterest() method and learn the interest for a given principal, rate and time?

We have to INVOKE this function to achieve the above purpose.
Research Activator.Getobject() method!

Thursday, April 19, 2007

UML: Unified-Modeling Language

Defined by OMG (Object Management Group).
Set of graphical components that model the objects and the relationships between them.

Let's consider the case of construction business.
Architects design the buildings and builders use these designs to realize what is described in the blueprints. Blueprints are a common language between architects and the builders.

Similarly, UML standard is a common language between all software developers. Apart from the implementations, all members of a team can see the classes and objects used in the design, which is especially useful in situations where the design is quite complex.

UML has nine modelling diagrams. The first three are below:

  • Use case diagrams
  • Describes what a specific model does. (Not how it does that!)
  • Class diagrams
  • Displays all of the classes in a system and the inter-relationships between classes. Name, Attributes and Operations of each class are displayed. (Does not give any idea what happens when the classes interact!)
  • Object diagrams
  • Displays the objects being used, instead of classes. (An object is an instance of a class) Like a university has multiple departments, we can have multiple objects of the same class. Object diagrams display all of the objects in the system.

  • Source: http://dn.codegear.com/article/31863

    Wednesday, April 18, 2007

    4. Finding the Weekday of a Given Date



    Is April 18, 2007 a Wednesday?
    This program asks the user to enter a random date, like 4/18/2007.
    Then it will tell you the weekday, like Wednesday.



    I am encouraging the readers of this title to refer to this link first. It is the source for my implementation. It is a more general approach rather than a simple calculation.

    Program.cs

    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace DayFinder
    {
    class Program
    {
    static void Main(string[] args)
    {
    Date D = new Date(); // Don't need my coding to define the constructor.
    Console.Write("Enter month: ");
    D.month = uint.Parse(Console.ReadLine());
    Console.Write("Enter day: ");
    D.day = uint.Parse(Console.ReadLine());
    Console.Write("Enter year (yyyy): ");
    D.year = uint.Parse(Console.ReadLine());

    Console.WriteLine("You have entered: "+D.month+"/"+D.day+"/"+D.year);

    //Assuming the entries are legal:
    /*********************************************
    For July 13, 1989:
    1. Divide the last two digits of the year by 12. 89 divided by 12 is 7, with 5 left over.

    2. Find how many 4's go into the remainder evenly. 5 divided by 4 is 1, with a remainder that we ignore.
    3. Add these three numbers. 7 + 5 + 1 = 13.
    4. Divide the sum by 7 and take the remainder. 13 divided by 7 is 1, with a remainder of 6.
    5. Add the remainder to the century's anchor day to find the year's Doomsday. Wendesday + 6 = Tuesday.
    6. Use the month's Doomsday to find the day you need. July's Doomsday is 7/11, a Tuesday, so July 13 is a Thursday.
    Source: http://www.theworldofstuff.com/other/day.html
    *********************************************************/

    uint yearTwoDigits = D.year % 100;
    uint ydivision = yearTwoDigits / 12;
    uint yremainder = yearTwoDigits % 12;
    uint NumberOfFours = yremainder / 4;
    uint FinalRemainder = (ydivision + yremainder + NumberOfFours) % 7;
    uint AnchorDay = UtilityFunctions.GetAnchorDay(((uint) (D.year/100) * 100) % 400);

    uint YearsDoomsDay = (FinalRemainder + AnchorDay) % 7;
    int difference = (int) D.day - (int) UtilityFunctions.GetMonthsDoomsDay(D.month, D.year);
    int ExactDay = (int) YearsDoomsDay + difference; //Need typecasting
    if (ExactDay <0) face="courier new">ExactDay += 7;
    else if (ExactDay > 7)
    ExactDay=ExactDay % 7;

    string WeekDay=UtilityFunctions.GetExactDay(ExactDay);

    Console.WriteLine("This date is a "+WeekDay);
    Console.Read();
    // Logic Errors: 2/29/2007 => Thursday. There is no such 2/29 in 2007!
    // Day undefined for 7/3/1989!
    }
    }
    }


    UtilityFunc.cs

    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace DayFinder
    {
    class Date
    {
    public uint month; //public is needed!
    public uint day;
    public uint year;
    }


    class UtilityFunctions
    {

    public static uint GetAnchorDay(uint AnchorCheck)
    {
    uint AnchorDay= 0;
    switch (AnchorCheck)
    {
    case 100:
    AnchorDay = 0; //"Sunday"
    break;
    case 200:
    AnchorDay = 5; //"Friday"
    break;
    case 300:
    AnchorDay = 3; //"Wednesday"
    break;
    case 0:
    AnchorDay = 2; //"Tuesday"
    break;
    }
    return AnchorDay;
    }
    public static int GetMonthsDoomsDay(uint Month, uint Year)
    {
    int DoomsDay = 0;
    switch (Month)
    {
    case 1: //January
    DoomsDay = 3 + (int)isLeap(Year);
    break;
    case 2: //February
    DoomsDay = 28;
    break;
    case 3: //...
    DoomsDay = 0;
    break;
    case 4:
    DoomsDay = 4;
    break;
    case 5:
    DoomsDay = 9;
    break;
    case 6:
    DoomsDay = 6;
    break;
    case 7:
    DoomsDay = 11;
    break;
    case 8:
    DoomsDay = 8;
    break;
    case 9:
    DoomsDay = 5;
    break;
    case 10:
    DoomsDay = 10;
    break;
    case 11:
    DoomsDay = 7;
    break;
    case 12:
    DoomsDay = 12;
    break;
    }
    return DoomsDay;
    }
    internal static uint isLeap(uint Year)
    {
    if ((Year % 4) == 0) return 1;
    else return 0;
    }
    public static string GetExactDay(int ExactDay)
    {
    string WeekDay=null;
    switch (ExactDay)
    {
    case 0:
    WeekDay = "Sunday";
    break;
    case 1:
    WeekDay = "Monday";
    break;
    case 2:
    WeekDay = "Tuesday";
    break;
    case 3:
    WeekDay = "Wednesday";
    break;
    case 4:
    WeekDay = "Thursday";
    break;
    case 5:
    WeekDay = "Friday";
    break;
    case 6:
    WeekDay = "Saturday";
    break;
    }
    return WeekDay;
    }

    }
    }

    3. Doubly-linked list

    In addition to the properties of the singly-linked list, each node in a doubly-linked list contains a link to the previous node in the chain.
    Program.cs


    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace Doubly_LinkedList
    {
    class Program
    {
    static void Main(string[] args)
    {
    char Answer = 'y';
    Node n;
    Node head = new Node();
    Node currNode;
    Node TempNode;
    currNode = head;

    while (Answer == 'y')
    {
    n = new Node();
    Console.Write("Enter Node Data: ");
    n.Data = int.Parse(Console.ReadLine());
    TempNode = currNode;
    currNode.Next = n;
    currNode = n;
    currNode.Prev = TempNode;

    Console.Write("Answer (y/n): ");
    Answer = char.Parse(Console.ReadLine());
    }

    Console.WriteLine("Printing the Node Data: (Forward)");

    TempNode = head;
    while (TempNode.Next != null)
    {

    TempNode = TempNode.Next;
    Console.WriteLine(TempNode.Data);
    }

    Console.WriteLine("Printing the Node Data: (BACKward)");

    do
    {
    Console.WriteLine(currNode.Data);
    currNode = currNode.Prev;
    } while (currNode != head);

    Console.Read();
    }
    }
    }


    Node.cs

    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace Doubly_LinkedList
    {
    class Node
    {
    public Node()
    {
    Data = 0;
    Next = null;
    Prev = null;
    }
    public int Data;
    public Node Next;
    public Node Prev;
    }
    }