Welcome to CrankyGoblin.Com Sign in | Join | Help

Public Class GeoffAppleby

Inherits Microsoft.VisualBasic.MVP : Implements IBrainFart
I Love It When Logic Comes Together

I just proved to myself something. It's interesting, sure, but mostly I'm just pleased that I'm right :)

On Friday, a co-worker asked me if he needed to wrap the VB TypeOf keyword around a null check. While I try to appear godlike at work and seem to have all the answers, I really couldn't come straight out with the answer.

The easy way to find out is to check either a) the doco, or b) run it with a null and see if it goes kaboom. I chose neither. Times like these allow me to increase my IL knowledge just that little bit more, so I wrote a quick piece of code so that I could view the IL in reflector.

  Private Class TestTypeOf

  End Class

  Private Sub DoTypeOfCheck()
    Dim foo As TestTypeOf = Nothing

    If TypeOf foo Is TestTypeOf Then
      Debug.WriteLine("true")
    Else
      Debug.WriteLine("false")
    End If
  End Sub

Having done this, it's time to jump to reflector and have a look at the IL.

.method private instance void DoTypeOfCheck() cil managed
{
      // Code Size: 39 byte(s)
      .maxstack 1
      .locals (
            WindowsApplication4.Form1/TestTypeOf of1)
      L_0000: nop 
      L_0001: ldnull 
      L_0002: stloc.0 
      L_0003: ldloc.0 
      L_0004: isinst WindowsApplication4.Form1/TestTypeOf
      L_0009: brfalse.s L_0018
      L_000b: ldstr "true"
      L_0010: call void [System]System.Diagnostics.Debug::WriteLine(string)
      L_0015: nop 
      L_0016: br.s L_0024
      L_0018: nop 
      L_0019: ldstr "false"
      L_001e: call void [System]System.Diagnostics.Debug::WriteLine(string)
      L_0023: nop 
      L_0024: nop 
      L_0025: nop 
      L_0026: ret 
}

So, now we can see that the TypeOf keyword in VB is simply a wrapper around the IL call isinst, and the result of the call is tested for null. Now it was time to look up isinst in my trusty copy of The Common Language Infrastructure Annotated Standard.

The book told me what it does, and what exceptions are thrown, but it didn't come right out and tell me if it threw an exception if you passed it null. I should have logic-ed it out that it wouldn't, but maybe I was just itching to watch it blow. I ran my code and discovered that it doesn't blow up, just returns false in this case.

But the book also mentioned that it's very similar to the castclass call. The main difference is that isinst will only throw a TypeLoadException if it can't find a definition of the class you're interested in, whereas castclass will also throw an InvalidCastException if the instance can't be cast. When calling isinst, an actual cast takes place, just like for castclass - the difference is that it won't throw an exception, only return null. But they both do the actual cast.

This was fine and dandy, and my coworker went back to work, deleting the lines of code that he'd just written :)

Today I was thinking about it even more. I'd been pushed onto another train of IL playing and looking as a result of reading Joel Pobar's newest blog entry, CLR Dynamic Languages under the hood (Part 1 of many) - go read this by the way, it's very cool.

It occurred to me while I was playing with IL again that it's a shame that VB didn't wrapper the isinst call. This sounds like a nice way of having some conversions done, and it also seems like a waste that if we have code like this:

    If TypeOf foo Is TestTypeOf Then
      foo2 = DirectCast(foo, TestTypeOf)
    End If

Then we really have two casts being carried out - once for the TypeOf, once for the DirectCast (and we really do - compile and look at the IL, it really happens - there's an isinst and a castclass call :) Surely it would be quicker to only cast once and then check for null, than cast twice?

Then it struck me. In VB in the 2.0 framework there's a new keyword - TryCast. TryCast will do a cast if possible, otherwise return null - and there'll be no exception. Of course, I had to prove the theory.

I wrote the following in VS2005B2:

  Private Class Class1

  End Class

  Private Sub SomeFunc(ByVal sender As System.Object)
    Dim i As Class1
    i = TryCast(sender, Class1)
    i = DirectCast(sender, Class1)
  End Sub

Then we check the IL, and I see that I'm right *muhahaha*. TryCast calls isinst, and DirectCast calls castclass.

I love being right. It's educational for all of us :)

Listening to: on the mend - foo fighters - (4:31)
Posted: Sunday, 3 July 2005 8:41 PM by Geoff Appleby

Comments

Dave Donaldson said:

Yep... TryCast is the VB version of the 'as' keyword in C# today. Using 'as' in C# is quite handy, and I'm glad the VB team added their version to the language.
# July 20, 2005 10:44 PM

LeVaN said:

http://www.anal-mom.seksi-***.com ^^^ http://www.anal-movie.seksi-***.com ^^^ http://www.asiatiche-sex-in-anticamera.str0nz0.com ^^^ http://www.amichevole-ragazze-orale-fotti.str0nz0.com ^^^ http://www.mujer-mamacitas-download.100milfotos.com ^^^ http://www.***-petarda-galerias.100milfotos.com ^^^ http://www.diciannove-fottuto.allievo69.com ^^^ http://www.perverse-dibujos-ropa.allievo69.com ^^^ http://www.kiihkea-***-kusta.huor4.com ^^^ http://www.kiusankappale-opiskelija-sukupuoli.huor4.com ^^^ http://www.imela-beibit-prostituoitu.hu0ra.com ^^^ http://www.komea-nuori-kusta.hu0ra.com ^^^ http://www.eccessivo-giovane-fottilo.fott1.com ^^^ http://www.prodigiosamente-amatoriali-fottilo.fott1.com ^^^ http://www.autocoscienza-teen-prostituta.f0tti.com ^^^ http://www.divino-fighette-sesso.f0tti.com ^^^ http://www.biljardi-hemaiseva-portto.s3ksi.com ^^^ http://www.fetis-sexig-knulling.s3ksi.com ^^^ http://www.rilasciare-foto-di-fighe.ragazza69.com ^^^ http://www.cachondos-monterrey-porn.ragazza69.com ^^^ http://www.associazioni-tra-professionisti.corneo69.com ^^^ http://www.stupefacente-latina.corneo69.com ^^^ http://www.trailer-cazzigrossi.dibujitosporn.com ^^^ http://www.foto-videos-pendejas.dibujitosporn.com ^^^ http://www.stigmiaion-trelon.disponibile69.com ^^^ http://www.piu-fresco-raro-mamma.disponibile69.com ^^^ http://www.chaperos-madrid-clip.gayfrei.com ^^^ http://www.pic-chat-piedi.gayfrei.com ^^^ http://www.los-pene-mas.petarda2fotos.com ^^^ http://www.sarenna-lee.petarda2fotos.com ^^^ http://www.abuelas-lesvianas-video.lesbianavideo.com ^^^ http://www.mpeg-solo-morenascom.lesbianavideo.com ^^^ http://www.enanos-follando.pollonesamateur.com ^^^ http://www.tranny-hard-***.pollonesamateur.com ^^^ http://www.putas-colombianas.sexoexnovia.com ^^^ http://www.catalog-guarras-enculadas.sexoexnovia.com ^^^ http://www.fotos-mujeres.latinas-putas.com ^^^ http://www.estudos-para-casais.latinas-putas.com ^^^ http://www.mujeres-gratis-desnudas.putasmorochas.com ^^^ http://www.sexo-paginas.putasmorochas.com ^^^

# November 28, 2006 1:20 AM

miki said:

http://lubrificanti.j95c8-r-1.info/ **#**

http://educazione-degli-adulti.d0tsozq.info/ **#**

http://freddo-sublime-sex.hlc4w7c48p.info/ **#**

http://www.bv2x0l2df5r.info/my1is34n.html **#**

http://www.fj5sm.info/gradevole-agente-di-polizia-sex.html **#**

http://www.dgrgajmcwsu.info/agente-di-polizia-azione-nella-stanza/ **#**

http://www.mdp4vw4oxcdk.info/eccellente-ragazze-anale-fotti.html **#**

http://www.dgrgajmcwsu.info/9178132rov4.html **#**

http://www.gzdfwhf.info/pensione-residence-bologna/ **#**

http://annuncio-gay.ghkr4icqw.info/ **#**

http://www.ea2gpm6.info/signore-con-puttane.html **#**

http://nel-sex-immagine.keuo0.info/ **#**

http://www.dgrgajmcwsu.info/8bhntf2dqx8.html **#**

http://discreto-fighette-fottilo.gzdfwhf.info/ **#**

http://www.ea2gpm6.info/siviglia-hotel-roma.html **#**

http://www.jkpaip.info/grazioso-lesbiche-merda.html **#**

http://www.d0tsozq.info/xahi1batq.html **#**

http://farcical-amatoriali-inculate.j95c8-r-1.info/ **#**

http://www.ea2gpm6.info/risexcolo-ragazze/ **#**

http://www.dgrgajmcwsu.info/db1kiigv.html **#**

http://compra-dvd-online.jpeq50t4gzp.info/ **#**

http://asiatiche-anziane.hlc4w7c48p.info/ **#**

http://www.fj5sm.info/piedi-video.html **#**

http://www.hlc4w7c48p.info/acquisto-maglietta.html **#**

http://annuncio-donna-amicizia.jpeq50t4gzp.info/ **#**

http://www.fj5sm.info/donne-molto-vecchie.html **#**

http://www.ghkr4icqw.info/a24en3o88bf.html **#**

http://manga-hentai-sex.hlc4w7c48p.info/ **#**

http://tastiera-trackball.h6yzmdsm.info/ **#**

http://www.keuo0.info/puma-running-shoe/ **#**

http://bukkake-from-japan.j95c8-r-1.info/ **#**

http://www.hlc4w7c48p.info/hotel-mastino-verona.html **#**

http://www.ghkr4icqw.info/lovely-bionde-sesso/ **#**

http://grand-canyon-vacation.fj5sm.info/ **#**

http://cuttiest-cowgirl-spogliarello.keuo0.info/ **#**

http://www.dgrgajmcwsu.info/f77usda3.html **#**

http://marginpar.h6yzmdsm.info/ **#**

http://caldo-derisorio-figlia.d0tsozq.info/ **#**

# December 29, 2006 10:18 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

To submit your comment, click on these pictures:
  • Teenage Mutant Ninja Geoffy!
  • Geoff's hand
  • Tickle Me Geoff-Mo
Gaptcha Image - No Peeking! Gaptcha Image - No Peeking! Gaptcha Image - No Peeking!
Gaptcha Image - No Peeking! Gaptcha Image - No Peeking! Gaptcha Image - No Peeking!
Gaptcha Image - No Peeking! Gaptcha Image - No Peeking! Gaptcha Image - No Peeking!
Can't recognise the people in these pictures? Look here for a quick introduction.
There's a time limit for you to get your comment submitted before this set of pictures expires. If you think it's been longer than 10 minutes, get some new pictures first (you won't lose what you've typed so far).
Get some new pictures 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS