Friday, June 24, 2011

Extension method (2)

In my previous article, I have created an extension method call StrToInt for String class. Now, the question is what if a string object instance is null value and it's calling the extension method? Will the program crash? The answer is NO.

Try this out:

string s = null;
int i = s.StrToInt();

Thursday, April 14, 2011

Extension method

This is a new feature that has been added to C# in C# 3.0. One of the usefulness of extension method is to shorten the codes that we used to write.

For example, we want to convert a string value to integer. Normally, we will write code like this:

int i = 0;
            if (Int32.TryParse(s, out i))
            {
                return i;
            }
            else
            {
                return 0;
            }

Instead of doing this, we may already have declared a static function which will do the conversion like this:

public class MyClass
    {
        public static int StrToInt(string s)
        {
            int i = 0;
            if (Int32.TryParse(s, out i))
            {
                return i;
            }
            else
            {
                return 0;
            }
        }
    }

So, we will write our code like this:

int i = MyClass.StrToInt("1234");

Now, with extension method, the data type conversion call will be shorten:

    public static class MyClass
    {
        public static int StrToInt(this string s)
        {
            int i = 0;
            if (Int32.TryParse(s, out i))
            {
                return i;
            }
            else
            {
                return 0;
            }
        }
    }

    public class Test11
    {
        public void MyTestMethod()
        {
           // We don't have to write like this anymore:
           //int i = MyClass.StrToInt("1234");

           // Now, we can do this:
            int i = "1234".StrToInt();
        }
    }

Wednesday, February 23, 2011

Dynamic sorting using Linq Expression

You may wonder how to avoid hard coding the sorting order in the list. Here is one of the solution using Linq Expression:

class MyClass
{
   public int age { get; set; }
   public string name { get; set; }
}

MyClass[] arr =
{
   new MyClass() {age = 10, name ="abc" },
   new MyClass() {age = 20, name ="xyz" },
   new MyClass() {age = 30, name ="cde" }
};


// create the parameter
ParameterExpression param = Expression.Parameter(typeof(MyClass), "i");

// create the property to be used base on the 
// type (ie, MyClass) in the param variable. 
// you may change the "name" to any other field.
Expression prop = Expression.Property(param, "name");

// construct the lambda expression.
Expression> lambda 
   = Expression.Lambda>(prop, param);

// compile it to a function.
Func compile = lambda.Compile();

// now, we can use the function by passing it 
// to the OrderBy() proc.
var v3 = arr.OrderBy(compile);

string s;
foreach (var item in v3)
{
   s = item.name;
}

Thursday, December 30, 2010

WCF TCP Port Sharing

Check this out. WCF allows you to share the same port among the services.

http://msdn.microsoft.com/en-us/library/ms734772.aspx
http://msdn.microsoft.com/en-us/library/ms731810.aspx

The article below has a diagram that depicts the port sharing:
http://www.codeproject.com/KB/WCF/Port_Sharing__in_WCF.aspx

Configuration:
http://blogs.msdn.com/b/drnick/archive/2006/04/14/configuring-http.aspx

Sunday, November 21, 2010

Open source Zip library

DotNetZip is an open source Zip library which is written in managed code. You may freely use it:

http://dotnetzip.codeplex.com/

Tuesday, October 26, 2010

event VS delegate

Check this out.

http://blog.monstuff.com/archives/000040.html

Tuesday, October 19, 2010

Get the field size in the entity

In the entity framework, the diagram shown in the Visual Studio contains field information of all tables. You may get the field size that stores in the diagram by using the following method:

public static int GetMaxLength(ObjectContext p,
            string tb_name,
            string fld_name)
        {
            if (p == null)
            {
                throw new ArgumentNullException();
            }

            MetadataWorkspace w = p.MetadataWorkspace;
            var items = w.GetItems(DataSpace.CSpace);

           //i.Properties["tb_name"].TypeUsage.Facets["MaxLength"].Value
            var v = items.Where(i => string.Compare(i.Name, tb_name, true) == 0);
            EntityType et = v.First();

            object obj = et.Properties[fld_name].TypeUsage.Facets["MaxLength"].Value;
            if (obj != null)
            {
                string len = obj.ToString();
                if (string.Compare(len, "max", true) == 0)
                {
                    return -1;
                }
                else
                {
                    int max_len = Int32.Parse(len);
                    return max_len;
                }
            }
            else
            {
                return 0;
            }
        }