Professional Web Applications Themes

Best Practice......Const Vs. Hardcode - ASP.NET General

In most situations (unless you are dealing with interpreted code) constants will be stripped away by the compiler, leaving both of these functionally equivalent. If you are using it exactly once, then there really isn't a difference. If you use it more than once, then you should use the constant. If you want ease of modification, then you may also want to consider storing the value in web.config - a single place you can point administrators to for adjusting values that are application-wide and should be dynamic. -- Chris Jackson Software Engineer Microsoft MVP - Windows XP Windows XP Associate ...

  1. #1

    Default Re: Best Practice......Const Vs. Hardcode

    In most situations (unless you are dealing with interpreted code) constants
    will be stripped away by the compiler, leaving both of these functionally
    equivalent. If you are using it exactly once, then there really isn't a
    difference. If you use it more than once, then you should use the constant.
    If you want ease of modification, then you may also want to consider storing
    the value in web.config - a single place you can point administrators to for
    adjusting values that are application-wide and should be dynamic.

    --
    Chris Jackson
    Software Engineer
    Microsoft MVP - Windows XP
    Windows XP Associate Expert
    --
    "Rajeev Soni" <rajeev_cssrediffmail.com> wrote in message
    news:OKXr0m$XDHA.1940TK2MSFTNGP10.phx.gbl...
    Hi,
    In case of comparing the user input value with some constant fixed value...

    which is the best..

    1. Define a consting string
    private const string CONST_SOMEVALUE = "SomeValue";

    if (TextBox1.Text == CONST_SOMEVALUE)
    {
    ..................
    }

    OR

    2. Just use the value directly of comparing like
    if (TextBox1.Text == "SomeValue")
    {
    ..................
    }

    Is it that declaring constants is overhead, or it depends on number of the
    constants we need to declare?

    Rajeev


    Chris Jackson Guest

  2. #2

    Default Re: Best Practice......Const Vs. Hardcode

    Thanks Chris.

    "Chris Jackson" <chrisjmvps.org> wrote in message news:u0bl5sAYDHA.2620TK2MSFTNGP09.phx.gbl...
    > In most situations (unless you are dealing with interpreted code) constants
    > will be stripped away by the compiler, leaving both of these functionally
    > equivalent. If you are using it exactly once, then there really isn't a
    > difference. If you use it more than once, then you should use the constant.
    > If you want ease of modification, then you may also want to consider storing
    > the value in web.config - a single place you can point administrators to for
    > adjusting values that are application-wide and should be dynamic.
    >
    > --
    > Chris Jackson
    > Software Engineer
    > Microsoft MVP - Windows XP
    > Windows XP Associate Expert
    > --
    > "Rajeev Soni" <rajeev_cssrediffmail.com> wrote in message
    > news:OKXr0m$XDHA.1940TK2MSFTNGP10.phx.gbl...
    > Hi,
    > In case of comparing the user input value with some constant fixed value...
    >
    > which is the best..
    >
    > 1. Define a consting string
    > private const string CONST_SOMEVALUE = "SomeValue";
    >
    > if (TextBox1.Text == CONST_SOMEVALUE)
    > {
    > ..................
    > }
    >
    > OR
    >
    > 2. Just use the value directly of comparing like
    > if (TextBox1.Text == "SomeValue")
    > {
    > ..................
    > }
    >
    > Is it that declaring constants is overhead, or it depends on number of the
    > constants we need to declare?
    >
    > Rajeev
    >
    >
    Rajeev Soni Guest

Similar Threads

  1. #26050 [Opn->Bgs]: Unable to use CONST vars in other CONST vars
    By sniper@php.net in forum PHP Development
    Replies: 0
    Last Post: October 31st, 01:41 PM
  2. #26050 [NEW]: Unable to use CONST vars in other CONST vars
    By kris dot hofmans at pandora dot be in forum PHP Development
    Replies: 0
    Last Post: October 31st, 10:24 AM
  3. [PHP-DEV] const in parameters
    By Andi Gutmans in forum PHP Development
    Replies: 0
    Last Post: October 13th, 05:46 AM
  4. Hardcode user for helper.exe
    By Tim S. in forum ASP.NET General
    Replies: 0
    Last Post: June 26th, 09:16 PM

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