Make immutable objects mutable

In .Net the String object is an immutable object, which means that it’s basically constant and can’t be changed. You can’t change a string, you can only create new strings. The various methods that the String class have never directly change the string they operate on, instead they return a new string. Dim myString As String = "Hello World" Dim mySecondString As String = myString.Substring(0, 5) Console.WriteLine(myString) ‘Hello World Console.WriteLine(mySecondString) ‘Hello Even though we developers modify strings all the time, which can make them look mutable, they aren’t. What happens is that a new string is created and the reference … Continue reading Make immutable objects mutable

Nifty extension methods

One of my favorite features in .Net 3.5 is the ability to extend a class using extension methods. Earlier you could only extend a class either by creating a new class that inherits from the first class or you could create a partial class. Of course if you have the source code of the original class you could just write the new code directly in it and recompile it. In many cases you don’t have access to the source code of a class and if the class is also sealed (NotInheritable in VB) you would not be able to extend … Continue reading Nifty extension methods

How to prevent Extension methods from appearing as global functions

C# have static (or shared) classes which VB lacks. Instead VB uses modules which are almost the same thing. One thing differs though and that is that modules makes its public methods appear as global accessible functions, meaning you don’t have to type the ModuleName.MethodName() when calling such a member. You can instead simply call MethodName() directly. A disadvantage of this fact is that extension methods, that must exist in modules in VB, will appear in IntelliSense as global functions and not only as a method of the type you extended. Let’s say you have the following: Public Module ExtensionMethods … Continue reading How to prevent Extension methods from appearing as global functions