const string values

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

    const string values

    I'm fairly new to the whole .NET and OOP programming so bear with me.

    In the past if I wanted to have constants defined that I could use in my
    code, I'd include them either the file or as an include file. But with .NET
    and OOP, there has to be a different way than just including them in each of
    the classes I want to utilize them in.

    Here's an example of what's in the definition of each of the classes I need
    to use it in.

    private const string LAUNCH_TEXT = "Welcome to the Program";
    private const string QUIT_TEXT = "Goodbye";

    Can this be put into it's own file and then "included" with each class I
    want to use it in? What would be the proper way? Thanks!


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: const string values

    ESPN Lover (happy 25th),

    What you would do in this situation is to make the constants public, and
    then reference the assembly from other assemblies where you want to use it.
    Then, you would just have to use your type name and the constant name, like
    so:

    // In the assembly that has the value defined.
    public class Values
    {
    public const string Value1 = "Value1";
    }

    // In the assembly in a class where you want to get Value1 and have a
    reference to where Values is kept.
    string value1 = Values.Value1;

    Also, if you want to expose these values, you should abide by the naming
    conventions for publically accessible members.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "ESPN Lover" <espn@lover.com > wrote in message
    news:%23cVKhkis EHA.160@TK2MSFT NGP11.phx.gbl.. .[color=blue]
    > I'm fairly new to the whole .NET and OOP programming so bear with me.
    >
    > In the past if I wanted to have constants defined that I could use in my
    > code, I'd include them either the file or as an include file. But with
    > .NET
    > and OOP, there has to be a different way than just including them in each
    > of
    > the classes I want to utilize them in.
    >
    > Here's an example of what's in the definition of each of the classes I
    > need
    > to use it in.
    >
    > private const string LAUNCH_TEXT = "Welcome to the Program";
    > private const string QUIT_TEXT = "Goodbye";
    >
    > Can this be put into it's own file and then "included" with each class I
    > want to use it in? What would be the proper way? Thanks!
    >
    >[/color]


    Comment

    • Jay B. Harlow [MVP - Outlook]

      #3
      Re: const string values

      ESPN,
      Due to Encapsulation I would put the constants specific to a class in that
      class, if a constant was shared between classes, then I consider putting
      them in a base class. If the "constants" happen to be integers, I would
      consider creating an Enum for related constants...

      Is LAUNCH_TEXT the same for each class, or does it vary?

      If LAUNCH_TEXT varies then I would consider a polymorphic constant function
      (or readonly property).

      The base class would declare LAUNCH_TEXT as an abstract function or readonly
      property, each derived class would implement the function or property
      returning a literal.

      The base class could then use LAUNCH_TEXT much like a constant.

      If LAUNCH_TEXT is the same, then I would use a base class or a "static"
      class that groups related constants...

      Hope this helps
      Jay

      "ESPN Lover" <espn@lover.com > wrote in message
      news:%23cVKhkis EHA.160@TK2MSFT NGP11.phx.gbl.. .[color=blue]
      > I'm fairly new to the whole .NET and OOP programming so bear with me.
      >
      > In the past if I wanted to have constants defined that I could use in my
      > code, I'd include them either the file or as an include file. But with
      > .NET
      > and OOP, there has to be a different way than just including them in each
      > of
      > the classes I want to utilize them in.
      >
      > Here's an example of what's in the definition of each of the classes I
      > need
      > to use it in.
      >
      > private const string LAUNCH_TEXT = "Welcome to the Program";
      > private const string QUIT_TEXT = "Goodbye";
      >
      > Can this be put into it's own file and then "included" with each class I
      > want to use it in? What would be the proper way? Thanks!
      >
      >[/color]


      Comment

      • Wes

        #4
        Re: const string values

        Hello ESPN,

        You could create, for example, another class called constants.

        public class Constants
        {
        public const string LAUNCH_TEXT = "Welcome to the Program";
        public const string QUIT_TEXT = "Goodbye";
        }

        Then wherever you want to use them just use Constants.LAUNC H_TEXT, etc. Also if you want to be sure that no can create an instance of this class for .Net Framework 1.1 and lower just add a dummy private constructor and mark the class sealed, but in .Net Framework 2.0 there is a static keyword to handle this.

        HTH
        Wes Haggard

        [color=blue]
        > I'm fairly new to the whole .NET and OOP programming so bear with me.
        >
        > In the past if I wanted to have constants defined that I could use in
        > my code, I'd include them either the file or as an include file. But
        > with .NET and OOP, there has to be a different way than just including
        > them in each of the classes I want to utilize them in.
        >
        > Here's an example of what's in the definition of each of the classes I
        > need to use it in.
        >
        > private const string LAUNCH_TEXT = "Welcome to the Program"; private
        > const string QUIT_TEXT = "Goodbye";
        >
        > Can this be put into it's own file and then "included" with each class
        > I want to use it in? What would be the proper way? Thanks!
        >[/color]

        Comment

        • ESPN Lover

          #5
          Re: const string values

          Nic, thanks! This is what I was looking for :-)

          "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in
          message news:up6QetisEH A.2460@TK2MSFTN GP09.phx.gbl...[color=blue]
          > ESPN Lover (happy 25th),
          >
          > What you would do in this situation is to make the constants public,[/color]
          and[color=blue]
          > then reference the assembly from other assemblies where you want to use[/color]
          it.[color=blue]
          > Then, you would just have to use your type name and the constant name,[/color]
          like[color=blue]
          > so:
          >
          > // In the assembly that has the value defined.
          > public class Values
          > {
          > public const string Value1 = "Value1";
          > }
          >
          > // In the assembly in a class where you want to get Value1 and have a
          > reference to where Values is kept.
          > string value1 = Values.Value1;
          >
          > Also, if you want to expose these values, you should abide by the[/color]
          naming[color=blue]
          > conventions for publically accessible members.
          >[/color]


          Comment

          • tribal

            #6
            Re: const string values

            [color=blue]
            >-----Original Message-----
            >ESPN Lover (happy 25th),
            >
            > What you would do in this situation is to make the[/color]
            constants public, and[color=blue]
            >then reference the assembly from other assemblies where[/color]
            you want to use it.
            [color=blue]
            >Then, you would just have to use your type name and the[/color]
            constant name, like
            [color=blue]
            >so:
            >
            >// In the assembly that has the value defined.
            >public class Values
            >{
            > public const string Value1 = "Value1";
            >}
            >
            >// In the assembly in a class where you want to get[/color]
            Value1 and have a[color=blue]
            >reference to where Values is kept.
            >string value1 = Values.Value1;
            >
            > Also, if you want to expose these values, you should[/color]
            abide by the naming[color=blue]
            >conventions for publically accessible members.[/color]



            If this assembly was of a large size with lots of code
            and the constants were needed in more than one assembly
            would this approach be feasible..?

            Can a class such as Features for example be more
            suitable here which would detail the characteristic
            features of a dog with constants such as
            HearingInDecibe ls,SmellFromDis tance etc

            I would appreciate your thoughts as this would broaden my
            perspective of thinking in code

            Comment

            • Senthil

              #7
              Re: const string values

              hi,

              You need to be beware of one thing though. If the constant is defined in
              another assembly, the actual constant value gets embedded in the assembly
              using the constant. So if you were to change the constant to some other
              value, it wouldn't get reflected in the other assembly, unless you recompile
              it.

              Comment

              Working...