Best Practice......Const Vs. Hardcode

Ask a Question related to ASP.NET General, Design and Development.

  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_css@rediffmail.com> wrote in message
    news:OKXr0m$XDHA.1940@TK2MSFTNGP10.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. Similar Questions and Discussions

    1. ColdFusion_MX7 Web Appl.Const. Kit
      After successful installation of CFMX7 CD from Ben Forta's manual the following message appears: ...
    2. #26050 [Opn->Bgs]: Unable to use CONST vars in other CONST vars
      ID: 26050 Updated by: sniper@php.net Reported By: kris dot hofmans at pandora dot be -Status: Open +Status: ...
    3. #26050 [NEW]: Unable to use CONST vars in other CONST vars
      From: kris dot hofmans at pandora dot be Operating system: Linux 2.4.21 PHP version: 5CVS-2003-10-31 (dev) PHP Bug Type: ...
    4. [PHP-DEV] const in parameters
      Hi, Does anyone remember why function parameters support the const keyword? It doesn't change any of the behavior. It seems to me as-if this is...
    5. Hardcode user for helper.exe
      I have an ASP.Net app call another program, "helper.exe", on its same server. I want the helper.exe to always have a user = Administrator (as...
  3. #2

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

    Thanks Chris.

    "Chris Jackson" <chrisj@mvps.org> wrote in message news:u0bl5sAYDHA.2620@TK2MSFTNGP09.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_css@rediffmail.com> wrote in message
    > news:OKXr0m$XDHA.1940@TK2MSFTNGP10.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

Posting Permissions

  • You may not post new threads
  • You may 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