Archive

Archive for March, 2008

HybridDictionary

March 28, 2008 amiraryani Leave a comment

HybridDictionary provides an efficient way to manage unknown size list.

This is the MSDN description about HybridDictionary:

“This class is recommended for cases where the number of elements in a dictionary is unknown. It takes advantage of the improved performance of a ListDictionary with small collections, and offers the flexibility of switching to a Hashtable which handles larger collections better than ListDictionary.”

In general if the number of elements is less than 10 then ListDictionary is the best choice, but for more than 10 elements Hashtable should be used. If you do not know how many memeber your collection will include, then you should HybridDictionary. It uses the ListDictionary class to manage the collection when there are only a few elements, then it automatically converts the elements into Hashtable when their numbers exceed optimum size of ListDictionary.

public class HybridDictionary :
IDictionary, ICollection, IEnumerable

Categories: .Net CLR

IComparer VS IComparable

March 28, 2008 amiraryani Leave a comment

Following sample code illustrate differences between IComparable and IComparer. It shows how to achieve the same thing by using these interefaces. The fist approach uses IComparable:

class IComparableTester
{
     class Student:IComparable<Student>
     {
         string _Name;
         int _OverallScore;
         public Student(string name, int overallScore)
         {
             _Name = name;
             _OverallScore = overallScore;
         }
         public string Name
         {
             get { return _Name; }
         }

         public int OverallScore
         {
             get { return _OverallScore; }
         }

         int IComparable<Student>.CompareTo(Student other)
         {
             return (this.OverallScore.CompareTo(other.OverallScore));                   
         }

     }
     public void PrintSortedStudentList()
     {
         List<Student> studentList = new List<Student>();
         studentList.Add(new Student(“Tom”, 80));
         studentList.Add(new Student(“Ali”, 90));
         studentList.Add(new Student(“Jeff”, 70));
         studentList.Sort();

         Console.WriteLine(“***** Use IComparable Interface *****”);
         foreach (Student student in studentList)
         {
             Console.WriteLine(“Name:\t{0}\t OverallScore:\t{1}”,
                 student.Name, student.OverallScore);
         }
     }
}

 

The second class uses the IComparer interface to sort student list:

class IComparerTester
  {
      class StudentComparer: IComparer<Student> 
      {
          int IComparer<Student>.Compare(Student a, Student b)
          {
              return a.OverallScore.CompareTo(b.OverallScore);  
          }
      }

      class Student
      {        
          string _Name;
          int _OverallScore;
          public Student(string name, int overallScore)
          {
              _Name = name;
              _OverallScore = overallScore;
          }
          public string Name
          {
              get { return _Name;}
          }
          public int OverallScore
          {
              get { return _OverallScore; }
          }
      }

      public void PrintSortedStudentList()
      {
          List<Student> studentList = new List<Student>();
          studentList.Add(new Student(“Tom”, 80));
          studentList.Add(new Student(“Ali”, 90));
          studentList.Add(new Student(“Jeff”, 70));
          studentList.Sort(new StudentComparer());

          Console.WriteLine(“***** Use IComparer Interface *****”); 
          foreach (Student student in studentList)
          {
              Console.WriteLine(“Name:\t{0}\t OverallScore:\t{1}”,
                  student.Name, student.OverallScore); 
          }
      }
  }

 

And the output from this console program shows that both works the same:

static void Main(string[] args)
{
   IComparerTester tester1 = new IComparerTester();
   tester1.PrintSortedStudentList();
   IComparableTester tester2 = new IComparableTester();
   tester2.PrintSortedStudentList();
   Console.ReadKey(); 
}

image

Categories: .Net CLR

Vista Compatibility – Avoid UAC

March 27, 2008 amiraryani Leave a comment

As part of upgrading your source codes to make them work on Vista, it is preferred to remove anything that required administrative privilege. The UAC message (permission request message) reduce the usability of the system and it is a good practice to avoid triggering UAC as much as possible. Also in environment that user is not administrator, your software will crash or stop working if it needs administrative privilege in Vista.

In the process of upgrading some of classic Visual Basic projects, I found a very surprising element which can trigger Vista UAC. Just add word “setup” to following Version Information of your VB6 project and you will get UAC guard on your exe file:

  1. VersionCompanyName
  2. VersionFileDescription
  3. VersionProductName
image File description is equal to “setup“. This Keyword put make Vista to ask for administrative privilege to run StandardProject.exe

image

I need to mention that the further test with .Net projects shows that this is not an issue with managed codes and projects that are developed based on .Net Framework.

Categories: VB Classic

Generic and Non-Generic Counterparts

March 26, 2008 amiraryani Leave a comment
Generic Non Generic
List<T> ArrayList
Dictionary<TKey,TValue> HashTable
SortedDictionary<TKey,TValue> SortedList
Stack<T> Stack
Queue<T> Queue
LinksList<T> (none)

I found above table in Jeffrey’s well known book “CLR via C#”. The comparison is quite useful specially for migrating some of .Net 1 source codes to .Net 2 or later versions.

Categories: .Net CLR