Pavan Gayakwad

Friday, July 03, 2009

2 'Why's

1. Why Microsoft is still using Flash when its own baby- Silverlight light is out there?


2. As shown above, having popup menu open on IE8, why Windows Start menu does not appear when windows key (on keyboard) is pressed? (Windows Vista with SP2 Operating system)

Grr...

Labels:

Thursday, June 11, 2009

In This World - 14

Any mistake is big a failure if we don't understand why and how it happened.

Labels:

Monday, June 08, 2009

DotNet: Dynamic or Runtime Type Conversion

When I was coding for one of my applications, I encountered one tricky situation which hinted me about the need of Dyanmic Type Casting. Dynamic Type Casting is not a dotnet ready to use feature, so I had to spend some good time to come up with nice piece of generic code. Let me explain the challenge and then the solution. Below code and scenario is cooked up only for demonstration purpose and it is not straight from my application code base.

Let’s say, I have 3 classes. ‘Address’ which is a base class for ‘Email’ and ‘Postal’ classes.

public class Address
{
public int ID { get; set; }
}

public class Email : Address
{ }

public class Postal : Address
{ }
I have 3 overloaded methods which performs verification actions on object it gets.
public void StartContactProcess(Address obj)
{ //do work... }

public void StartContactProcess(Email obj)
{ //do work... }

public void StartContactProcess(Postal obj)
{ //do work... }
From UI, I always receive either Email or Postal object, type casted to Address type. So my routing method (which UI has access to) with if-else conditions looks like this.
private void DoVerification(Address obj)
{
if (obj is Postal)
StartContactProcess((Postal)obj);
else if (obj is Email)
StartContactProcess((Email)obj);
else StartContactProcess(obj);
}
As a good programming/design practice, overloaded methods can be smartly used to avoid writing big nested if-else conditions. So I think of changing code as shown below,
private void DoVerification(Address obj)
{
StartContactProcess(obj);
}
It will not work as expected because all the time StartContactProcess(obj) statement calls method with parameter ‘Address’ type only. But I want my DoVerification() method intelligent enough to understand the incoming (type cast) objects type and call the appropriate overloaded method at run time.

So finally, DotNet reflections made the solution simple and generic. The improved version of DoVerification() method is here.
private void DoVerification(Address obj)
{
MethodInfo mi = this.GetType().GetMethod("StartContactProcess", new Type[] { obj.GetType() });
mi.Invoke(this, new object[] { obj });
}
Idea here is, invoke StartContactProcess method on the fly using Reflections. Create appropriate StartContactProcess() method instance for the incoming object type (Email or Postal) and invoke by supplying type cast object as its parameter at run time. ‘this’ holds implementation for StartContactProcess() overloaded methods with public scope. The DoVerification() method will remain untouched in future even if we pass new children of Address base class.

My attention in this post is on explaining Dynamic type conversion technique at run-time in dotnet rather concentrating more on the design decision. However, you are always free to express your thoughts :)

Labels: ,

Friday, June 05, 2009

DotNet: Generic Sorting Class

Recently, I was searching for a generic sorting approach on dotnet List. Many articles (Like one, two..etc) were suggesting standard way of creating IComparer implementer class and using that with generic list (List <T>) in our application. Writing IComparer class for every business object not only take huge time also create challenges in code management when it grow considerably. So, here is simple and effective generic sorting class which can be used with any custom business object and can be sorted on any one of its properties.

All you have to do is, copy paste the complete code in to a new .cs file.

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

namespace Srushti.Business
{
public class GenericComparer<T> : IComparer<T>
{
string _propName = string.Empty;
bool _sortDirection = true;
public GenericComparer(string propertyName, bool SortAscending)
{
_propName = propertyName;
_sortDirection = SortAscending;
}

private int CompareAsc(object o1, object o2)
{
if (o1 is IComparable)
return ((IComparable)o1).CompareTo(o2);
else if (o1.Equals(o2))
return 0;
else
return o1.ToString().CompareTo(o2);
}
#region IComparer<T> Members

public int Compare(T x, T y)
{
PropertyInfo pi = x.GetType().GetProperty(_propName);
object o1 = pi.GetValue(x, null);

pi = y.GetType().GetProperty(_propName);
object o2 = pi.GetValue(y, null);

if (_sortDirection == false)
return CompareAsc(o1, o2) * -1;
else
return CompareAsc(o1, o2);
}
#endregion
}
}


...and start consuming it as shown below. You have to supply, property name (as String) on which you want to sort the list and sorting direction (Boolean, true for ascending and false for descending) to GenericComparer() constructor.

private void Form1_Load(object sender, EventArgs e)
{
List empList = new List();
empList.Add(new Employee() { ID = 1, Name = "Three" });
empList.Add(new Employee() { ID = 2, Name = "Two" });
empList.Add(new Employee() { ID = 3, Name = "One" });

/*** Generic Comparer ***/
empList.Sort(new GenericComparer("Name", true));

foreach (Employee em in empList)
MessageBox.Show(em.ID.ToString());
}

public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
}

Please Note: Class properties with complex data types like DateTime, Nullable might require special attention.

Labels:

Thursday, May 21, 2009

Quick Quest - A Step Ahead

With a strong history of serving Kuvempu University for question paper generation, Quick Quest is now serving Scholar’s private college in Nigeria and results are very satisfying. The teacher using this software sent his experiences and I am very happy to publish his 'as is' words on main page of Quick Quest section.
This is a great encouragement to the development effort of Quick Quest and it boosts making this product more useful.

Thanks to “The Scholar’s Private Collage, Akure, Nigeria” for choosing Quick Quest for their critical examination activities where question paper generation is core part of it.

Labels:

Sunday, April 12, 2009

LINQ Quick Start

If you are a new bee to LINQ (Language Integrated Query), stunned by looking at big books and longer web pages on LINQ technology, here is the relaxing news. Read “LINQ: The Future of Data Access in C# 3.0” by Joe Hummel. Joe presented LINQ in a very effective and simple manner by making sure to touch all aspects of it.

This book with 60 pages quickly
gives pragmatic understanding of LINQ and make you to start using LINQ in next hour. This may not be 'the complete reference book', however its a feel good starter in spades.

I
Personally prefer reading such quick start books to get glimpse of technology and later pick thicker book or detailed tech web links to dig deeper.

Labels:

Sunday, April 05, 2009

Present

The saxophone playing in lavish and lazy mood on my home theater audio system. Half filled crystal clear mocktail glass is standing still on the table. It served me 12 times. Ice cubes in the container melting slowly. Few small pieces of pizza left out in the take-away box. Ceiling fan is blowing cool air. With aesthetic gratification, I am sleeping in my bed. Not fully slipped in dream world. My senses are still enjoying the atmosphere. My right leg big and second toe shaking unnoticeably. Laptop silently downloading recent updates and also scanning for Conficker warm which recently attracted much of attention all over the world. Television is in mute, “DOA: Dead or Alive” action was on. Washing machine is busy cleaning dirty cloths. My salary credit message is sitting unread in my mobile message inbox. The alarm is down counting to wake me up in next 2 minutes…
I felt I needed this lazy day after my busy project work schedule at office. I spent restless week. I am lying in my bed
like a reluctant chap. I got to hear this from somewhere my inside, "Is this what I wanted?"
I said- “of course Yes! Not sure about the future though. Whatever I have now is what all I use to dream in my college days. Life gifted me this day. One lazy day with
immense satisfaction!"

Labels:

Why I Am Not Ready for IE8?

Microsoft’s free web browser Internet Explorer 8 is out for public use and it started appearing on windows update programs list as well. This time IE8 is offering a tight competition to its rivalry browsers by putting up noticeable features like accelerators, web slices, common color tab groups, security filters and better performance in comparison with its previous flavors.

It was impressive in the beginning when I started using IE8 with all its new features; on my aggressive usage below points came to notice.

  • Still few web sites are breaking
  • Tabs are not responsive if one of the tabs is struggling
  • Page rendering sometimes takes more processing time and
  • Browser speed is not always faster
Let me admit, I got addicted to Firefox though it’s a memory hogging browser. But it just doesn’t toddle when I am doing too many things on it. IE8 is an obtrusive update for IE7/IE6 users. Users like me have slim to none chances to migrate to IE8 yet.

Labels:

Wednesday, March 18, 2009

MS Works for Windows 7.0

Windows Vista never ‘wow’ed me. Office 2003 installation did, when I was fiddling around MS office 2003 advanced installation options. I got to see 'Works for Windows 7.0'!!!


Did Microsoft have intuition about Windows 7 operating system in the year 2003? Before I think too wild, the description helped me to understand that 7.0 is the version of ‘Microsoft Works’ ;)

Labels:

Sunday, March 15, 2009

One Evening Rain

I was too far from my home by the time I realize that its gonna rain nicely now. Its 7 eve and it’s my routine walk time. Weather turned darker. When I turn around, I saw slight panic in people’s eyes. It started raining. Those who were walking started running, two wheelers stopped near shops or big trees for shelter. People in car were closing windows. Many were kind of worried about getting drenched.

It's getting everything wet including me. I am standing looking up at the sky. The rain drops are falling on my face and I shut my eyes. I am feeling cold. My heart is more lively and warm. I am flying free. Thunder and lightning didn’t change what I'm feeling. I kept my walk as usual and as simple as it can. Many were looking at me strangely, some with a question mark on their face and few with confusion. They all had one common question-“Are you mad? It’s raining!!”
I just laughed at it! I couldn’t stop walking in the rain and so my thinking. We became so much ‘civilized’ that we now worry so much to enjoy natural happenings. We are worried more about what others think. Our etiquettes are not comfy with natural events. We fell sick when weather changes slightly. Our health is more delicate. We became complicated. Our day to day 'modern busy life' is making us to ignore the fact that ‘we are part of nature’.

Labels: