Sunday, March 26, 2006 

Making Optionals with C#

As being a component or library developer one has to consider many aspects of component development. One of the such aspect is providing a feature which is not natively supported by the language in which components is being developed. For example Optional parameters feature of VB.Net. Everyone knows that optional parameters are not possible in C#. Yes, that's true, well there is a way out. Just apply OptionalAttribute defined in the System.Runtime.InteropServices to the parameter that you want to make optional. That's it, you are done...

Here is an example...

public string GetName([Optional]string param)
{
if (param == null) //Check for null.
param = "Aamir"; //Set the default value.
return param;
}

Andyes, remember C# optional parameters are not optional in C#. This is the way of creating optional parameter for vb.net.

Saturday, March 25, 2006 

Ways of Instantiation

.Net provide many ways to instantiate an object. Here I am going to enlighten some of them.

  1. When Type is loaded or available.
    This is the most basic way of instantiating an object, To instantiate and object use new keyword, following the type’s constructor.

    Object obj = new Object();

  2. When type is not loaded or available

    If you want to instantiate object dynamically when Type (class, struct, etc…) is not available or not loaded into the assembly then Activator comes at your service. You can create an instance using Activator.CreateInstance(…) and Activator.CreateInstanceFrom(…) methods. These methods provide many overloads for creating instances from various parameters. Some of examples given below.
    1. Using Type’s object:

      IObject obj = (IObject) Activator.CreateInstance( type, new object [] {"a", 10});

      Here the first parameter is an object's Type, and the second parameter is an array of arguments which are supplied to the constructor of given Type.

    2. Using Assembly and class name as string

      ObjectHandle handle = Activator.CreateInstance( "AssemblyName","NamespaceName.ClassName" );
      IObject obj = (IObject)handle.Unwrap();

      There are other overloads also please check out MSDN document…

Thursday, March 23, 2006 

@Software-Architect.Net EMail Launched

I am pleased to announce that an online email service was integrated into Software-Architect.Net. If you are an architect, then why are you using an irrelavent email service. Get a 25 MB free email which suits your designation: your-name@software-architect.net.

Logon to Software Architect.Net Now...
http://www.software-architect.net/

About me

  • I'm M. Aamir Maniar
  • From Mumbai, Maharastra, India
My profile

Archives

  • September 2011
  • September 2006
  • August 2006
  • July 2006
  • April 2006
  • March 2006
  • February 2006
  • January 2005
  • November 2004
  • October 2004
Powered by Blogger
and Blogger Templates