Standing on the shoulders of giants. RSS 2.0
# Monday, February 28, 2011

I’ve been doing a lot of development using Visual Basic in the past couple of months and there are some interesting features in the language which may or may not be very well known. Features that don’t have an equivalent in C#.

Default values for auto implemented properties.

Visual Basic support the option of setting a default value for a property, like you would in a field.

Public Property MyProp As Integer = 42

MSDN: Auto-Implemented Properties

Key properties in anonymous types

By defining properties in anonymous types as key properties, you can enable specific behavior for the equality behavior of these types. When an anonymous type contains key properties, then for both the Equals and the GetHashCode overridde is generated based on these key properties. Additionally the key properties are readonly, where normal properties are read/write.

Dim one = New With {.Name = "one", Key .No = 1}

Dim two = New With {.Name = "two", Key .No = 1}

' This is true because the key property for both instances of the same class is equal

Assert.IsTrue(one.Equals(two))

MSDN: Key

Filtered exceptions

Adding a filter to a catch expression in a try/catch block allows you to direct the exception handling based on a condition. This is especially useful when you’re catching a general exception, which should be handled differently based on the error code it contains.

Try

Throw New COMException()

Catch ex As COMException When ex.ErrorCode = 0

Console.WriteLine("y = 0")

Catch ex As COMException When ex.ErrorCode <> 0

Console.WriteLine("y <> 0")

End Try

MSDN: Try...Catch...Finally Statement

Posted via email from Paul van Brenk's posterous

Monday, February 28, 2011 4:36:33 PM (Pacific Standard Time, UTC-08:00)  #    Comments [0] - Trackback

Comments are closed.
Ads
About
© Copyright 2012
Paul van Brenk
Sign In
newtelligence dasBlog 2.3.2011.0
All Content © 2012, Paul van Brenk
DasBlog theme 'Business' created by Christoph De Baene (delarou)