Do I need to Convert with Convert.ToInt32(session("myNumber")) ?

Discussion in 'ASP .Net' started by Andreas Klemt, Jul 23, 2003.

  1. Hello,
    I have this

    session("myNumber") = 888
    Dim intNumber As Integer

    a) intNumber = session("myNumber")
    b) intNumber = Convert.ToInt32(session("myNumber"))


    What has better performance and what should I do?

    Thanks,
    Andreas
    Andreas Klemt, Jul 23, 2003
    #1
    1. Advertising

  2. Andreas Klemt

    Karl Seguin Guest

    You are clearly not programming with Option Strict on. You should turn it
    on and then you wouldn't have a choice but to select the most performant and
    safest method.

    b) Explicietly converting is the best way to do it, spefically from a
    perfromance point of view...otherwise you are late-binding...which is bad.

    Alternative (and better) ways to write b) would be using cint() or
    DirectCast...both of which should be faster than Convert.ToInt32 and are
    recommeded by Microsoft. You should always explicetly convert. Session,
    Application, Context, DataReader and DataRow objects are all great examples
    of places that return object, but you are likely implicetly assigning to
    ints or strings.

    Also, some error handling would be nice ,no?

    dim intNumber as integer
    try
    intNumber = cint(session("myNumber"))
    catch ex as FormatException
    'maybe you wanna redirect? Maybe you wanna set a default value
    catch ex as OverflowException
    'maybe you wanna redirect? Maybe you wanna set a default value
    end try

    Karl


    "Andreas Klemt" <> wrote in message
    news:...
    > Hello,
    > I have this
    >
    > session("myNumber") = 888
    > Dim intNumber As Integer
    >
    > a) intNumber = session("myNumber")
    > b) intNumber = Convert.ToInt32(session("myNumber"))
    >
    >
    > What has better performance and what should I do?
    >
    > Thanks,
    > Andreas
    >
    >
    Karl Seguin, Jul 23, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas Klemt
    Replies:
    4
    Views:
    511
    John Saunders
    Jul 24, 2003
  2. Jeff Smythe
    Replies:
    3
    Views:
    1,228
    Jeff Smythe
    Jan 2, 2004
  3. sck10
    Replies:
    4
    Views:
    15,988
    daniel
    Sep 3, 2006
  4. =?Utf-8?B?Um9iSEs=?=
    Replies:
    4
    Views:
    5,254
    =?Utf-8?B?Um9iSEs=?=
    Apr 11, 2007
  5. Jazzis
    Replies:
    2
    Views:
    234
    Jazzis
    Sep 23, 2003
Loading...

Share This Page