Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a keyword to VB.net? ("Exists" vs "IsNot Nothing")

IsNot Nothing is very common, but it's a double negative =o

I'd like to use Exists instead. Is there someway I can add a keyword to my VB vocab?

For now, I wrote an extension that adds _Exists() as a property to each object. I use this frequently, but I'd still prefer an actual keyword.

<HideModuleName()>
Public Module CustomExtensions

    ''' <summary>
    ''' Returns <c>True</c> if [object] is not <c>Nothing</c>; otherwise <c>False</c>.
    ''' </summary>
    <System.Runtime.CompilerServices.Extension()>
    Public Function _Exists(obj As Object) As Boolean
        Return obj IsNot Nothing
    End Function

End Module

I use Visual Studio 2010 exclusively, so if I could trick VS into converting my custom phraseology into the standard syntax, that'd work for me.

Thanks!

like image 695
J.Steve Avatar asked Aug 22 '11 16:08

J.Steve


1 Answers

This answer isn’t very helpful but here goes: currently, you cannot do this.

Future versions of VS (particularly when they release their compiler service internals and make them extensible) could allow it – although I actually doubt that they will allow new keywords to be added, as this isn’t in the interest of a compiler vendor who wants to ensure an ecosystem of compatible code.

like image 64
Konrad Rudolph Avatar answered Nov 15 '22 03:11

Konrad Rudolph