Compartir a través de


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

  • Anonymous
    March 02, 2004
    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
  • Anonymous
    March 02, 2004
    Both of those look cool to me.
  • Anonymous
    March 02, 2004
    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
  • Anonymous
    March 04, 2004

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

    If Not something IsNot somethingElse Then
    ...
    End If

    :)
  • Anonymous
    March 04, 2004
    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.
  • Anonymous
    March 04, 2004
    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.
  • Anonymous
    March 05, 2004
    how do i move an image around the page like it is moving?
  • Anonymous
    March 07, 2004
    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
  • Anonymous
    March 08, 2004
    teaching VB6
  • Anonymous
    March 09, 2004
    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.