Friday, February 25, 2011

Compare File versions using System.IO.Version class


Following is the sample code to compare versions for two file:

using System;


using System.Collections.Generic;
using System.Text;
using System.IO;
static void Main(string[] args)
{
           string strFileVersion1 = "1.0.2254.37578";
           string strFileVersion2 = "1.0.2254.36884";
            

           Version version1= new Version(strFileVersion1 );
           Version version2 = new Version(strFileVersion2);

            Console.WriteLine("File Version File 1 = " + strFileVersion1 + " File Version File 2 = " + strFileVersion2);
            

           if (version1 > version2)
                Console.WriteLine(strFileVersion1 + " is Newer then " + strFileVersion2);
           else if (version1 < version2)
                Console.WriteLine(strFileVersion1 + " is Older then " + strFileVersion2);
           else
                Console.WriteLine(strFileVersion1 + " is Equal to " + strFileVersion2);
            Console.ReadLine();
}



OUTPUT:

File Version File 1 = 1.0.2254.37578  File Version File 2 = 1.0.2254.36884
1.0.2254.37578 is Newer then 1.0.2254.36884

File Version File 1 = 1.0.2254.37578  File Version File 2 = 1.0.2254.37578
1.0.2254.37578 is Equal to 1.0.2254.37578

File Version File 1 = 1.0.2154.37578  File Version File 2 = 1.0.2254.37578
1.0.2154.37578 is Older then 1.0.2254.37578

File Version File 1 = 3.0.2154.37578  File Version File 2 = 2.0.2254.37578
3.0.2154.37578 is Newer then 2.0.2254.37578









Tuesday, February 15, 2011

Environment.SpecialFolder Enumeration


In Dot net path for these special folders can be accesses by using the following method:


Environment.GetFolderPath(Environment.SpecialFolder.Personal)
Or
Environment.GetFolderPath(Environment.SpecialFolder.Windows) 

Following are the paths for the  dot net framework versions 2.0,3.5 and 4.0  respectively:


Environment.SpecialFolder (For Dotnet 2.0)

ApplicationData
C:\Users\UserName\AppData\Roaming

CommonApplicationData
C:\ProgramData

CommonProgramFiles
C:\Program Files\Common Files

Cookies
C:\Users\UserName\AppData\Roaming\Microsoft\Windows\Cookies

Desktop
C:\Users\UserName\Desktop

DesktopDirectory
C:\Users\UserName\Desktop

Favorites
C:\Users\UserName\Favorites

History
C:\Users\UserName\AppData\Local\Microsoft\Windows\History

InternetCache
C:\Users\UserName\AppData\Local\Microsoft\Windows\Temporary Internet Files

LocalApplicationData
C:\Users\UserName\AppData\Local

MyDocuments 
C:\Users\UserName\Documents

MyMusic 
C:\Users\UserName\Music

MyPictures 
C:\Users\UserName\Pictures

Personal 
C:\Users\UserName\Documents

ProgramFiles 
C:\Program Files

Programs 
C:\Users\UserName\AppData\Roaming\Microsoft\Windows\Start Menu\Programs

Recent
C:\Users\UserName\AppData\Roaming\Microsoft\Windows\Recent

SendTo 
C:\Users\UserName\AppData\Roaming\Microsoft\Windows\SendTo

StartMenu
C:\Users\UserName\AppData\Roaming\Microsoft\Windows\Start Menu

Startup 
C:\Users\UserName\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

System 
C:\Windows\system32

Templates 
C:\Users\UserName\AppData\Roaming\Microsoft\Windows\Templates

Environment.SpecialFolder (For Dotnet 3.5)


LocalApplicationData
C:\Users\UserName\AppData\Local

Environment.SpecialFolder (For Dotnet 4.0)

Personal
C:\Users\UserName\Documents
AdminTools
C:\Users\UserName\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Administrative Tools

CDBurning  
C:\Users\UserName\AppData\Local\Microsoft\Windows\Burn\Burn

CommonAdminTools  
C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Administrative Tools

CommonDocuments
C:\Users\Public\Documents

CommonMusic  
C:\Users\Public\Music

CommonPictures  
C:\Users\Public\Pictures

CommonStartMenu  
C:\ProgramData\Microsoft\Windows\Start Menu

CommonPrograms  
C:\ProgramData\Microsoft\Windows\Start Menu\Programs

CommonStartup
C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup

CommonDesktopDirectory
C:\Users\Public\Desktop

CommonTemplates  
C:\ProgramData\Microsoft\Windows\Templates

CommonVideos  
C:\Users\Public\Videos

Fonts  
C:\Windows\Fonts

MyVideos  
C:\Users\UserName\Videos

NetworkShortcuts  
C:\Users\UserName\AppData\Roaming\Microsoft\Windows\Network Shortcuts

PrinterShortcuts  
C:\Users\UserName\AppData\Roaming\Microsoft\Windows\Printer Shortcuts

UserProfile  
C:\Users\UserName

CommonProgramFilesX86  
C:\Program Files\Common Files

ProgramFilesX86  
C:\Program Files

Resources  
C:\Windows\resources

SystemX86  
C:\Windows\system32

Windows  
C:\Windows


Note: Above given path are generated on Windows 7 32 bit version, using visual studio 2010.
UserName represents the name of user.

For further details check it out here.

Sunday, February 13, 2011

How to pass Command line arguments in C#

 To pass command line argument, create a windows form application and the Main() method should look like:

       [STAThread]
        static void Main()
        {
                string[] args = Environment.GetCommandLineArgs();
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1(args[1], int.Parse(args[2])));
        }
Here,
  Method Environment.GetCommandLineArgs() accepts arguments for this application.
  new Form1(args[1], int.Parse(args[2])) this code segment is passing the  parameters to the constructor, args[1] is for Name, args[2] is for Year of Birth and missing args[0] contains the current executing exe path.

This is the code segment for the constructor accepting parameters:
 public Form1(string Name, int YearOfBirth)
 {             MessageBox.show(string.Format(" NAME :: {0} \n Your year of Birth :: {1}",      
                 Name,YearOfBirth.ToString()));
  }
        
For testing goto command prompt and execute:
 TestArguments.exe "Ahmed" 1980
Or,

 System.Diagnostics.Process.Start(@"TestArguments.exe", "Ahmed " + " 1980" );
    




Saturday, February 12, 2011

How to set Image.Source in WPF?


I found this information to be useful for newbie, as setting up the image source from the code is not straight forward

in WPF Application. Consider following XAML
XAML::
<image Name="imgApplication" HorizontalAlignment="Center" Source="myApp.png" Margin="0,0,17,0" VerticalAlignment="Center" Stretch="None"/>

here if we want to change this "imgApplication" image with some other image from code we can use following code.


Code::
ImageSourceConverter c = new ImageSourceConverter();
imgApplication.Source = (ImageSource)c.ConvertFromString(@"C:\myNewApp.png");

Here,
@"C:\myNewApp.png" specifies the path to the new image.


By Resource Image:


In the same way if you have added the image in Resource then you can use following code:
imgApplication.Source = BitmapSourceFromImage(Properties.Resources.myNewAppPNG);

Here,
"myNewAppPNG" is the name of the image added in resource.
"BitmapSourceFromImage()" is a function to convert the image to type "ImageSource"

public BitmapSource BitmapSourceFromImage(System.Drawing.Image img)
{
      System.IO.MemoryStream memStream = new System.IO.MemoryStream();
      //save the image to memStream as a png
      img.Save(memStream, System.Drawing.Imaging.ImageFormat.Png);

      //gets a decoder from this stream
      System.Windows.Media.Imaging.PngBitmapDecoder decoder = new
              System.Windows.Media.Imaging.PngBitmapDecoder( memStream,
              System.Windows.Media.Imaging.BitmapCreateOptions.PreservePixelFormat,  
              System.Windows.Media.Imaging.BitmapCacheOption.Default);
        return decoder.Frames[0];
}

That's it.

Wednesday, December 29, 2010

What is JQuery?

History
jQuery 1.0 was released in 2006 and was originally written primarily by John Resig of the Mozilla Software Foundation. What was originally a cross-browser means of easy DOM traversal and manipulation has now grown into a mature, full-featured library resource that contains not just tools that make working with the DOM a pleasure, but many additional and robust tools that make JavaScript coding much easier.

What is jQuery ?
jQuery is a fast and concise JavaScript Library, jQuery simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development.

jQuery is a JavaScript toolkit designed to simplify various tasks by writing less code. Here is the list of important core features supported by jQuery:

* DOM manipulation: The jQuery made it easy to select DOM elements, traverse them and modifying their content by using cross-browser open source selector engine called Sizzle.
* Event handling: The jQuery offers an elegant way to capture a wide variety of events, such as a user clicking on a link, without the need to clutter the HTML code itself with event handlers.
* AJAX Support: The jQuery helps you a lot to develop a responsive and feature-rich site using AJAX technology.
* Animations: The jQuery comes with plenty of built-in animation effects which you can use in your websites.
* Lightweight: The jQuery is very lightweight library - about 19KB in size ( Minified and gzipped).
* Cross Browser Support: The jQuery has cross-browser support, and works well in IE 6.0+, FF 2.0+, Safari 3.0+, Chrome and Opera 9.0+
* Latest Technology: The jQuery supports CSS3 selectors and basic XPath syntax.

The jQuery library harnesses the power of Cascading Style Sheets (CSS) selectors to let us quickly and easily access elements or groups of elements in the Document Object Model (DOM).

A jQuery Selector is a function which makes use of expressions to find out matching elements from a DOM based on the given criteria.

The $() factory function:

All type of selectors available in jQuery, always start with the dollar sign and parentheses: $().

The factory function $() makes use of following three building blocks while selecting elements in a given document:

jQuery Description
Tag Name: Represents a tag name available in the DOM. For example $('p') selects all paragraphs in the document.

Tag ID: Represents a tag available with the given ID in the DOM. For example $('#some-id') selects the single element in the document that has an ID of some-id.

Tag Class: Represents a tag available with the given class in the DOM. For example $('.some-class') selects all elements in the document that have a class of some-class.

All the above items can be used either on their own or in combination with other selectors. All the jQuery selectors are based on the same principle except some tweaking.

NOTE: The factory function $() is a synonym of jQuery() function. So in case you are using any other JavaScript library where $ sign is conflicting with some thing else then you can replace $ sign by jQuery name and you can use function jQuery() instead of $().

Selectors
The selectors are very useful and would be required at every step while using jQuery. They get the exact element that you want from your HTML document.

Borrowing from CSS 1–3, and then adding its own, jQuery offers a powerful set of tools for matching a set of elements in a document.

If you wish to use any of the meta-characters (#;&,.+*~':"!^$[]()=>|/@ ) as a literal part of a name, you must escape the character with two backslashes: \\. For example, if you have an an input with name="names[]", you can use the selector $("input[name=names\\[\\]]").

Attributes
Some of the most basic components we can manipulate when it comes to DOM elements are the properties and attributes assigned to those elements.

Most of these attributes are available through JavaScript as DOM node properties. Some of the more common properties are:
  • className
  • tagName
  • id
  • href
  • title
  • rel
  • src
By jQuery we can get and set DOM attributes of elements, by using methods: .addClass(),.attr(),.hasClass(),.val() etc..

Traversing

jQuery is a very powerful tool which provides a variety of DOM traversal methods to help us select elements in a document randomly as well as in sequential method.

Most of the DOM Traversal Methods do not modify the jQuery object and they are used to filter out elements from a document based on given conditions.

CSS Methods
The jQuery library supports nearly all of the selectors included in Cascading Style Sheet (CSS) specifications 1 through 3, as outlined on the World Wide Web Consortium's site.

Using JQuery library developers can enhance their websites without worrying about browsers and their versions as long as the browsers have JavaScript enabled.

Most of the JQuery CSS Methods do not modify the content of the jQuery object and they are used to apply CSS properties on DOM elements.

DOM Manipulation Methods
JQuery provides methods to manipulate DOM in efficient way. You do not need to write big code to modify the value of any element's attribute or to extract HTML code from a paragraph or division.

JQuery provides methods such as .attr(), .html(), and .val() which act as getters, retrieving information from DOM elements for later use.

Events Handling
We have the ability to create dynamic web pages by using events. Events are actions that can be detected by your Web Application.

Following are the examples events:

* A mouse click
* A web page loading
* Taking mouse over an element
* Submitting an HTML form
* A keystroke on your keyboard
* etc.

When these events are triggered you can then use a custom function to do pretty much whatever you want with the event. These custom functions call Event Handlers.

AJAX
AJAX is an acronym standing for Asynchronous JavaScript and XML and this technology help us to load data from the server without a browser page refresh.

If you are new with AJAX, I would recommend you go through our Ajax Tutorial before proceeding further.

JQuery is a great tool which provides a rich set of AJAX methods to develope next generation web application.

Effects
jQuery provides a trivially simple interface for doing various kind of amazing effects. jQuery methods allow us to quickly apply commonly used effects with a minimum configuration.

For References:
JQuery.com
JQuery Tutorial
Jquery API

Friday, April 23, 2010

How to get string in Title case

Here is the code example for converting the string to title case
DateTime todaysDate = DateTime.Now;
CultureInfo properCase = System.Threading.Thread.CurrentThread.CurrentCulture;
TextInfo textInfoObject = properCase.TextInfo;
grp.DrawString(textInfoObject.ToTitleCase(todaysDate.ToString("MMMM")) + " " + todaysDate.Year, new Font(new FontFamily("verdana"), 12, FontStyle.Bold), new SolidBrush(textColor), new PointF(360, 230));

Sunday, October 25, 2009

Autocomplete textbox in C#

Here is the code to develop auto complete text box.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
private void Form1_Load(object sender, EventArgs e)
{
AutoCompleteStringCollection autolist = new AutoCompleteStringCollection();
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");
con.Open();

SqlCommand comm = new SqlCommand("select * from users", con);
SqlDataAdapter da = new SqlDataAdapter(comm);

DataTable dt = new DataTable();
da.Fill(dt);
foreach (DataRow r in dt.Rows)
{
autolist.Add(r[0].ToString());
}
textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
textBox1.AutoCompleteCustomSource = autolist;
}

Wednesday, October 21, 2009

DateTime Format String

DateTime has its own set format string modifiers because there are so many ways to display a date and time.

Standard Format String

This is basically built in short hand for custom format string. You pass in the one character string to denote which custom format you want.

i.e.
now.ToString("d"); // "10/21/2009"
now.ToString("D"); // "Wednesday, October 21, 2009"
now.ToString("G"); // "10/21/2009 10:16:01 PM"


Custom Format String
Custom format string gives you the flexibility to build your own formatting. When using a single character format string specifier, you will need to prepend it with a "%", otherwise it will be interpreted as a Standard Format String. Here are the basics for building your own string:

Example:
StringBuilder strBuilder=new StringBuilder ();
DateTime now = new DateTime(2009,10, 21, 22, 16, 01, 08, DateTimeKind.Local);
strBuilder.AppendLine ("now.ToString() ==> " + now .ToString() );
strBuilder.AppendLine("now.ToString(\"d\")" + now.ToString("d"));
strBuilder.AppendLine("now.ToString(\"D\")" + now.ToString("D"));
strBuilder.AppendLine("now.ToString(\"G\")" + now.ToString("G"));

// Year
strBuilder.AppendLine("YEAR ");
strBuilder.AppendLine ("now.ToString(\"%y\") ==> " + now.ToString("%y") );
strBuilder.AppendLine ("now.ToString(\"yy\") ==> " + now.ToString("yy") );
strBuilder.AppendLine ("now.ToString(\"yyy\") ==>" + now.ToString("yyy") );
strBuilder.AppendLine ("now.ToString(\"yyyy\") ==>" + now.ToString("yyyy") );

//Month
strBuilder.AppendLine("MONTH ");
strBuilder.AppendLine ("now.ToString(\"%M\") ==> " + now.ToString("%M") );
strBuilder.AppendLine ("now.ToString(\"MM\") ==> " + now.ToString("MM") );
strBuilder.AppendLine ("now.ToString(\"MMM\") ==>" + now.ToString("MMM") );
strBuilder.AppendLine("now.ToString(\"MMMM\") ==>" + now.ToString("MMMM"));


//Day
strBuilder.AppendLine("Day ");
strBuilder.AppendLine("now.ToString(\"%d\") ==> " + now.ToString("%d"));
strBuilder.AppendLine("now.ToString(\"dd\") ==> " + now.ToString("dd"));
strBuilder.AppendLine("now.ToString(\"ddd\") ==>" + now.ToString("ddd"));
strBuilder.AppendLine("now.ToString(\"dddd\") ==>" + now.ToString("dddd"));

//Hour
strBuilder.AppendLine("HOUR ");
strBuilder.AppendLine("now.ToString(\"%h\") ==> " + now.ToString("%h"));
strBuilder.AppendLine("now.ToString(\"hh\") ==> " + now.ToString("hh"));
strBuilder.AppendLine("now.ToString(\"hhh\") ==>" + now.ToString("hhh"));
strBuilder.AppendLine("now.ToString(\"hhhh\") ==>" + now.ToString("hhhh"));
strBuilder.AppendLine("now.ToString(\"%H\") ==> " + now.ToString("%H"));
strBuilder.AppendLine("now.ToString(\"HH\") ==> " + now.ToString("HH"));
strBuilder.AppendLine("now.ToString(\"HHH\") ==>" + now.ToString("HHH"));
strBuilder.AppendLine("now.ToString(\"HHHH\") ==>" + now.ToString("HHHH"));

//Minutes
strBuilder.AppendLine("MINUTES ");
strBuilder.AppendLine("now.ToString(\"%m\") ==> " + now.ToString("%m"));
strBuilder.AppendLine("now.ToString(\"mm\") ==> " + now.ToString("mm"));
strBuilder.AppendLine("now.ToString(\"mmm\") ==>" + now.ToString("mmm"));
strBuilder.AppendLine("now.ToString(\"mmmm\") ==>" + now.ToString("mmmm"));

//Seconds
strBuilder.AppendLine("SECONDS ");
strBuilder.AppendLine("now.ToString(\"%s\") ==> " + now.ToString("%s"));
strBuilder.AppendLine("now.ToString(\"ss\") ==> " + now.ToString("ss"));
strBuilder.AppendLine("now.ToString(\"sss\") ==>" + now.ToString("sss"));
strBuilder.AppendLine("now.ToString(\"ssss\") ==>" + now.ToString("ssss"));

//Milliseconds
strBuilder.AppendLine("MILLISECONDS ");
strBuilder.AppendLine("now.ToString(\"%f\") ==> " + now.ToString("%f"));
strBuilder.AppendLine("now.ToString(\"ff\") ==> " + now.ToString("ff"));
strBuilder.AppendLine("now.ToString(\"fff\") ==>" + now.ToString("fff"));
strBuilder.AppendLine("now.ToString(\"ffff\") ==>" + now.ToString("ffff"));
strBuilder.AppendLine("now.ToString(\"%F\") ==> " + now.ToString("%F"));
strBuilder.AppendLine("now.ToString(\"FF\") ==> " + now.ToString("FF"));
strBuilder.AppendLine("now.ToString(\"FFF\") ==>" + now.ToString("FFF"));
strBuilder.AppendLine("now.ToString(\"FFFF\") ==>" + now.ToString("FFFF"));

//Kind
strBuilder.AppendLine("KIND ");
strBuilder.AppendLine("now.ToString(\"%K\") ==> " + now.ToString("%K"));
strBuilder.AppendLine("now.ToString(\"KK\") ==> " + now.ToString("KK"));
strBuilder.AppendLine("now.ToString(\"KKK\") ==>" + now.ToString("KKK"));
strBuilder.AppendLine("now.ToString(\"KKKK\") ==>" + now.ToString("KKKK"));

//TimeZone
strBuilder.AppendLine("TIMEZONE ");
strBuilder.AppendLine("now.ToString(\"%z\") ==> " + now.ToString("%z"));
strBuilder.AppendLine("now.ToString(\"zz\") ==> " + now.ToString("zz"));
strBuilder.AppendLine("now.ToString(\"zzz\") ==>" + now.ToString("zzz"));
strBuilder.AppendLine("now.ToString(\"zzzz\") ==>" + now.ToString("zzzz"));

//Other
strBuilder.AppendLine("OTHER ");
strBuilder.AppendLine("now.ToString(\"%g\") ==> " + now.ToString("%g"));
strBuilder.AppendLine("now.ToString(\"gg\") ==> " + now.ToString("gg"));
strBuilder.AppendLine("now.ToString(\"ggg\") ==>" + now.ToString("ggg"));
strBuilder.AppendLine("now.ToString(\"gggg\") ==>" + now.ToString("gggg"));

strBuilder.AppendLine("now.ToString(\"%t\") ==> " + now.ToString("%t"));
strBuilder.AppendLine("now.ToString(\"tt\") ==> " + now.ToString("tt"));
strBuilder.AppendLine("now.ToString(\"ttt\") ==>" + now.ToString("ttt"));
strBuilder.AppendLine("now.ToString(\"tttt\") ==>" + now.ToString("tttt"));

textBox1.Text = strBuilder.ToString();


Output:

now.ToString() ==> 10/21/2009 10:16:01 PM
now.ToString("d")10/21/2009
now.ToString("D")Wednesday, October 21, 2009
now.ToString("G")10/21/2009 10:16:01 PM
YEAR
now.ToString("%y") ==> 9
now.ToString("yy") ==> 09
now.ToString("yyy") ==>2009
now.ToString("yyyy") ==>2009
MONTH
now.ToString("%M") ==> 10
now.ToString("MM") ==> 10
now.ToString("MMM") ==>Oct
now.ToString("MMMM") ==>October
Day
now.ToString("%d") ==> 21
now.ToString("dd") ==> 21
now.ToString("ddd") ==>Wed
now.ToString("dddd") ==>Wednesday
HOUR
now.ToString("%h") ==> 10
now.ToString("hh") ==> 10
now.ToString("hhh") ==>10
now.ToString("hhhh") ==>10
now.ToString("%H") ==> 22
now.ToString("HH") ==> 22
now.ToString("HHH") ==>22
now.ToString("HHHH") ==>22
MINUTES
now.ToString("%m") ==> 16
now.ToString("mm") ==> 16
now.ToString("mmm") ==>16
now.ToString("mmmm") ==>16
SECONDS
now.ToString("%s") ==> 1
now.ToString("ss") ==> 01
now.ToString("sss") ==>01
now.ToString("ssss") ==>01
MILLISECONDS
now.ToString("%f") ==> 0
now.ToString("ff") ==> 00
now.ToString("fff") ==>008
now.ToString("ffff") ==>0080
now.ToString("%F") ==>
now.ToString("FF") ==>
now.ToString("FFF") ==>008
now.ToString("FFFF") ==>008
KIND
now.ToString("%K") ==> +05:00
now.ToString("KK") ==> +05:00+05:00
now.ToString("KKK") ==>+05:00+05:00+05:00
now.ToString("KKKK") ==>+05:00+05:00+05:00+05:00
TIMEZONE
now.ToString("%z") ==> +5
now.ToString("zz") ==> +05
now.ToString("zzz") ==>+05:00
now.ToString("zzzz") ==>+05:00
OTHER
now.ToString("%g") ==> A.D.
now.ToString("gg") ==> A.D.
now.ToString("ggg") ==>A.D.
now.ToString("gggg") ==>A.D.
now.ToString("%t") ==> P
now.ToString("tt") ==> PM
now.ToString("ttt") ==>PM
now.ToString("tttt") ==>PM

How to get string in Title case

Here is the code example for converting the string to title case.

//initialize the datetime object with today's date
DateTime todaysDate = DateTime.Now;

//get the current culture
CultureInfo properCase = System.Threading.Thread.CurrentThread.CurrentCulture;

//get text info object from the current culture
TextInfo textInfoObject = properCase.TextInfo;

//the textinfo ToTitleCase method will return the text in title case
MessageBox.Show(textInfoObject.ToTitleCase(todaysDate.ToString("MMMM")));



Output:

October

Here, the todaysDate.ToString("MMMM") prints the complete month like October, December etc.

Friday, August 14, 2009

Programmatically setting Master Page

The MasterPage class derives from UserControl. The master page will inject itself as the top control in a page’s control hierarchy by clearing the page’s Controls array and adding itself to into the collection. Doing so includes all of the markup and controls defined inside the master page in the page's control tree. The master page can then walk the Content controls that existed in the web form and bring them back them into the control hierarchy in the appropriate locations. The master page injection happens after the PreInit event fires for a Page object, but before the Init event fires.

We can use the @ Page directive and the web.config to specify master page files for our web forms, but sometimes we want to set the master page programatically. A page’s MasterPageFile property sets the master page for the content page to use. This property must be set in the PreInit event (or earlier), like the code below. One the master page has injected itself into the control hierarchy it is too late to try to set a new master page for the web form. If you try to set the MasterPageFile property after the PreInit event fires, the runtime will throw an InvalidOperationException.


Protected Sub Page_PreInit(ByVal sender As Object, _
ByVal e As EventArgs) _
Handles Me.PreInit
' we can select a different master page in the PreInit event
Me.MasterPageFile = "~/otherMasterPage.master"
End Sub


Another way to interact with a master page is through the Master property of a page object. Let’s say we need to get to the Footer property defined in our master page. There are two approaches to touching this property. The first is to use the Master property of the System.Web.UI.Page class, which returns a MasterPage reference. In order to get to the Footer property, though, we would have to cast the reference to our derived master page type, like the following code.
CType(Master, otherMasterPage).FooterText = "New Footer Text"


A second approach, if you know the exact master page file your page will be using at runtime, is to let the ASP.NET page parser generate a strongly typed Master property by adding an @ MasterType directive to your ASPX file, as shown below.
<%@ MasterType VirtualPath="~/myMasterPage.master" %>


The MasterType directive will instruct the runtime to add a new Master property to code-generated file for the page. The new property will return a reference matching the MasterType. With a MasterType directive in place for our web form, we don't need a cast.

Master Pages Events

Another master page twist that catches developers off guard is the order of the page lifecycle events. Let’s say we write the following code in our web form:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Response.Write("Hello from Page_Load in default.aspx ")
End Sub


.. and the following code in our master page:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Response.Write("Hello from Page_Load in Master1.master")
End Sub


Pop quiz: which Response.Write will appear in the output first?
Hint: most ASP.NET events are raised starting at the top of the control tree and working downward.
In this case, “Hello from Page_Load in default.aspx” will appear before “Hello from Page_Load in Master1.master”, because the content page’s Load event fires before the master page’s Load event.

Let’s set up another quiz using the following code in our content page.

Protected Sub Page_Init(ByVal sender As Object,ByVal e As System.EventArgs)
Response.Write("Hello from Page_Init in default.aspx")
End Sub


... and the following code in our master page.

Protected Sub Page_Init(ByVal sender As Object,ByVal e As System.EventArgs)
Response.Write("Hello from Page_Init in Master1.master")
End Sub


Pop quiz: which Init event will fire first?

Earlier we said most ASP.NET events work their way down the tree of controls. The truth is all lifecycle events (Load, PreRender, etc.) work in this fashion except the Init event. The initialization event works from the inside out. Since the master page is inside the content page, the master page’s Init event handler will fire before the content page’s Init event handler.

Obviously, problems will occur if the content page’s Load event handler depends on the master page's Load event to finish some work or initialize a reference. If you find yourself with this problem, or are worried about the order of events when a master page is involved, you might be too tightly coupled to the master page. Consider our earlier approach of using a custom event when when something interesting happens in the master page, and let the content page subscribe to the event and take action. This approach achieves greater flexibility.

Master Pages

Master pages in ASP.NET are the key to building a professional web application with a consistent, easy to maintain layout.

A professional web site will have a standardized look across all pages. For example, one popular layout type places a navigation menu on the left side of the page, a copyright on the bottom, and content in the middle. It can be difficult to maintain a standard look if you must always put the common pieces in place with every web form you build. In ASP.NET 2.0, master pages will make the job easier. You’ll only need to write the common pieces once - in the master page. A master page can serve as a template for one or more web forms. Each ASPX web form only needs to define the content unique to itself, and this content will plug into specified areas of the master page layout.

Example Master Pages:

To add a master page to a web project, right-click your web project in the Solution Explorer window, select Add New Item, and select the Master Page item type from the Add New Item dialog. The listing below shows a sample master page in source view.


<%@ Master Language="VB" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>


A master page looks very similar to an ASPX file, except a master page will have a .master file extension instead of a .aspx extension, and uses an @ Master directive instead of an @ Page directive at the top. Master pages will define the <html>, <head>, <body> , and <form> tags. A new control, the ContentPlaceHolder control also appears in our master page. You can have one or more ContentPlaceHolder controls in a master page. ContentPlaceHolder controls are where we want our ASPX web forms to place their content.
Let’s also write a simple web form to use our master page.


<%@ Page Language="VB" MasterPageFile="~/Master1.master"
AutoEventWireup="true" Title="Untitled Page" %>
<asp:Content ID="Content1" Runat="Server"
ContentPlaceHolderID="ContentPlaceHolder1" >
<asp:Label ID="Label1" runat="server" Text="Hello, World"/>
</asp:Content>



Just like any ASPX form, our master page can contain code in a <script> block, or in a code-behind file, and can respond to page lifecycle events. The MasterPage class (from the System.Web.UI namespace) derives from UserControl and will have all of the usual events: Init, Load, PreRender, etc.

The web form contains a single Content control, which in turn is the proud parent of a Label. At this point, the page and master page are two separate objects, each with their own children. When it comes time for the master page to do its job, the master page replaces the page’s children with itself.

The master page’s next step is to look for Content controls in the controls formerly associated with the page. When the master page finds a Content control that matches a ContentPlaceHolder, it moves the controls into the matching ContentPlaceHolder. In our simple setup, the master page will find a match for ContentPlaceHolder1, and copy over the Label.

All of this work occurs after the content page’s PreInit event, but before the content page’s Init event. During this brief slice of time, the master page is deserving of its name. The master page is in control - giving orders and rearranging controls. However, by the time the Init event fires the master page becomes just another child control inside the page. In fact, the MasterPage class derives from the UserControl class.

Of Headers and Meta tags

The master page must define <head>,meaning the master page will include the <title> tag, among others. Since the master page does not know the title of the content page that will plug in, it simply sets the title to “untitled”.
Since a content page cannot contain any markup outside of Content controls, there is no way for a content page to use a title tag, but there is now a Title attribute in the @ Page directive. There is also a Title property on the page itself.
For other elements that end up in the header, the Page class now exposes a Header property of type HtmlHead. You can use the HtmlHead reference to modify style sheet settings and add HtmlMeta objects as needed.

Conclusions

Master pages provide a key component to any web application, namely the ability to organize a consistent layout into reusable templates. These master page templates offer full designer support and programmatic interfaces to meet the demands of most applications.

Saturday, August 8, 2009

The extern modifier C# Reference

The extern modifier is used to declare a method that is implemented externally. A common use of the extern modifier is with the DllImport attribute when using Interop services to call into unmanaged code; in this case, the method must also be declared as static, as shown in the following example:

[DllImport("avifil32.dll")]
private static extern void AVIFileInit();


It is an error to use the abstract (C# Reference) and extern modifiers together to modify the same member. Using the extern modifier means that the method is implemented outside the C# code, while using the abstract modifier means that the method implementation is not provided in the class.

Example

In this example, the program receives a string from the user and displays it inside a message box. The program uses the MessageBox method imported from the User32.dll library.

using System;
using System.Runtime.InteropServices;
class MainClass
{
[DllImport("User32.dll")]
public static extern int MessageBox(int h, string m, string c, int type);

static int Main()
{
string myString;
Console.Write("Enter your message: ");
myString = Console.ReadLine();
return MessageBox(0, myString, "My Message Box", 0);
}
}


This example creates a DLL from a C program that is invoked from within the C# program in the next example.

// cmdll.c
// compile with: /LD
int __declspec(dllexport) SampleMethod(int i)
{
return i*10;
}


This example uses two files, CM.cs and Cmdll.c, to demonstrate extern. The C file is the external DLL created in Example 2 that is invoked from within the C# program.

// cm.cs
using System;
using System.Runtime.InteropServices;
public class MainClass
{
[DllImport("Cmdll.dll")]
public static extern int SampleMethod(int x);

static void Main()
{
Console.WriteLine("SampleMethod() returns {0}.", SampleMethod(5));
}
}


Output

SampleMethod() returns 50.


Remarks

To build the project:

* Compile Cmdll.c to a DLL using the Visual C++ command line:

cl /LD Cmdll.c

* Compile CM.cs using the command line:

csc CM.cs

This will create the executable file CM.exe. When you run this program, SampleMethod will pass the value 5 to the DLL file, which returns the value multiplied by 10.

Note
The extern keyword also can define an external assembly alias, making it possible to reference different versions of the same component from within a single assembly. For more information, see extern alias (C# Reference).

The extern keyword is more limited in use than in C++. To compare with the C++ keyword, see Using extern to Specify Linkage in the C++ Language Reference.

IIf Function

Returns one of two objects, depending on the evaluation of an expression.

Public Function IIf( _
ByVal Expression As Boolean, _
ByVal TruePart As Object, _
ByVal FalsePart As Object _
) As Object


Parameters

Expression

Required. Boolean. The expression you want to evaluate.

TruePart

Required. Object. Returned if Expression evaluates to True.

FalsePart

Required. Object. Returned if Expression evaluates to False.


Remarks

The IIf function provides a counterpart for the ternary Conditional Operator: ? : in Visual C++.

Example

This example uses the IIf function to evaluate the testMe parameter of the checkIt procedure and returns the word "Large" if the amount is greater than 1000; otherwise, it returns the word "Small".
Visual Basic

Function checkIt(ByVal testMe As Integer) As String
Return CStr(IIf(testMe > 1000, "Large", "Small"))
End Function


Note that if Option Strict is On, you must use the CStr keyword to explicitly convert the return from Object to String.

?: Operator

The conditional operator (?:) returns one of two values depending on the value of a Boolean expression. The conditional operator is of the form.
condition ? first_expression : second_expression;


If condition is true, first expression is evaluated and becomes the result; if false, the second expression is evaluated and becomes the result. Only one of two expressions is ever evaluated.

Calculations that might otherwise require an if-else construction can be expressed more concisely and elegantly with the conditional operator. For example, to avoid a division by zero in the calculation of the sin function you could write either

if(x != 0.0) s = Math.Sin(x)/x; else s = 1.0;


or, using the conditional operator,

s = x != 0.0 ? Math.Sin(x)/x : 1.0;


The conditional operator is right-associative, so an expression of the form

a ? b : c ? d : e


is evaluated as

a ? b : (c ? d : e)


not

(a ? b : c) ? d : e


The conditional operator cannot be overloaded.

Example

// cs_operator_conditional.cs
using System;
class MainClass
{
static double sinc(double x)
{
return x != 0.0 ? Math.Sin(x)/x : 1.0;
}

static void Main()
{
Console.WriteLine(sinc(0.2));
Console.WriteLine(sinc(0.1));
Console.WriteLine(sinc(0.0));
}
}



Output

0.993346653975306
0.998334166468282
1

Friday, July 31, 2009

How to get uploaded image dimensions in asp.net

If you want to know the image dimension you can get the dimensions by using the following code snippet by providing the image path to the method:

public string getImageDimensions(string strPath)
{
System.Drawing.Image imgFromFile= System.Drawing.Image.FromFile(strPath);
float imgWidth = imgFromFile.PhysicalDimension.Width;
float imgHeight = imgFromFile.PhysicalDimension.Height;
string size= imgWidth.toString()+","+imgHeight.toString();
return size;
}

Where,
strPath is the path of the file.

Similary we can get the get uploaded image dimensions as follows:

public string getImageDimensions(Stream streamImgFile)
{
System.Drawing.Image imgFromStream= System.Drawing.Image.FromStream(streamImgFile);
float imgWidth = imgFromStream.PhysicalDimension.Width;
float imgHeight = imgFromStream.PhysicalDimension.Height;
string size= imgWidth.toString()+","+imgHeight.toString();
return size;
}


For e.g.here in the following code imgUploader is name of asp.net file uploader control:

string strImgUploadedType = imgUploader.PostedFile.ContentType.ToString().ToLower();
string strImUploadedFileName = imgUploader.PostedFile.FileName;

//get Dimensions from stream
string strDim= getImageDimensions(imgUploader.PostedFile.InputStream);
//Or we can use from file
string strDim= getImageDimensions(imgUploader.PostedFile.FileName);

string[] strDimArr= s.Split(',');

Response.Write( "Image Width:" + strDimArr[0]+ “
”);
Response.Write("Image Height:" + strDimArr[1]+ “
”);


The PostedFile property will contain a valid System.Web.HttpPostedFile object if file indeed was uploaded. HttpPostedFile provides us with 4 properties:

* ContentLength: size of uploaded file in bytes
* ContentType: MIME type of uploaded file, i.e. "image/gif"
* FileName: full path to uploaded file on client's system, i.e.c:\Bin\Img1.gif
* InputStream: stream object that gives us access to uploaded data

Wednesday, July 22, 2009

Adding Client-Side Script from the Code-Behind Class

All ASP.NET Web pages must be derived directly or indirectly from the Page class in the System.Web.UI namespace. The Page class contains the base set of methods, properties, and events required for a functioning Web page. Among the class's many methods are a few methods designed for injecting client-side script into the rendered HTML. These methods are called from the code-behind class and can therefore be used to emit data-driven client-side script. The pertinent Page class methods for emitting client-side script follow.

The base class is derived from the System.Web.UI.Page class, so you can access the Page class's public methods by calling them directly from your code-behind class.
Note To access the Page class's methods, you can either type in the method name directly, or utilize IntelliSense in Microsoft Visual Studio .NET by entering MyBase. (for Microsoft Visual Basic .NET), this. (for C#), or Page. (for either C# or Visual Basic .NET). If you are using Visual Basic .NET as your programming language of choice, be sure to configure Visual Studio .NET to not hide advanced members, or you won't see these client-side script methods. (To show advanced members, go to Tools | Options | Text Editor | Basic and uncheck Hide advanced members.)

RegisterClientScriptBlock(key, script)

The RegisterClientScriptBlock method adds a block of client-side script after the Web Form's rendered form element, before any Web controls contained within the Web Form. The key input parameter allows you to specify a unique key associated with this script block, whereas the script parameter includes the complete script code to emit. (This script parameter should include the actual script element, along with the client-side JavaScript or Microsoft VBScript.)

When emitting client-side script through the code-behind class of an ASP.NET Web page, typically the value of the key parameter isn't of paramount importance. Simply choose a descriptive key value. The key parameter is more pertinent when injecting client-side script code through a custom, compiled server control. There may be instances where a compiled control requires that there be a set of client-side functions. Multiple instances of the server control on one page might be able to share these common client-side script functions, so these functions need only be emitted once for the entire page, and not once per control instance. For example, the validation controls utilize client-side code to enhance the user experience. This client-side code must be present if there are any validation controls on the page, but if there are multiple validation controls, all can use this single set of shared functions.
By giving a script block a key, a control developer building a control that utilizes a set of common client-side functions can check to see if the required set of common functions has already been added by another instance of the control on the page. If so, it need not re-add the common script. To check if a script block has been added with the same key, use the IsClientScriptBlockRegistered(key) method, which will return a Boolean value indicating whether or not a script block with the same key has been registered. Realize that you can add a script block without first checking if it's registered. If you attempt to add a script block with a key that's already registered, the added script block will be ignored and the original script block will remain assigned to that key.

Note The IsClientScriptBlockRegistered method is particularly useful in two situations. First, it comes in handy when you're adding similar, but unique script blocks, and you need to insure that each script block is given a unique key. The code we'll examine later on in this article illustrates the utility of the "is registered" method. A second use is when building a control that needs some common script, especially if the script is not trivially generated. By using the IsClientScriptBlockRegistered method, you can ensure that the script common to all instances of the server control on the page is generated only once per page load, rather than once per control instance on the page.


The RegisterClientScriptBlock method is useful for adding client-side script that does not rely on any of the form fields present within the Web Form. A common use of this method is to display a client-side alert box. For example, imagine that you had a Web page with some TextBox Web controls and a Save Button. The TextBox controls might have particular values from a database. Imagine that this page allowed the user to modify these values and commit their changes by clicking the Save button. When clicking Save, the Web page would be posted back, and the Button's Click event would fire. You could create a server-side event handler for this event that updates the database. To let the user know that their changes had been saved, you might want to display an alert box that says, "Your changes have been saved." This could be accomplished by adding the following line of code to the Button's Click event handler:


The above will add the specified script content within the page's form, but before the content within the form. When the page is rendered in the user's browser they will see a client-side alert box displayed upon page load.



Note One potentially undesirable side effect of the above example is that the alert box will be displayed right after the browser received the form tag. The browser will suspend rendering of the Web page until the user clicks the alert box's OK button. This means that the user will see a white browser page until they click OK. If you want to have the page displayed completely before displaying the alert box, you can have the JavaScript inserted at the end of the form element using the RegisterStartupScript method, which we'll discuss next.


RegisterStartupScript(key, script)
The RegisterStartupScript method is quite similar to the RegisterClientScriptBlock method. The main difference is the location where the client-side script is emitted. Recall that with the RegisterClientScriptBlock the script is emitted after the start of the form element, but before the form's contents. RegisterStartupScript, on the other hand, adds the specified script at the end of the form, after all form fields. Use RegisterStartupScript to place client-side script that interacts with the rendered HTML elements. (Later we'll look at an example of setting the focus to a form field upon page load; to accomplish this you'll use the RegisterStartupScript method.)
Like RegisterClientScriptBlock, the script blocks added by RegisterStartupScript need a unique key value. Again, this key value is primarily used by custom control developers. Not surprisingly, there is an IsStartupScriptRegistered(key) method as well, which returns a Boolean value indicating if a script block with the specified key has already been registered or not.
Note For more information on using RegisterStartupScript and RegisterClientScriptBlock in creating custom, compiled server controls, read an earlier article of mine: Injecting Client-Side Script from an ASP.NET Server Control.


RegisterArrayDeclaration(arrayName, arrayValue)
If you need to create a client-side JavaScript Array object with some set values, use this method to add a value to a specific array. For example, when using validation controls in an ASP.NET Web page, an Array object (Page_Validators) is built that contains references to the set of validation controls on the page. When the form is submitted, this array is enumerated to check if the various validation controls are valid or not.
To add the values 1, 2, and 3 to a client-side Array object named FavoriteNumbers, you'd use the following server-side code:
RegisterArrayDeclaration("FavoriteNumbers", "1")
RegisterArrayDeclaration("FavoriteNumbers", "2")
RegisterArrayDeclaration("FavoriteNumbers", "3")

This code would emit the following client-side script:

Notice that each array value passed in must be a string; however, the client-side script rendered sets the values of the Array object as the contents of the string. That is, if you wanted to create an Array with the string values "Scott" and "Jisun", you'd use:
RegisterArrayDeclaration("FavoriteFolks", "'Scott'")
RegisterArrayDeclaration("FavoriteFolks ", "'Jisun'")

Notice that the second input parameters are strings that contain 'Scott' and 'Jisun'—text delimited by a single apostrophe. This would render the following client-side script:
var FavoriteFolks = new Array('Scott', 'Jisun');

RegisterHiddenField(hiddenFieldName,hiddenFieldValue)

In classic ASP there was often the need to pass around various bits of information from one page to another. A common way of accomplishing this was using hidden form fields. (A hidden form field is a form field that is not displayed, but whose value is sent on the form's submission. The syntax for creating a hidden form field is .) The need for passing information around by custom hidden form fields in ASP.NET is greatly reduced since the state of the controls in the page is automatically persisted. If, however, you find that you need to create a custom hidden form field, you can do so through the RegisterHiddenField method.
The RegisterHiddenField method accepts two input parameters: the name of the hidden field and the value. For example, to create a hidden form field with the name foo and the value bar, use the following code:
RegisterHiddenField("foo", "bar")

This would add a hidden form field within the page's form element, like so:

Friday, July 17, 2009

Features in C# 3.0

Implicitly Typed Local Variables
C# 3.0 introduces a new keyword called "var". This new keyword enables you to declare a variable whose type is implicitly inferred from the expression used to initialize the variable. For example, consider the following line of code:
var i = 30;

The preceding line initializes the variable i to value 30 and gives it the type of integer. Note that "i" is strongly typed to an integer—it is not an object or a VB6 variant, nor does it carry the overhead of an object or a variant.
We can also write for strings like…
var s = "Hello again!";


In addition to implicitly declaring variables, you can also use the var keyword for declaring arrays as well. For example, consider the following lines of code:
int[] numbers = new int[] { 1, 2, 3, 4, 5};
string[] names = new string[] { "Dave", "Doug", "Jim" };

By using the var keyword, you can rewrite the preceding lines of code as follows:
var numbers = new[] { 1, 2, 3, 4, 5};
var names = new[] { "Dave", Doug, "Jim" };

The var keyword is NOT a completely new type, instead the compiler just takes a look at the right-hand side of the expression. If the right-hand side is an int, for example, the compiler will "replace" the var keyword with int.

Object initializers
Whenever you declare a class, most of the times the class is used only as a placeholder with getters and setters for holding property values without any additional logic. For example, consider a simple class like the following:
public class Person
{
int _id;
string _firstName;
string _lastName;

public int ID
{
get{return _id;}
set{_id = value;}
}
public string FirstName
{
get{return _firstName;}
set{_firstName = value;}
}
public string LastName
{
get{return _lastName;}
set{_lastName = value;}
}
public string FullName
{
get{return FirstName + " " + LastName;}
}
}


As you can see from the above class declaration, it doesn't contain any additional logic. The get and set properties are repetitive and they simply set or get the values of the properties without adding any value. In C#, you can simplify that by leveraging the new feature named Auto-Implemented properties. By taking advantage of this new feature, you can rewrite the above code as follows:
public class Person
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName
{
get
{
return FirstName + " " + LastName;
}
private set {;}
}
}

In the above code, when you declare a property, the compiler automatically creates a private, anonymous field that is available only to the property's get and set accessors. Note that the auto implemented properties must declare both a get and set accessor. However if you need to create a read-only property, modify the scope of the set accessor to be private.

Object and Collection Initializers
In previous versions of C#, you would create the object in one step and then populate its properties in a separate next step. However with C# 3.0, you can create the object as well as set its properties in a single line of code. For example, consider the following lines of code:
Person obj = new Person();
obj.ID = 1;
obj.FirstName = "Thiru";
obj.LastName = "Thangarathinam";
MessageBox.Show(obj.FullName);

In the above code, you create the Person object and set its properties appropriate values. By leveraging the object initializers feature, you can simplify the above code as follows:
Person obj = new Person { ID = 1, FirstName = "Thiru", LastName = "Thangarathinam" };
MessageBox.Show(obj.FullName);

In the above code lines of code, you directly assign the values to the properties of the Person object. Although you can simulate this behavior using constructor for the object, object initializers reduce the need to have a specific constructor for every variation of argument variation we need over time.
In C# 2.0, when you want to populate a collection, you need to write the following line of code:
List names = new List();
names.Add("David");
names.Add("Tim");
names.Add("Doug");

Now with the introduction of the new collection initializers feature in C# 3.0, you can shorten the above lines of code to a single line of code:
List names = new List {"David", "Tim", "Doug"};


Extension methods
Another cool thing is that you're able to add (static) methods to existing (default) classes. As an example, here is the code to add a ToMD5() method to the string class:

public static class StringExtensions
{
public static string ToMD5(this string s)
{
System.Security.Cryptography.MD5 md5;
md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] result = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(s));
// Return the result, replace unneeded "-"
return System.BitConverter.ToString(result).Replace("-", string.Empty);
}
}



Once compiled, all string objects now have the ToMD5 method. If you placed the extension class above in a seperate namespace, you should import this namespace to enable the usage. Example below:
string s = "Hello, this string is gonna be MD5-ed!";
Console.WriteLine(s.ToMD5()); // Prints the MD5 hash


As you can see from the preceding lines of code, the extension methods allow you to write cleaner and easy-to-maintain code.
Here are some of the key characteristics of extension methods:
• The extension method as well as the class that contains the extension method should be static.
• Although extension methods are static methods, they are invoked as if they are instance methods.
• The first parameter passed to the extension method specifies the type on which they operate and it is preceded by the "this" keyword.
• From within the extension method, you can't access the private variables of the type you are extending.
• Instance methods take precedence over extension methods in situations where they have same signature.


Anonymous Types
As the name suggests, anonymous types allow you to create a type on-the-fly at compile time. The newly created type has public properties and backing fields defined for the members you initialize during construction. For example, consider the following line of code:
var obj=new{ID=1,FirstName="Thiru",LastName="Thangarathinam"};

In the above line, you just specify the various attributes you want to have in the anonymous class and assign the instantiated object to a variable of type "var". The actual type assigned to obj is determined by the compiler. Since the compiler assigns the name of the type only at compile time, you can't pass an anonymous type to another method and it can only be used within the method they were declared.
When the compiler sees the above code, it automatically declares a class as follows:
class __Anonymous1
{
private int _id = 1;
private string _firstName = "Thiru";
private string _lastName = "Thangarathinam";

public int ID
{
get{return _id;}
set{_id = value;}
}
public string FirstName
{
get{return _firstName;}
set{_firstName = value;}
}
public string LastName
{
get{return _lastName;}
set{_lastName = value;}
}
}

Anonymous Types use the Object Initializer to specify what properties the new type will be declare.
Note that the anonymous types are just meant to be placeholders for quickly defining entity types and you can't add methods or customize the behavior of an anonymous type.

Lambda Expressions
Lambda expressions are simply functions and they are declared in the context of expressions than as a member of a class. It is an inline expression or a statement block which can be used to pass arguments to a method or assign value to delegate. All lambda expressions use the lambda operator => and the left side of the operator denotes the results and the right side contains the expression to be evaluated. For instance, consider the following lambda expression:
age => age + 1

The above function takes one argument named age, and returns age + 1 as the result. As you can see, Lambda expressions follow the below syntax:
(parameter-list) => expression;

where expression can be any C# expression or a block of code. Just like anonymous methods you can use a lambda expression in place of a delegate. Here are some sample lambda expressions and their corresponding delegates.
//Explicitly typed parameter
(Person obj) => MessageBox.Show(obj.FirstName.ToUpper());

//Implicitly typed parameter
(obj) => obj.FirstName == "Thiru";

//Explicitly typed parameter
(int a, int b) => a + b

//Implicitly typed parameter
(x, y) => { return x + y; }


As you see from the preceding lines of code, lambda expressions can be written in such a way that it can infer the parameter type from the signature of the delegate it is assigned to.

Query Expressions
This feature, also known as LINQ (Language Integrated Query), allows you to write SQL-like syntax in C#.
For instance, you may have a class that describes your data as follows:
public class CoOrdinate
{
public int x ;
public int y;
}

You now could easily declare the logical equivalent of a database table inside C# as follows:
// Use Object and collection initializers
List coords = ... ;

And now that you have your data as a collection that implements IEnumerable, you easily can query this data as follows:
var filteredCoords =
from c in coords
where x == 1
select (c.x, c.y)

In the SQL-like syntax above, "from", "where", and "select" are query expressions that take advantage of C# 3.0 features such as anonymous types, extension methods, implicit typed local variables, and so forth. This way, you can leverage SQL-like syntax and work with disconnected data easily.
Each query expression is actually translated into a C#-like invocation behind the scenes. For instance, the following:
where x == 1
Translates to this:
coords.where(c => c.x == 1)

As you can see, the above looks an awful lot like a lambda expression and extension method. C# 3.0 has many other query expressions and rules that surround them.

Friday, July 3, 2009

JSON (JavaScript Object Notation)

JSON is a part of the ECMAScript standard since ECMA has defined in 1999 the eval() function that parses the format.
It has been popularized with the success of Ajax.
The JSON word appears often when one is speaking about Ajax. We know this is another data format, that can replace XML, and this format is supported by a lot of programmers. But what is exactly JSON, what are the advantages?

Why JSON?

The benefit of JSON is that it is recognized natively by JavaScript. No need for parsing an XML document to extract the data and get it throught the net.

JSON and XML

Benefits of JSON:
- The easiness of reading.
- The easiness of using.


Benefits of XML:
- XML is extensible.
- It is widely used and recognized by almost all programming languages.


Unfortunally, both XML and JSON are enable to integrate a large amount of data in binary form.

The syntax of JSON

The components of JSON:
- An object: contains objets or attributes.
- A scalar variable: Number, String, Boolean.
- An array.
- Literal values: null, true, false, "string of characters", and numerical values.
Object


It contains a member or a list of members, and each member has the form:
"name" : "value"

The syntax of the object is:
{ member, member, .... }


Array
A collection of values, separated by commas.
[ value, value, ....]


Values
A value may be: an object, an array, a litteral (string, number, true, false, null).
Nothing more is required to create a JSON file!

Example of JSON file
A simple example, designing a menu:
It is an object made of members that are an attribute and an array that holds other objects, the rows of the menu.

{
"menu": "File",
"commands": [
{
"title": "New",
"action":"CreateDoc"
},
{
"title": "Open",
"action": "OpenDoc"
},
{
"title": "Close",
"action": "CloseDoc"
}
]
}


The XML equivalent:




How to use the format
The JSON file allows to load data from the server or to send data to it, in this format. For example, storing the content of a form, just filled by an user. This involves three steps: the browser processing, the server processing, and the data exchange between them.

Client side (browser)
This is rather easy, as JSON is a part of the JavaScript definition. The content of a file, or the definition of the data is assigned to a variable, and this variable becomes an object of the program.

Server side
JSON file are used by various programming languages, including PHP and Java thanks to parsers that allow to get the content and that may even convert it into classes and attributes of the language.
The json.org includes a C parser and a list of parsers in other languages.

Data exchange
Loading a file may be accomplished from JavaScript in several ways:
- direct including of the file into the HTML page, as a JavaScript .js external file.
- loading by a JavaScript command.
- using XMLHttpRequest.


The JSON file is parsed by the eval() JavaScript function.

Sending the file to the server may be accomplished by XMLHttpRequest. The file is sent as a text file and processed by the parser of the programming language that uses it.
Example
The XMLHttpRequest code:
var req = new XMLHttpRequest();
req.open("GET", "file.json", true);
req.onreadystatechange = myCode; // the handler
req.send(null);

The JavaScript handler:
function myCode()
{
if (req.readyState == 4)
{
var doc = eval('(' + req.responseText + ')');
}
}


Using the data:
var menuName = document.getElementById('jsmenu'); // finding a field
menuName.value = doc.menu.value; // assigning a value to the field


How to access data:
doc.commands[0].title // read value of the "title" field in the array
doc.commands[0].action // read value of the "action" field in the array