Constants

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

    #1

    Constants

    OK, my brain has turned to mush on this one.

    I want to implement some constant integers that can be used by several
    classes in the same namespace. i.e.

    namespace MySpace
    {
    const int THEBOXWIDTH = 24;
    const int THEBOXHEIGHT = 48;

    class A
    {
    private int theHeight = THEBOXHEIGHT;
    }

    class B
    {
    private int theHeight = THEBOXHEIGHT;
    }
    }

    How can I achieve this? Am I going to have to use enums?

    Thanks in advance

    Mike



  • Lasse Vågsæther Karlsen

    #2
    Re: Constants

    Publicjoe wrote:[color=blue]
    > OK, my brain has turned to mush on this one.
    >
    > I want to implement some constant integers that can be used by several
    > classes in the same namespace. i.e.
    >
    > namespace MySpace
    > {
    > const int THEBOXWIDTH = 24;
    > const int THEBOXHEIGHT = 48;[/color]
    <snip>

    Constants must be placed inside a class.

    --
    Lasse Vågsæther Karlsen

    mailto:lasse@vk arlsen.no
    PGP KeyID: 0x2A42A1C2

    Comment

    • Tim Wilson

      #3
      Re: Constants

      You could just wrap them in another class.

      internal class Constants
      {
      public const int THEBOXWIDTH = 24;
      public const int THEBOXHEIGHT = 48;
      }

      public class A
      {
      private int theHeight = Constants.THEBO XHEIGHT;
      }

      public class B
      {
      private int theHeight = Constants.THEBO XHEIGHT;
      }

      --
      Tim Wilson
      ..NET Compact Framework MVP

      "Publicjoe" <mike@publicjoe .co.uk> wrote in message
      news:usjkyQ3FGH A.532@TK2MSFTNG P15.phx.gbl...[color=blue]
      > OK, my brain has turned to mush on this one.
      >
      > I want to implement some constant integers that can be used by several
      > classes in the same namespace. i.e.
      >
      > namespace MySpace
      > {
      > const int THEBOXWIDTH = 24;
      > const int THEBOXHEIGHT = 48;
      >
      > class A
      > {
      > private int theHeight = THEBOXHEIGHT;
      > }
      >
      > class B
      > {
      > private int theHeight = THEBOXHEIGHT;
      > }
      > }
      >
      > How can I achieve this? Am I going to have to use enums?
      >
      > Thanks in advance
      >
      > Mike
      >
      >
      >[/color]


      Comment

      • Publicjoe

        #4
        Re: Constants

        Thanks, works a treat.

        Mike

        "Tim Wilson" <TIM(UNDERSCORE )WILSON(AT)ROGE RS(PERIOD)COM> wrote in message
        news:el$paa3FGH A.3120@TK2MSFTN GP10.phx.gbl...[color=blue]
        > You could just wrap them in another class.
        >
        > internal class Constants
        > {
        > public const int THEBOXWIDTH = 24;
        > public const int THEBOXHEIGHT = 48;
        > }
        >
        > public class A
        > {
        > private int theHeight = Constants.THEBO XHEIGHT;
        > }
        >
        > public class B
        > {
        > private int theHeight = Constants.THEBO XHEIGHT;
        > }
        >
        > --
        > Tim Wilson
        > .NET Compact Framework MVP
        >
        > "Publicjoe" <mike@publicjoe .co.uk> wrote in message
        > news:usjkyQ3FGH A.532@TK2MSFTNG P15.phx.gbl...[color=green]
        >> OK, my brain has turned to mush on this one.
        >>
        >> I want to implement some constant integers that can be used by several
        >> classes in the same namespace. i.e.
        >>
        >> namespace MySpace
        >> {
        >> const int THEBOXWIDTH = 24;
        >> const int THEBOXHEIGHT = 48;
        >>
        >> class A
        >> {
        >> private int theHeight = THEBOXHEIGHT;
        >> }
        >>
        >> class B
        >> {
        >> private int theHeight = THEBOXHEIGHT;
        >> }
        >> }
        >>
        >> How can I achieve this? Am I going to have to use enums?
        >>
        >> Thanks in advance
        >>
        >> Mike
        >>
        >>
        >>[/color]
        >
        >[/color]


        Comment

        Working...