Code Repo    |     RSS
MD's Technical Sharing



Monday, June 1, 2009

New Features in VB 9.0 and C# 3.0

The following is a list of some new language-specific features available to C# 3.0 and VB 9.0, available in Visual Studio 2008, including Express editions.

Both VB and C#:
Extension methods
Anonymous Types
Local variable type inference

Object Initializers

Automatic properties
Lambda Expressions
Partial methods in VB and C#

VB Only:
Read More »

Monday, March 16, 2009

Use the DefaultEvent attribute in .NET

The DefaultEvent attribute indicates a class's default event. If the class is a component and you double-click on it in a form, the code editor opens to this event.

VB.NET:
<DefaultEvent("Reorg")> _
Public Class Organization
Public Event Reorg()
End Class


C#:

[DefaultEvent("Reorg")]
public class Organization
{
….
}

Reference:

www.vb-helper.com/howto_net_default_event.html
Read More »

Wednesday, October 22, 2008

Late-binding in .NET

Early-binding:

Variable types are defined at compile time (e.g. string, integers):

String st = "hello world";
Boolean ok = st.Contains("hello");


The compiler and linker will help verify that the argument types used to call a function matches the function's signature.

Late-binding:

At compile time, variable are declared as generic type (Object in .NET or Variant in VB6). The actual type will only be defined later at run-time, just before the object is used.

The developer needs to specify the function signatures and to ensure that the correct types are used.

In C/C++, late binding (a.k.a. run-time dynamic linking) often takes the form of LoadLibrary / GetProcAddress / FreeLibrary. The MSDN Library provides a good example of late binding in C/C++. Early biding is known as load-time dynamic linking in C++.

In VB6 and VB.NET, late-binding is possible with Option Strict turn off:

Option Strict Off
Dim st as Object = "hello world"
Dim ok as Object = st.Contains("hello")


'st' will be resolved to String type and 'ok' to Boolean at run-time.

Option Strict is by default off for all new VB.NET project created in VS2005. To change the default to On, go to the Visual Studio's Tools | Options menu, click the Projects \ VB Defaults folder and set Option Strict to On.

However, in C#, late-binding needs to be performed via Reflection:

Object obj = new object();
obj = "Hello world";
Type objType = obj.GetType();
Object objRet = objType.InvokeMember("IndexOf", BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Instance, null, obj, new object[] { "world" });


Reference:
  1. What is late binding?, http://blogs.msdn.com/davidklinems/archive/2006/11/27/what-is-late-binding.aspx
  2. Late Binding with Reflection, http://www.c-sharpcorner.com/UploadFile/samhaidar/LateBindingWithReflection09122005053810AM/LateBindingWithReflection.aspx?ArticleID=921b6d13-f609-4520-8a7d-8e70ce13b53b
Read More »