Thursday, September 17, 2009

C# - An Interview w/ the Design Team

Hey Guys,

Check out this interview w/ the C# design team. It's cool! C# is one language I admire the most next to C/C++ ;) Enjoy!

http://channel9.msdn.com/posts/Charles/C-40-Meet-the-Design-Team/

Happy coding :)

Tuesday, August 11, 2009

Multiple Main() methods inside a project

Hey All,

I am back :)

If you try to compile a project having multiple Main() methods, the compiler will throw an error. The simplest solution in this case would be to comment out/ remove teh Main() methods apart from the one we currently wish to use.

If you do not want to delete the additional Main() methods, do this:

Choose Project -> Properties
Choose the "Application" tab
In this tab, there is a drop down list titled "Startup object"
Choose the class that contains the "entry point" that should be run, ignorin the remaining Main() methods

I found this out in a MS developer site and thought of posting it. Hope this helps somebody!

Happy Coding :)

~ Karthik

Thursday, July 23, 2009

Implementing Functions that Times out after a pre-defined time interval

Hi All!

Been a long time! I found this post by Ohad extremely useful. This is about implementing functions that will time out after a specified time interval. This behavior would be expected in various cases - for example - when you wait for a web service to respond to you. So in this case you may choose to time out after you wait for 's' seconds and try some other service, for instance.

This is the link -

http://weblogs.asp.net/israelio/archive/2004/06/19/159985.aspx

Happy coding!

~ Karthik

Friday, June 12, 2009

Debugging Windows Services

Hey Guys!

This post is some info. about windows services. There are lot of blog posts/tutorials floating around in the web that deals with creation of windows services. But this post deals with the methods to debug a windows service. After you start your service you can attach it to a debugger, so that you can debug a windows service! Conventionally it wouldn't be possible to debug a windows service by pressing F5, as you would have known. This is because services are run from the context of a services control manager.

Now let me stop these stories and get directly in to reality!!

In a nutshell, these are the steps to be followed:

* Develop the service
* Start the service
- Start / Control Panel / Administrative Services / Computer Management
- Click on Services and Applications / Services
- Scroll to your service
- Start it (Right click - start)
* Open your project for this service in VS.Net
* Set breakpoints (Obviously you cannot debug the onStart method or the Main method!)
* Choose Debug -> Processes
* Choose the windows service in the list of "Available Processes"
* Click on "Attach"

There you go!! Hope this helps somebody!!!

Want a "professional" view of the same? Refer to,

http://msdn.microsoft.com/en-us/library/7a50syb3(VS.80).aspx

Happy coding :)

Friday, June 5, 2009

Getting the Type of the current instance and Name of the method currently being executed

Hi Folks,

I am back, after a long time :) This post describes how we could get the name of the instance that is currently being executed and also the method name that is currently being executed.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;

namespace TestProject
{
class ToCall
{
public ToCall()
{
Console.WriteLine("This is " + this.GetType());
MethodBase method = MethodInfo.GetCurrentMethod();
string methodname = method.DeclaringType.FullName + "." + method.Name;
Console.WriteLine("Method name is " + methodname);

sample();
}

public void sample()
{
Console.WriteLine("This is " + this.GetType());
MethodBase method = MethodInfo.GetCurrentMethod();
string methodname = method.DeclaringType.FullName + "." + method.Name;
Console.WriteLine("Method name is " + methodname);
}
}

class Program
{
static void Main(string[] args)
{
ToCall d = new ToCall();
}
}
}


I will add some comments soon, just wanted to get started initially!!

Happy Coding :)

Sunday, October 19, 2008

RegEx - To validate emails...

Hey All,

You can use this to validate emails!

[a-zA-Z0-9_]+\.{0,1}[a-zA-Z0-9_]*\.{0,1}[a-zA-Z_]*[\@][a-zA-Z]+[\.][a-zA-Z]+[\.]{0,1}[a-zA-Z]*

Happy coding :)

Saturday, October 11, 2008

Power of LINQ - Querying ArrayLists

Hey all!

Welcome back :)

This posting is about querying ArrayLst's using LINQ. Following is an example of the same. Explanations follow after the code..

using System;
using System.Linq;
using System.Collections;

namespace QueryArrayList
{
public class Report
{
public int reportNo;
public string reportName;
}

public class QueryArrayList
{
private static bool found = false;

public QueryArrayList()
{
}

public static void Main(string[] args)
{
string query_text = "null";

if (args.Length == 1)
{
query_text = args[0];
}

ArrayList aList = new ArrayList();

for (int i=0; i < 10; i ++)
{
Report report = new Report();
report.reportNo = i;
report.reportName = "Report " + i.ToString();

aList.Add(report);
}

var query =
(from Report reportList in aList
where reportList.reportName.Contains(query_text)
select new { reportList });

if (query != null)
{
foreach (var report in query)
{
Report rpt = new Report();
rpt.reportNo = report.reportList.reportNo;
rpt.reportName = report.reportList.reportName;

Console.WriteLine("Report retrieved: Report Number: {0} and Report Name: {1}",rpt.reportNo,rpt.reportName);

found = true;
}
}

if (! found)
Console.WriteLine("Empty result set");
}
}
}


The code is pretty straight forward. So I will get into the LINQ related part directly. There is a class called Report and the class QueryArrayList creates 10 objects of Report. The following LINQ query takes in as a parameter some text that is used to select a set of reports containing that text.

var query = (from Report reportList in aList
where reportList.reportName.Contains(query_text)
select new { reportList });


Here 'aList' is the ArrayList that contains the list of "Report" objects and using this part

from Report reportList

we specify that we are querying an ArrayList of type "Report".

where reportList.reportName.Contains(query_text)

The above part is to limit rows with the query text and finally

select new { reportList }

is to select the reports satisfying the condition and place it in a variable called reportList

In the foreach part I get each row in the set, create a new object of type "Report" and print it to the console.

That's it! We have successfully queried an ArrayList!!

Happy coding :)