Sunday, August 31, 2008

Order of execution for class Constructor and Initializers

Hi All,

The origin of this post is the query posted at MSDN blog

This was a featured article at Start Page of VS2008 as well.

Well in short this is about that why Constructors of Base class are invoked first where as Initialization of class member variables is performed in Reverse Order i.e. from Derived to Base.

Until now we were suggested to save paper but now i believe in saving server hard disk as well :) So in case if you are interested to know the answer which i provided then please see search "Atreya A" at the original MSDN blog


Wednesday, August 27, 2008

UML - Types of Relationships

1. Association

a. Aggregation: ‘has-a’ relationship: Employee has an address.
b. Composition: ‘part-of’ relationship: Engine is a part of Car

2. Inheritance: ‘type-of’ relationship : Car is a type of Vehicle
3. Realization: Implementation of some Interface

NOTE: Aggregation and Composition are sometimes confusing E.g. in examples above I have used car and engine in Composition Relationship, whereas Car and engine can also share an Aggregation as shown below:

“Swift Car has a Fiat Engine”

But the v.v.imp point here is that when we say that “Swift Car has a Fiat Engine”, the lifetime of object Fiat engine is not in hands of particular instance of ‘Swift Car’ because there might be some other car for which we can say that “Suzuki Sx4 has a Fiat Engine as well”
So the crux is that engine is a generic term here as there could be more than 1 owner/consumer
Whereas at the same time in a Composition Relationship there can’t be more than 1 owner at the same time i.e. 1 particular engine could be part of 1 car only. So if we destroy car object we can easily destroy the said instance of Engine object.

Thursday, February 14, 2008

C# keywords: readonly vs const

The readonly keyword is different from the const keyword. A const field can only be initialized at the declaration of the field. A readonly field can be initialized either at the declaration or in a constructor. Therefore, readonly fields can have different values depending on the constructor used.

In a nut shell we can say that, while a const field is a compile-time constant, the readonly field can be used for runtime constants as in the following example:

public static readonly uint l1 = (uint) DateTime.Now.Ticks;

Refrences: MSDN July 2004

namaste !

Thursday, February 7, 2008

ByRef keyword while passing arguments in C#

I have to come up with this post when I heard a number of people saying that “all Reference type values in C# are passed ‘By Reference’ and the byRef Keyword is there just to increase the readability of code”

So my dear friend this is not the case at all byRef Keyword is not here to increase the readability of code!

So what does byRef actually do?

In situations when we have to pass a Reference Type argument and there is a need to use new operator on the passed argument; we have to have make use of byRef keyword otherwise a new dangling instance will be created on the HEAP (as reference types are stored on heap).

By dangling instance I mean that the newly created memory location will not be linked to the variable for which we called new operator.
You will not get any compile time or runtime error but the code will behave as if you never used the new operator.

I guess we can better understand this with the help of a simple example. So please go ahead and experience it!

You are welcome to leave comments so that I can help you out if more clarification is required.

namaste!  Atreya, A

Thursday, January 10, 2008

Quick note on IDispatch, Dual & DispInterface

IDispatch Interface
The IDispatch interface was initially designed to support Automation. It provides a late-binding mechanism to access and retrieve information about an object's methods and properties(often useful in scripting languages such as VBScript, JavaScript etc).

IDispatch is designed in such a way that it can call virtually any other COM interface. Developers not working on C or C++ often cannot call COM interfaces through direct OLE VTable binding. However, when their development language supports IDispatch( e.g. as Visual Basic does) and when the object they want to call supports IDispatch, they can call its COM interfaces indirectly through IDispatch::Invoke method.

DispInterface is a pure IDispatch Interface which can not use vtable based interface. Hence DUAL attribute cant be used on dispInterface.

Dual Interfaces
OLE Automation enables an object to expose a set of methods in two ways: via the IDispatch interface, and through direct OLE VTable binding. Dual Interface supports both.

The first seven entries of the VTBL for a dual interface are the seven members of IDispatch(i.e. 4 of IDispatch and 3 of IUnknown), and the remaining entries are COM entries for direct access to members of the dual interface.


Wednesday, January 9, 2008

Database Normalization

Different Normal Forms in a Relational Database

1NF says:
*Eliminate duplicate columns from the table.
*No attribute should contain multiple values(e.g. 2 or more phone numbers separated by coma under column 'Contacts').
In such case Create separate tables for each group of related data.
*User should be able to identify each row with a unique column or set of columns (the primary key).

2NF says:
*Table Should be in 1st NF
*all the non-prime attributes must have Full functional dependency on all candidate keys.
i.e. non-prime attributes depends on complete candidate keys and not on its subset.
*Place any such data to new tables and link them with the help of foreign keys.

3NF Says:
*Table Should be in 2nd NF
*It prohibits transitive functional dependencies of non-prime attributes on candidate keys.

BCNF says:
*Table should be in 3NF
*If X -> Y i.e. If Y depends on X, then X shoud be a super key (i.e. X is either a candidate key or a superset of candidate key).

4NF says
*Table should be in 3NF
*A relation is in 4NF if it has no multi-valued dependencies.

- Normalization on
- Normalization on