Where is best place to define hard coded values?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brett

    Where is best place to define hard coded values?

    If I have many hard coded values such as file paths, file names, timeouts,
    etc, where is the best place to define them? Meaning, in the case something
    needs changing for example, rather than running down all the subs or
    functions that may contain these values, I'd like one place to change them
    and have that changed reflected in the subs or functions that use those
    values. I'd like to avoid globals; keeping the values private to only those
    subs or functions using them.

    Thanks,
    Brett


  • Ken Tucker [MVP]

    #2
    Re: Where is best place to define hard coded values?

    Hi,

    I would make a variable that is private to the class or form.
    That way all the sub routines and functions can use it. It wont be
    accessable out side the form or class.

    Ken
    -----------
    "Brett" <no@spam.net> wrote in message
    news:%23UgQoegE FHA.2032@tk2msf tngp13.phx.gbl. ..
    If I have many hard coded values such as file paths, file names, timeouts,
    etc, where is the best place to define them? Meaning, in the case something
    needs changing for example, rather than running down all the subs or
    functions that may contain these values, I'd like one place to change them
    and have that changed reflected in the subs or functions that use those
    values. I'd like to avoid globals; keeping the values private to only those
    subs or functions using them.

    Thanks,
    Brett



    Comment

    • Brett

      #3
      Re: Where is best place to define hard coded values?

      Nice. Thanks.

      Brett
      "Ken Tucker [MVP]" <vb2ae@bellsout h.net> wrote in message
      news:Opmg%236gE FHA.560@TK2MSFT NGP15.phx.gbl.. .[color=blue]
      > Hi,
      >
      > I would make a variable that is private to the class or form.
      > That way all the sub routines and functions can use it. It wont be
      > accessable out side the form or class.
      >
      > Ken
      > -----------
      > "Brett" <no@spam.net> wrote in message
      > news:%23UgQoegE FHA.2032@tk2msf tngp13.phx.gbl. ..
      > If I have many hard coded values such as file paths, file names, timeouts,
      > etc, where is the best place to define them? Meaning, in the case
      > something
      > needs changing for example, rather than running down all the subs or
      > functions that may contain these values, I'd like one place to change them
      > and have that changed reflected in the subs or functions that use those
      > values. I'd like to avoid globals; keeping the values private to only
      > those
      > subs or functions using them.
      >
      > Thanks,
      > Brett
      >
      >
      >[/color]


      Comment

      • Brett

        #4
        Re: Where is best place to define hard coded values?

        There will be some shared variables mixed in. Meaning, I'll define
        variables in one class that will also be used in other classes or forms.
        For those shared variables, should I just create a class that only houses
        them? This provides one place to change them and have those changes cascade
        through out forms/classes that are using them.

        Brett

        "Ken Tucker [MVP]" <vb2ae@bellsout h.net> wrote in message
        news:Opmg%236gE FHA.560@TK2MSFT NGP15.phx.gbl.. .[color=blue]
        > Hi,
        >
        > I would make a variable that is private to the class or form.
        > That way all the sub routines and functions can use it. It wont be
        > accessable out side the form or class.
        >
        > Ken
        > -----------
        > "Brett" <no@spam.net> wrote in message
        > news:%23UgQoegE FHA.2032@tk2msf tngp13.phx.gbl. ..
        > If I have many hard coded values such as file paths, file names, timeouts,
        > etc, where is the best place to define them? Meaning, in the case
        > something
        > needs changing for example, rather than running down all the subs or
        > functions that may contain these values, I'd like one place to change them
        > and have that changed reflected in the subs or functions that use those
        > values. I'd like to avoid globals; keeping the values private to only
        > those
        > subs or functions using them.
        >
        > Thanks,
        > Brett
        >
        >
        >[/color]


        Comment

        • Hal Rosser

          #5
          Re: Where is best place to define hard coded values?


          "Brett" <no@spam.net> wrote in message
          news:%23UgQoegE FHA.2032@tk2msf tngp13.phx.gbl. ..[color=blue]
          > If I have many hard coded values such as file paths, file names, timeouts,
          > etc, where is the best place to define them? Meaning, in the case[/color]
          something[color=blue]
          > needs changing for example, rather than running down all the subs or
          > functions that may contain these values, I'd like one place to change them
          > and have that changed reflected in the subs or functions that use those
          > values. I'd like to avoid globals; keeping the values private to only[/color]
          those[color=blue]
          > subs or functions using them.
          >
          > Thanks,
          > Brett
          >
          >[/color]

          In situations like this - what do you think of including the file paths,
          file names, timeouts, and those hard-code values in a separate text file you
          can read at run-time.
          The program would not need to be modified when things change - just change
          the values in the file.


          Comment

          • Brett

            #6
            Re: Where is best place to define hard coded values?


            "Hal Rosser" <hmrosser@bells outh.net> wrote in message
            news:1DSPd.626$ k7.339@bignews2 .bellsouth.net. ..[color=blue]
            >
            > "Brett" <no@spam.net> wrote in message
            > news:%23UgQoegE FHA.2032@tk2msf tngp13.phx.gbl. ..[color=green]
            >> If I have many hard coded values such as file paths, file names,
            >> timeouts,
            >> etc, where is the best place to define them? Meaning, in the case[/color]
            > something[color=green]
            >> needs changing for example, rather than running down all the subs or
            >> functions that may contain these values, I'd like one place to change
            >> them
            >> and have that changed reflected in the subs or functions that use those
            >> values. I'd like to avoid globals; keeping the values private to only[/color]
            > those[color=green]
            >> subs or functions using them.
            >>
            >> Thanks,
            >> Brett
            >>
            >>[/color]
            >
            > In situations like this - what do you think of including the file paths,
            > file names, timeouts, and those hard-code values in a separate text file
            > you
            > can read at run-time.
            > The program would not need to be modified when things change - just change
            > the values in the file.[/color]

            Is this how it is normally done?

            This file would only need to be read the first time the program loads
            correct? It will also only be for globals right?

            Thanks,
            Brett


            Comment

            • Hal Rosser

              #7
              Re: Where is best place to define hard coded values?


              "Brett" <no@spam.net> wrote in message
              news:%23Hqz9ckE FHA.3992@tk2msf tngp13.phx.gbl. ..[color=blue]
              >
              > "Hal Rosser" <hmrosser@bells outh.net> wrote in message
              > news:1DSPd.626$ k7.339@bignews2 .bellsouth.net. ..[color=green]
              > >
              > > "Brett" <no@spam.net> wrote in message
              > > news:%23UgQoegE FHA.2032@tk2msf tngp13.phx.gbl. ..[color=darkred]
              > >> If I have many hard coded values such as file paths, file names,
              > >> timeouts,
              > >> etc, where is the best place to define them? Meaning, in the case[/color]
              > > something[color=darkred]
              > >> needs changing for example, rather than running down all the subs or
              > >> functions that may contain these values, I'd like one place to change
              > >> them
              > >> and have that changed reflected in the subs or functions that use those
              > >> values. I'd like to avoid globals; keeping the values private to only[/color]
              > > those[color=darkred]
              > >> subs or functions using them.
              > >>
              > >> Thanks,
              > >> Brett
              > >>
              > >>[/color]
              > >
              > > In situations like this - what do you think of including the file paths,
              > > file names, timeouts, and those hard-code values in a separate text file
              > > you
              > > can read at run-time.
              > > The program would not need to be modified when things change - just[/color][/color]
              change[color=blue][color=green]
              > > the values in the file.[/color]
              >
              > Is this how it is normally done?
              >
              > This file would only need to be read the first time the program loads
              > correct? It will also only be for globals right?
              >
              > Thanks,
              > Brett[/color]


              Can be used for anything that has to be hard-coded.
              Some do something like this - for Sales Tax Rate and the like
              - If the rate changes - someone can change the rate in the file - and no
              re-programming is required for a change in rates.
              A separate file for each piece of data is one way to do it -
              frinstance a file named "stateTaxRate.d at" can have one entry like ".075".
              When the state changes the rate - tell the user to change the file entry to
              ".085" - or whatever the new rate is.



              Comment

              • Brett

                #8
                Re: Where is best place to define hard coded values?


                "Hal Rosser" <hmrosser@bells outh.net> wrote in message
                news:UwWPd.5382 $u16.2089@bigne ws6.bellsouth.n et...[color=blue]
                >
                > "Brett" <no@spam.net> wrote in message
                > news:%23Hqz9ckE FHA.3992@tk2msf tngp13.phx.gbl. ..[color=green]
                >>
                >> "Hal Rosser" <hmrosser@bells outh.net> wrote in message
                >> news:1DSPd.626$ k7.339@bignews2 .bellsouth.net. ..[color=darkred]
                >> >
                >> > "Brett" <no@spam.net> wrote in message
                >> > news:%23UgQoegE FHA.2032@tk2msf tngp13.phx.gbl. ..
                >> >> If I have many hard coded values such as file paths, file names,
                >> >> timeouts,
                >> >> etc, where is the best place to define them? Meaning, in the case
                >> > something
                >> >> needs changing for example, rather than running down all the subs or
                >> >> functions that may contain these values, I'd like one place to change
                >> >> them
                >> >> and have that changed reflected in the subs or functions that use
                >> >> those
                >> >> values. I'd like to avoid globals; keeping the values private to only
                >> > those
                >> >> subs or functions using them.
                >> >>
                >> >> Thanks,
                >> >> Brett
                >> >>
                >> >>
                >> >
                >> > In situations like this - what do you think of including the file
                >> > paths,
                >> > file names, timeouts, and those hard-code values in a separate text
                >> > file
                >> > you
                >> > can read at run-time.
                >> > The program would not need to be modified when things change - just[/color][/color]
                > change[color=green][color=darkred]
                >> > the values in the file.[/color]
                >>
                >> Is this how it is normally done?
                >>
                >> This file would only need to be read the first time the program loads
                >> correct? It will also only be for globals right?
                >>
                >> Thanks,
                >> Brett[/color]
                >
                >
                > Can be used for anything that has to be hard-coded.
                > Some do something like this - for Sales Tax Rate and the like
                > - If the rate changes - someone can change the rate in the file - and no
                > re-programming is required for a change in rates.
                > A separate file for each piece of data is one way to do it -
                > frinstance a file named "stateTaxRate.d at" can have one entry like ".075".
                > When the state changes the rate - tell the user to change the file entry
                > to
                > ".085" - or whatever the new rate is.
                >[/color]
                I see. I suppose you could build that into the interface for the user to
                make changes as well right?

                Using the text files for many variables, will that be a bunch of parsing -
                instr(), mid(), perhaps regular expressions and the like in other words?
                They just don't seem like the most efficient method. I guess they could be
                seperated by chr(13) & chr(10).

                Would an XML file work better?

                Thanks,
                Brett


                Comment

                • Dennis

                  #9
                  Re: Where is best place to define hard coded values?

                  Options may be: XML Resource File, Small DataBase, Serialized Class, etc.

                  "Brett" wrote:
                  [color=blue]
                  >
                  > "Hal Rosser" <hmrosser@bells outh.net> wrote in message
                  > news:UwWPd.5382 $u16.2089@bigne ws6.bellsouth.n et...[color=green]
                  > >
                  > > "Brett" <no@spam.net> wrote in message
                  > > news:%23Hqz9ckE FHA.3992@tk2msf tngp13.phx.gbl. ..[color=darkred]
                  > >>
                  > >> "Hal Rosser" <hmrosser@bells outh.net> wrote in message
                  > >> news:1DSPd.626$ k7.339@bignews2 .bellsouth.net. ..
                  > >> >
                  > >> > "Brett" <no@spam.net> wrote in message
                  > >> > news:%23UgQoegE FHA.2032@tk2msf tngp13.phx.gbl. ..
                  > >> >> If I have many hard coded values such as file paths, file names,
                  > >> >> timeouts,
                  > >> >> etc, where is the best place to define them? Meaning, in the case
                  > >> > something
                  > >> >> needs changing for example, rather than running down all the subs or
                  > >> >> functions that may contain these values, I'd like one place to change
                  > >> >> them
                  > >> >> and have that changed reflected in the subs or functions that use
                  > >> >> those
                  > >> >> values. I'd like to avoid globals; keeping the values private to only
                  > >> > those
                  > >> >> subs or functions using them.
                  > >> >>
                  > >> >> Thanks,
                  > >> >> Brett
                  > >> >>
                  > >> >>
                  > >> >
                  > >> > In situations like this - what do you think of including the file
                  > >> > paths,
                  > >> > file names, timeouts, and those hard-code values in a separate text
                  > >> > file
                  > >> > you
                  > >> > can read at run-time.
                  > >> > The program would not need to be modified when things change - just[/color]
                  > > change[color=darkred]
                  > >> > the values in the file.
                  > >>
                  > >> Is this how it is normally done?
                  > >>
                  > >> This file would only need to be read the first time the program loads
                  > >> correct? It will also only be for globals right?
                  > >>
                  > >> Thanks,
                  > >> Brett[/color]
                  > >
                  > >
                  > > Can be used for anything that has to be hard-coded.
                  > > Some do something like this - for Sales Tax Rate and the like
                  > > - If the rate changes - someone can change the rate in the file - and no
                  > > re-programming is required for a change in rates.
                  > > A separate file for each piece of data is one way to do it -
                  > > frinstance a file named "stateTaxRate.d at" can have one entry like ".075".
                  > > When the state changes the rate - tell the user to change the file entry
                  > > to
                  > > ".085" - or whatever the new rate is.
                  > >[/color]
                  > I see. I suppose you could build that into the interface for the user to
                  > make changes as well right?
                  >
                  > Using the text files for many variables, will that be a bunch of parsing -
                  > instr(), mid(), perhaps regular expressions and the like in other words?
                  > They just don't seem like the most efficient method. I guess they could be
                  > seperated by chr(13) & chr(10).
                  >
                  > Would an XML file work better?
                  >
                  > Thanks,
                  > Brett
                  >
                  >
                  >[/color]

                  Comment

                  • Pete Wright

                    #10
                    Re: Where is best place to define hard coded values?

                    I'm surprised no-one here has mentioned App.config yet (or did I miss it?)

                    Right click on your project in the solution explorer and choose to add a new
                    item. From the list of items that you can add to the project, scroll down
                    and find the one called ApplicationConf iguration File. Select it, call it
                    app.config and click Open to add it to the file.

                    In the file you just added, drop in an <appSettings> section and in that add
                    your values, like this
                    <?xml version="1.0" encoding="utf-8" ?>

                    <configuratio n>

                    <appSettings>

                    <add key="DirectoryN ame" value="c:\test"/>

                    </appSettings>

                    </configuration>

                    Here I've addded in a single entry called DirectoryName, with a value of
                    "c:\test"

                    Now, to get at that, and obviously the whole mass of other values you want
                    to store, just go ahead and create a class with some shared properties on
                    it, like this.

                    Imports System.Configur ation

                    Public Class AppSettings

                    Public Shared ReadOnly Property DirectoryName() As String

                    Get

                    Return ConfigurationSe ttings.AppSetti ngs("DirectoryN ame")

                    End Get

                    End Property

                    End Class

                    If I ever want to pull a value out of that app.settings file now I just call
                    AppSettings.wha teverpropertyyo ucoded up

                    Hope that helps,


                    --
                    Pete Wright
                    Author of ADO.NET Novice to Pro for Apress




                    "Brett" <no@spam.net> wrote in message
                    news:%23UgQoegE FHA.2032@tk2msf tngp13.phx.gbl. ..[color=blue]
                    > If I have many hard coded values such as file paths, file names, timeouts,
                    > etc, where is the best place to define them? Meaning, in the case
                    > something needs changing for example, rather than running down all the
                    > subs or functions that may contain these values, I'd like one place to
                    > change them and have that changed reflected in the subs or functions that
                    > use those values. I'd like to avoid globals; keeping the values private
                    > to only those subs or functions using them.
                    >
                    > Thanks,
                    > Brett
                    >[/color]


                    Comment

                    • Jay B. Harlow [MVP - Outlook]

                      #11
                      Re: Where is best place to define hard coded values?

                      Pete,
                      Depending on the nature of the (original) coded values, I would consider
                      creating custom a configuration section in the app.config rather then
                      directly in appSettings, as this allows better encapsulation of the coded
                      values...

                      Create a custom configuration section via configSections and implementing
                      the System.Configur ation.IConfigur ationSectionHan dler. I would pattern the
                      custom section handler after DictionarySecti onHandler (support: add, remove,
                      clear) or use a class derived from DictionarySecti onHandler...

                      <navigation>
                      <add form="Form 1" type="namespace .form1, assembly" />
                      <add form="Form 2" type="namespace .form2, assembly" />
                      </navigation>

                      Then within your code you can use ConfigurationSe ttings.GetConfi g to return
                      the above section of the app.config. Depending on what you do in your
                      IConfigurationS ectionHandler.C reate method (I would create a HashTable) will
                      decide what GetConfig returns.

                      See the following on how to create new sections via the configSections
                      section.



                      and:


                      Also read about the System.Configur ation.Configura tionSettings class and
                      other classes in the System.Configur ation namespace.


                      Hope this helps
                      Jay



                      "Pete Wright" <peterjwright@g mail.com> wrote in message
                      news:%23Df1DiAF FHA.2784@TK2MSF TNGP14.phx.gbl. ..[color=blue]
                      > I'm surprised no-one here has mentioned App.config yet (or did I miss it?)
                      >
                      > Right click on your project in the solution explorer and choose to add a
                      > new item. From the list of items that you can add to the project, scroll
                      > down and find the one called ApplicationConf iguration File. Select it,
                      > call it app.config and click Open to add it to the file.
                      >
                      > In the file you just added, drop in an <appSettings> section and in that
                      > add your values, like this
                      > <?xml version="1.0" encoding="utf-8" ?>
                      >
                      > <configuratio n>
                      >
                      > <appSettings>
                      >
                      > <add key="DirectoryN ame" value="c:\test"/>
                      >
                      > </appSettings>
                      >
                      > </configuration>
                      >
                      > Here I've addded in a single entry called DirectoryName, with a value of
                      > "c:\test"
                      >
                      > Now, to get at that, and obviously the whole mass of other values you want
                      > to store, just go ahead and create a class with some shared properties on
                      > it, like this.
                      >
                      > Imports System.Configur ation
                      >
                      > Public Class AppSettings
                      >
                      > Public Shared ReadOnly Property DirectoryName() As String
                      >
                      > Get
                      >
                      > Return ConfigurationSe ttings.AppSetti ngs("DirectoryN ame")
                      >
                      > End Get
                      >
                      > End Property
                      >
                      > End Class
                      >
                      > If I ever want to pull a value out of that app.settings file now I just
                      > call AppSettings.wha teverpropertyyo ucoded up
                      >
                      > Hope that helps,
                      >
                      >
                      > --
                      > Pete Wright
                      > Author of ADO.NET Novice to Pro for Apress
                      > www.petewright.org
                      >
                      >
                      >
                      > "Brett" <no@spam.net> wrote in message
                      > news:%23UgQoegE FHA.2032@tk2msf tngp13.phx.gbl. ..[color=green]
                      >> If I have many hard coded values such as file paths, file names,
                      >> timeouts, etc, where is the best place to define them? Meaning, in the
                      >> case something needs changing for example, rather than running down all
                      >> the subs or functions that may contain these values, I'd like one place
                      >> to change them and have that changed reflected in the subs or functions
                      >> that use those values. I'd like to avoid globals; keeping the values
                      >> private to only those subs or functions using them.
                      >>
                      >> Thanks,
                      >> Brett
                      >>[/color]
                      >
                      >[/color]


                      Comment

                      Working...