Professional Web Applications Themes

Do I need to Convert with Convert.ToInt32(session("myNumber")) ? - ASP.NET General

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...

  1. #1

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

    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 Guest

  2. #2

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

    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" <aklemt68hotmail.com> wrote in message
    news:ujlU9nSUDHA.2008TK2MSFTNGP11.phx.gbl...
    > 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 Guest

Similar Threads

  1. convert XML "values" to be passed as CDATA into Flash
    By elbolo in forum Macromedia Flash Data Integration
    Replies: 1
    Last Post: January 4th, 05:05 PM
  2. How to "Convert To Template" in custom control
    By PeterKellner in forum ASP.NET Building Controls
    Replies: 0
    Last Post: August 10th, 04:05 PM
  3. Stop popup " do you wish to convert existing data"
    By studiopeg in forum Macromedia Freehand
    Replies: 0
    Last Post: August 18th, 02:40 PM
  4. AI CS does not "Convert to Working Space"
    By Andrew_Colfelt@adobeforums.com in forum Adobe Illustrator Macintosh
    Replies: 0
    Last Post: April 29th, 12:09 AM
  5. Replies: 1
    Last Post: July 20th, 08:30 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139