Thursday, June 23, 2005

Generics in Visual Studio.NET 2005

Features Of Visual Studio 2005.NET Beta 2:
Struck by the sheer amount of time some of us have wasted by not playing around with Visual Studio.NET 2005!!! For the next few days I'll be publishing all new features of Visual Studio.NET 2005 as I play around with them. After installing Visual Studio.NET 2005 (and NOT being able to install MSDN Locally!) Generic Classes was the first thing I could lay my eyes on. I quickly wanted to write up a Generic Class and understand what I can and cannot do with them! The Below code snippet attempts to describe:
When Would I use Generic Classes?
From what I gathered I Would use Generics when I want a Type to accept any Generic Type and perform action on it! This requires some code to explain (the MSDN Code of Linked List implementation was slightly complicated):

/// <summary>
// Takes Any Generic Class.
/// </summary>
/// <typeparam name="T"></typeparam>
public class CurrentObject<T>
{
private T data;
/// <summary>
/// return the added object of the class to the calling program.
/// </summary>
/// <returns></returns>
public T returnObject()
{
return data;
}
/// <summary>
/// Allows addition of object of that Generic class
/// </summary>
/// <param name="objectData"></param>
public void AddObject(T objectData)
{
data = objectData;
}
}
 

The first thing we do in the above snippet is Create a class called CurrentObject which can accept any Generic Class as a parameter. Once we have accepted a type we create an object of the type accepted in the first line of the class.
We now introduce two new methods returnObject and AddObject. AddObject allows me to pass an object of the class I declared earlier. Once we have the object we just assign it to the object we created internally in the CreateObject class. In return Object we just return the class.
Lets take a quick preview of how we use this Generic Class. First thing we do is we create a quick class with two field that we will use with this Generic Class that we created!

/// <summary>
/// A Class That Will be passed to the Current Object object
/// </summary>
public class Customer
{
public string CustomerFirstName = "";
public string CustomerLastName = "";
}
Now the interesting part:
static void Main(string[] args)
{
// Create and Instance Of CurrentObject Generic Type
CurrentObject<Customer> currentCustomer = new CurrentObject<Customer> ();
Customer tmpCustomer = new Customer();
tmpCustomer.CustomerFirstName = "rajiv";
tmpCustomer.CustomerLastName = "popat";
// The Below could be a complex Linked List or Similar Implementation!
currentCustomer.AddObject(tmpCustomer);
Customer finalCustomer = currentCustomer.returnObject();
// Print the results obtained from the Generic type!
Console.WriteLine("Customer First Name is : " + finalCustomer.CustomerFirstName);
Console.WriteLine("Customer Last Name is : " + finalCustomer.CustomerLastName);
Console.ReadKey();

}

We create an object of CurrentObject Generic class by passing the Customer class to it. Then we use another customer object that we have and pass it to the AddObject Method. (This is the method which could have been as complicated as we want based on the logic of the collection that we are developing!) The returnObject finally returns the object and assigns it to finalCustomer. Again, it is to be noted here returnObject could return any object of the same class based on the complex logic of our collection!!!

So basically the same collection we design works for virtually ALL classes and types!!! MIND BLOWING!!!

Wednesday, June 22, 2005

Uninstalling Visual Studio.NET 2005 Beta 1

This link talks about uninstalling Visual Studio.NET 2005 (Beta 1) so that you can move on to better things in life (Beta 2). Some really useful tips here, namely...

If you see an error removing J# .NET Redistributable Package 2.0 from Add/Remove Programs, please run "msiexec /x {9046F10C-F5E7-4871-BED9-8288F19C70DF}" from a command line window

If you see an error removing .NET Framework 2.0 from Add/Remove Programs, please run "msiexec /x {71F8EFBF-09AF-418D-91F1-52707CDFA274}" from a command line window

Even if there were no errors and the Beta 2 keeps complaining that you have old J# Redistributable and Framework 2.0 still present in your computer EVEN after removing these from Add / Remove Programs the above tips are basically life savers!

Tuesday, June 07, 2005

Validating Date inputs for specific format.

There are times when we need to trap date input and see if the value is a valid date or not. the usual approach to this is to use DateTime.Parse and then trap exceptions. if there is an exception we assume that the date entered is not valid. however there are also times when we need to trap exact format in which the date entered. e.g. we want the user to enter 02-24-1995 5:00 and NOT 02/24/1995. The code snippet illustrates how this can be done.

System.Globalization.DateTimeFormatInfo dtFormat = new System.Globalization.DateTimeFormatInfo();
dtFormat.LongDatePattern = "MM'-'dd'-'yyyy HH':'mm";
try
{
DateTime dt = DateTime.ParseExact("02-24-1995 5:00","MM'-'dd'-'yyyy HH':'mm", dtFormat);
}
catch(Exception ex)
{
MessageBox.Show("The Following Exception Was Trapped While Validating Date:" + ex.Message);
}

Thursday, June 02, 2005

LDAP/Active Directory Authentication

Fucntion that authenticates username and password against active directory:
public bool AuthenticateLDAPUser(string LDAPServerIpAddress, string username, string password)
{
try
{
DirectoryEntry de_eForceDirectory = new DirectoryEntry("LDAP://" + LDAPServerIpAddress ,username, password);
DirectorySearcher deSearcher = new DirectorySearcher (de_eForceDirectory);
SearchResult searchResult = deSearcher.FindOne();
DirectoryEntry de_result = searchResult.GetDirectoryEntry();
if(de_result.Username==null)
{ return false; }
else
{ return true; }
}
catch (Exception ex)
{
throw ex;
}
}
Calling Code
MessageBox.Show("Authentication returned: " + AuthenticateLDAPUser("10.6.0.1", "rajiv", "password").ToString()); // Where 10.6.0.1 is your Active Directory Server.