Saturday, April 18, 2009

Fast clean way to check if an array contains particular element in C# 3.0

A good to know that really fast and the most elegant way to check if an array contains particular element in C# 3.0 seems to be using Contains() extension method.

using System;
using System.Diagnostics;
using System.Linq;
namespace Test
{
class Program
{
static void Main(string[] args)
{
int[] data = new int[10000000];

Random rand = new Random();
for (int i=0; i{
data[i] = rand.Next();
}

int value = data[data.Length/2];
ContainsViaForEachLoop(data, value);
ContainsViaForLoop(data, value);
ContainsViaContainsExtMethod(data, value);
ContainsViaAnyExtMethod(data, value);
Stopwatch watch = new Stopwatch();
watch.Start();
if (ContainsViaForEachLoop(data, value))
{
watch.Stop();
Console.WriteLine("foreach loop search:\t\t{0} ms",
watch.ElapsedMilliseconds);
}
watch.Reset();
watch.Start();
if (ContainsViaForLoop(data, value))
{
watch.Stop();
Console.WriteLine("for loop search:\t\t{0} ms",
watch.ElapsedMilliseconds);
}
watch.Reset();
watch.Start();
if (ContainsViaContainsExtMethod(data, value))
{
watch.Stop();
Console.WriteLine("Contains() method search:\t{0} ms",
watch.ElapsedMilliseconds);
}
watch.Reset();
watch.Start();
if (ContainsViaAnyExtMethod(data, value))
{
watch.Stop();
Console.WriteLine("Any() method search:\t\t{0} ms",
watch.ElapsedMilliseconds);
}
}
public static bool ContainsViaForEachLoop(int[] data, int value)
{
foreach (int i in data)
{
if (i == value)
{
return true;
}
}
return false;
}
public static bool ContainsViaForLoop(int[] data, int value)
{
for (int i=0; i{
if (data[i] == value)
{
return true;
}
}
return false;
}
public static bool ContainsViaContainsExtMethod(int[] data, int value)
{
return data.Contains(value);
}
public static bool ContainsViaAnyExtMethod(int[] data, int value)
{
return data.Any(i => i == value);
}
}
}

Accessing the properties of the event sender

I was Having two Controls in the Sender one dropdown list and other is a Radio Button List
the Access the object Sender

if (sender is DropDownList)
hiddenSelectedOptionID.Value = ddlOptions.SelectedValue;
if (sender is RadioButtonList)
hiddenSelectedOptionID.Value = rblTypes.SelectedValue;

Screenshot in 2 Clicks using .NET

Step 1: Fire up Visual Studio 2008 and Create a new Windows Application.
Step 2: From the toolbox, add a ‘ContextMenuStrip’ and a ‘NotifyIcon’ to the form.
Step 3: Choose an icon for the’ NotifyIcon’ using the Icon property. Change the ‘ContextMenuStrip’ property of the NotifyIcon to ‘contextMenuStrip1’ (which we added in Step 2)
Step 4: Add a new menu item to ContextMenuStrip1 control named “Grab Screenshot” as shown in the screen below:














Now double click this MenuItem and add the following code to its click event:
C#
private void grabScreenShotToolStripMenuItem_Click(object sender, EventArgs e)
{
Bitmap bmpSS = null;
Graphics gfxSS = null;

try
{
bmpSS = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height,
PixelFormat.Format32bppArgb);

gfxSS = Graphics.FromImage(bmpSS);

gfxSS.CopyFromScreen(
Screen.PrimaryScreen.Bounds.X,
Screen.PrimaryScreen.Bounds.Y,
0,
0,
Screen.PrimaryScreen.Bounds.Size,
CopyPixelOperation.SourceCopy);

SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.Filter = "JPeg Image*.jpg";
saveDialog.Title = "Save Image as";
saveDialog.ShowDialog();
if (saveDialog.FileName != string.Empty)
bmpSS.Save(saveDialog.FileName, ImageFormat.Jpeg);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

}



Note: Make sure you import System.Drawing and System.Drawing.Imaging namespaces.
In the code above, we create a new Bitmap which is equal to the width and height of the primary screen and then we create a new graphics using the Bitmap. We then use the CopyFromScreen() to capture the screen. Once this step is done, we create an object of SaveFileDialog and display it to the user to choose the location and save the file as an .jpeg image.
Step 5: Add the following code to the Form load event which will hide the form and remove it from taskbar.
C#
private void Form1_Load(object sender, EventArgs e)
{
this.ShowInTaskbar = false;
this.WindowState = FormWindowState.Minimized;
this.Hide();
}



Now run the program and right click the Icon that appears in your status bar. Click on the ‘Grab ScreenShot’ and save the screenshot to the desired location.

The next time you want a screenshot, remember it’s just two clicks away!
The entire source code of this article can be downloaded from here.

Tuesday, April 14, 2009

toolbar powered by Conduit

Saturday, April 4, 2009

Configuring NHibernate with ASP.NET

Object/Relational Mappers (OR/Ms) are getting increasing attention by .NET developers. By encapsulating many patterns that span from data access to data mapping and entity management, these tools often help developers overcome the so called impedance mismatch between the relational model at the base of the most used database engines and the object model supported by many programming languages.
One of the main benefits provided by OR/Ms is the increased speed in development that results from having a built-in implementation of many typical tasks related to data access, retrieval and mapping of data to objects – in a database-agnostic way.

read the Artilce on dotnetslackers.com
read also Implementing the Singleton Pattern in C#