Paul Vick on two new operators in VB Whidbey


In a recent post to his blog, Paul Vick discusses the new IsNot Operator (and the history/rational behind the Is operator), which allows you to write;

If myString IsNot Nothing Then
instead of
If Not myString Is Nothing Then
and then, in another post, he covers the new TryCast operator (which is like C#’s ‘as’ operator), which will allow you to save a bit of extra work (and produce a slight perf improvement in some situations) when checking to see if an object can be cast to a specific type …. allowing you to write;
Sub Print(ByVal o As Object)
Dim PrintableObject As IPrintable _
= TryCast(o, IPrintable)
If PrintableObject IsNot Nothing Then
PrintableObject.Print()
End If

End Sub
instead of the slightly less efficient
Sub Print(ByVal o As Object)
Dim PrintableObject As IPrintable
If TypeOf o Is IPrintable Then
PrintableObject = CType(o, IPrintable)
PrintableObject.Print()
End If

End Sub

Comments (12)

  1. Kent Sharkey says:

    First one should have been Aint. This way, we could get into childish arguments:

    If myThing Is IsNot Is IsNot Nothing then…

    ‘Says you…

    End If

    The TryCast will be cool, though. It’s been one of the very few items (As) I’ve really started to like in C# and miss in VB.

    TTFN- Kent

  2. Chris Sells says:

    Both of those look cool to me.

  3. shivnil says:

    Hi,

    I am doing a project on forex. my client’s requirement is that the currency values should be update daily by the system itself.

    I am developing the system in VB/Access.

    There is website of Thomas Cook which contains the table of all currency values. The link as below :

    http://www.thomascook.co.in/tc/user/forex/forexcurrencyrates.jsp?jsessionid=2580261077697883828

    So, my problem I want the currency values, in the above link to update my ‘Currency_Master’ Table in MS-Access when click a command button.

    Please help or provide some code for it on

    shivnil2@rediffmail.com.

    thank you,

    with regards,

    shivnil

  4. Great, now we can finally start to write some "proper" code:

    If Not something IsNot somethingElse Then



    End If

    🙂

  5. Ant says:

    The IsNot operator seems a bit pointless but the TryCast is quite a nice touch, even though I can’t imagine that the performance improvement will be that significant.

  6. Joey says:

    IsNot is equivalent to using <> instead of not(x=y), so to my mind it fills a little gap.

    TryCast is extra sweet if you have to check for nothing anyway.

  7. james says:

    how do i move an image around the page like it is moving?

  8. rami emam says:

    i make project by bv5 with data base

    this db i well make password for he

    how 2 open this data base in my proeject

    when press f5 i c msg

    inviled password

    please f u can send 2 my this code

    thank 4 help

  9. parviz says:

    teaching VB6

  10. Joku says:

    Why it has to be such horribe looking mess? For example:

    If myString IsNot Nothing Then

    Could be:

    if (myString) { }

    and

    If myString Is Nothing Then

    could be:

    if (!myString) { }

    Much more clean and simpler.

Skip to main content