Posts

Showing posts from August, 2015

Extending VB.NET objects that are marked as NotInheritable

One of the neat things in VB.Net is something called an "Extension" which allows you to extend the ability of other classes (even if its not possible to subclass the object with inheritance).  For example, in vb6 a lot of times we use Left$() and Right$() to work with strings, and there is a  vb.net  version of these (right() and left()) but it doesn't exist as a method as part of the string itself.  For example, dim MyString as string = "hello" debug.print left(myString,1) <-- prints 'h' but you can use the ToUpper method right off the string itself... debug.print myString.ToUpper <-- the same as ucase(myString) The code below adds 'left' and 'right' extensions to the string class so you can do this .. Imports  Example .Code.StringExtensions debug.print mystring.toupper. left(1)  I also added 'qptrim' as an extension of the trim method that strips out nulls as well.   debug.print mystring.qptrim Below is how you implement e