Monday, October 7, 2013

Lamda Expressions - C#

Lamda expressions are used when needed to make a delegate code more simple. Not only in delegates, but also in anonymous methods. Or else to build an expression tree.

Lamda expression is an anonymous function, whcih contains expression and a statement.

Lamda Operator : "=>" read as goes to.
left side of the lamda operator specifies the input parameter.

Default Delegate Snippet :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LambdaExpression
{
    class Program
    {
        delegate double DelegatePointerFunc(int r);
        static DelegatePointerFunc cpointer = CalculateArea;

        static void Main(string[] args)
        {
            double area = cpointer.Invoke(20);
        }

        static double CalculateArea(int r)
        {
            return 3.14 * r * r;
        }
    }


Anonymous Method : used to get rid of the extra Calculate Area Function
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LambdaExpression
{
    class Program
    { ///Introducing Anonymous Method
      
        delegate double DelegatePointerFunc(int r);
        //static DelegatePointerFunc cpointer = CalculateArea;

        static void Main(string[] args)
        {
            //double area = cpointer.Invoke(20);
            DelegatePointerFunc cpointer = new DelegatePointerFunc(
                delegate(int r)
                    {
                        return 3.14 * r * r;
                    }
                );
            double Area = cpointer(20);
        }

       // static double CalculateArea(int r)
       //  {
       //     return 3.14 * r * r;
       // }
    }
}

Lamda Expression  Code Snippet :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LambdaExpression
{
    class Program
    { 
        delegate double DelegatePointerFunc(int r);
        //static DelegatePointerFunc cpointer = CalculateArea;

        static void Main(string[] args)
        {
            //DelegatePointerFunc cpointer = new DelegatePointerFunc(
            //    delegate(int r)
            //        {
            //            return 3.14 * r * r;
            //        }
            //    );
            //double Area = cpointer(20);

            // USING LAMBDA EXPRESSIONS 
            DelegatePointerFunc cpointer = r => 3.14 * r * r;
            double Area = cpointer(20);
        }
    }
}

...

Delegates in C# and the reason for "Type Safe"

Delagates are known as type safe function pointers. Now now dont start to worry about what the hell is "type safe". Observer the following code snippet for a while. You'll realise the familiarity of the code.

What is a delegate?
delegate is a type safe function pointer. Using delegates you can pass methods as parameters. To pass a method as a parameter, to a delegate, the signature of the method must match the signature of the delegate. This is why, delegates are called type safe function pointers.

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

namespace Deligates
{
    public delegate void DelegateFunction(string temp);
    class Program
    {
        public static void Main(string[] args)
        {
            DelegateFunction del = new DelegateFunction(functionOne);
            del("this is the delegate message");
        }

        public static void functionOne(string msg) 
        {
            Console.WriteLine(msg);
            Console.ReadLine(); // to hold the screen
        }
    }
}


To the costructor of the delegate, you pass the name of the function to, which you want to delegate teh point from.
            DelegateFunction del = new DelegateFunction(functionOne);

What/Why it is called 'type safe'?
The delegate checks for the return type in the function header. not only the parameter type which is passing in.

If the function header is like
            public static string functionOne(string msg) 
instead of
            public static void functionOne(string msg)

when delegate is defined as
            public delegate void DelegateFunction(string temp);
it will the return type of the signature and throws an error because it expects a void (non return). The signature of the delegate should match the signature of the function to which it points. If the signature of the method does not match, the compiler will throw an error. This is why the delegates are called as type safe function pointer, coz it depends on the type to act as a pointer in function.

Delegates gives the exposure of flexibility for the real time projects.


Thursday, July 18, 2013

Windows App Store Graphic Icon Sizes

Standard Designig Logo lengths for W8 Metro app developers : ( in pixels )

Tile Images and Logos

  • Logo
    • 270 X 270
    • 210 X 210
    • 150 X 150
    • 120 X 120
  • Wide Logo
    • 558 X 270
    • 434 X 210
    • 310 X 150
    • 248 X 120
  • Small Logo
    • 54 X 54
    • 42 X 42
    • 30 X 30
    • 24 X 24
  • Store Logo
    • 90 X 90
    • 70 X 70
    • 50 X 50
Badge Logo
  • 43 X 43
  • 33 X 33
  • 24 X 24
Splash Screen

  • 1116 X 540
  • 868 X 420
  • 620 X 300

Wednesday, July 17, 2013

Step by Step Guide on Normalizing a Table with Dependencies (ERD)

Suppose you are given the following business rules to form the basis for a database design. The database must enable the manager of a company dinner club to mail invitations to the club’s members, to plan the meals, to keep track of who attends the dinners, and so on.
  • Each dinner serves many members, and each member may attend many dinners.
  • A member receives many invitations, and each invitation is mailed to many members.
  • A dinner is based on a single entree, but an entree may be used as the basis for many dinners. For example, a dinner may be composed of a fish entree, rice, and corn. Or the dinner may be composed of a fish entree, a baked potato, and string beans.
  • A member may attend many dinners, and each dinner may be attended by many members.

Because the manager is not a database expert, the first attempt at creating the database uses the structure shown in the following table

Attribute Name
Sample Value
Sample Value
Sample Value
MEMBER_NUM
214
235
214
MEMBER_NAME
Alice B. VanderVoort
Gerald M. Gallega
Alice B. VanderVoort
MEMBER_ADDRESS
325 Meadow Park
123 Rose Court
325 Meadow Park
MEMBER_CITY
Murkywater
Highlight
Murkywater
MEMBER_ZIPCODE
12345
12349
12345
INVITE_NUM
8
9
10
INVITE_DATE
23-Feb-2006
12-Mar-2006
23-Feb-2006
ACCEPT_DATE
27-Feb-2006
15-Mar-2006
27-Feb-2006
DINNER_DATE
15-Mar-2006
17-Mar-3006
15-Mar-2006
DINNER_ATTENDED
Yes
Yes
No
DINNER_CODE
DI5
DI5
DI2
DINNER_DESCRIPTION
Glowing Sea Delight
Glowing Sea Delight
Ranch Superb
ENTREE_CODE
EN3
EN3
EN5
ENTREE_DESCRIPTION
Stuffed crab
Stuffed crab
Marinated steak
DESERT_CODE
DE8
DE5
DE2
DESERT_DESCRIPTION
Chocolate mousse
with raspberry sauce
Cherries Jubilee
Apple pie with honey
crust

Given that structure, write its relational schema and draw its dependency diagram. Label all transitive and/or partial dependencies. (Hint: This structure uses a composite primary key.)



The relational schema may be written as follows:

MEMBER(MEMBER_NUM, MEMBER_NAME, MEMBER_ADDRESS, MEMBER_CITY, MEMBER_ZIP_CODE, INVITE_NUM, INVITE_DATE, ACCEPT_DATE, DINNER_DATE, DINNER_ATTENDED, DINNER_CODE, ENTRÉE_CODE, ENTRÉE_DESCRIPTION, DESSERT_CODE, DESSERT_DESCRIPTION)


The Dependency Diagram for above table





Note:
DIN_CODE in the above Figure does not determine DIN_ATTEND; just because a dinner is offered does not mean that it is attended. Note also that we have shortened the prefixes – for example, MEMBER_ADDRESS has been shortened to MEM_ADDRESS -- to provide sufficient space to include all the attributes.


  Break up the dependency diagram you drew in Problem 15 to produce dependency diagrams that are in 3NF and write the relational schema. (Hint: You might have to create a few new attributes. Also, make sure that the new dependency diagrams contain attributes that meet proper design criteria; that is, make sure that there are no multivalued attributes, that the naming conventions are met, and so on.)

The Dependency Diagram for above figure as follows




As you examine Figure P5.16, note how easy it is to see the functionality of the decomposition. For example, the (composite) INVITATION and DINNER entities make it possible to track who was sent an invitation on what date (INVITE_DATE) to a dinner to be held at some specified date (DIN_DATE), what dinner (DIN_CODE) would be served on that date, who (MEM_NUM) accepted the invitation (INVITE_ACCEPT), and who actually attended (INVITE_ATTEND. The INVITE_ACCEPT attribute would be a simple Y/N, as would be the INVITE_ATTEND. To avoid nulls, the default values for INVITE_ACCEPT and INVITE_ATTEND could be set to N. Getting the number of acceptances for a given dinner by a given date would be simple, thus enabling the catering service to plan the dinner better.




The relational schemas follow:
MEMBER (MEM_NUM, MEM_NAME, MEM_ADDRESS, MEM_CITY, MEM_STATE, MEM_ZIP)

INVITATION (INVITE_NUM, INVITE_DATE, DIN_CODE, MEM_NUM, INVITE_ACCEPT, INVITE_ATTEND)

ENTRÉE (ENT_CODE, ENT_DESCRIPTION)

DINNER (DIN_CODE, DIN_DATE, DIN_DESCRIPTION, ENT_CODE, DES_CODE)

DESSERT (DES_CODE, DES_DESCRIPTION)

Naturally, to tracks costs and revenues, the manager would ask you to add appropriate attributes in DESSERT and ENTRÉE. For example, the DESSERT table might include DES_COST and DES_PRICE to enable the manager to track net returns on each dessert served. One would also expect that the manager would want to track YTD expenditures of the members and, of course, there would have to be an invoicing module for billing purposes. And what about keeping track of member balances as the members charge meals and make payments on account?

 Using the results of above diagram, draw the Crow’s Foot ERD.

The Crow’s Foot ERD is shown below




Tuesday, July 2, 2013

Functions CANNOT overload based on the Return Type!

Can a function get overload based on its return type? As follows :- 
class My {
public:
    int get(int);
    char get(int); }
NO! You can't overload methods based on return type!, As mentioned in above snippet.

Overload resolution takes into account the function signature
A function signature is made up of:
  • function name
  • cv-qualifiers
  • parameter types
And here's the quote:
1.3.11 signature
the information about a function that participates in overload resolution (13.3): its parameter-type-list (8.3.5) and, if the function is a class member, the cv-qualifiers (if any) on the function itself and the class in which the member function is declared. [...]
Options:
1) change the method name:
class My {
public:
    int getInt(int);
    char getChar(int);
};
2) out parameter:
class My {
public:
    void get(int, int&);
    void get(int, char&);
}
3) templates... overkill in this case.

Friday, June 28, 2013

EmguCV binary image ( Thresholding )

For a grayscale image this is simple: 


OpenFileDialog open = new OpenFileDialog();
    open.Filter = "Image Files (*.tif; *.dcm; *.jpg; *.jpeg; *.bmp)|*.tif; *.dcm; *.jpg; *.jpeg; *.bmp";
    int threshold_value = 50; //0-255

    if (open.ShowDialog() == DialogResult.OK)
    {
        Image<Gray,Byte> img = new Image<Gray,Byte>(open.FileName);
        pictureBox1.Image = img.ToBitmap(); //Display if you want
        img = img.ThresholdBinary(new Gray(threshold_value), new Gray(255));
       //Image<Gray,Byte> Binary_Image = img.ThresholdBinary(new Gray(threshold_value), new Gray(255)); // to get it saved in seperate variable 
        pictureBox2.Image = img .ToBitmap(); //display results in different picturebox
    }


If you want to play with things a little you can add a trackbar to your form put the minimum to 0 and maximum to 255 and use this code.


    private void trackBar1_Scroll(object sender, EventArgs e)
    {
        int trackbar = trackBar1.Value;
        label1.Text = trackbar.ToString(); //use a label to display trackbar value
        if (img != null)
        {
            using (Image<Gray,Byte> Gray = img.ThresholdBinary(new Gray(trackbar), new Gray(255)))
            {
                pictureBox2.Image = Gray.ToBitmap();
            }
        }
    }

For a colour image things are a little different:
   OpenFileDialog open = new OpenFileDialog();
    open.Filter = "Image Files (*.tif; *.dcm; *.jpg; *.jpeg; *.bmp)|*.tif; *.dcm; *.jpg; *.jpeg; *.bmp";
    int Blue_threshold = 50; //0-255
    int Green_threshold = 50; //0-255
    int Red_threshold = 50; //0-255

    if (open.ShowDialog() == DialogResult.OK)
    {
        Image<bgr,Byte> img_colour = new Image<bgr,Byte>(open.FileName);
        pictureBox1.Image = img_colour.ToBitmap(); //Display if you want
        img_colour = img_colour.ThresholdBinary(new Bgr(Blue_threshold, Green_threshold, Red_threshold), new Bgr(255, 255, 255));
        pictureBox2.Image = img_colour.ToBitmap();//display results in different picturebox
    }