CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Apr 2005
    Posts
    1,828

    Constant Members

    Where do we initialize constant members of class in C#?? In C++ we use Data Members Initialization List. But in C# do we have such concept??

  2. #2
    Join Date
    Oct 2005
    Location
    Islamabad, Pakistan
    Posts
    1,277

    Re: Constant Members

    Constants can either be declared inside a class or a function. If inside a function, the constant can only be used inside that function. If in a class the constant can be used from anywhere (if marked as public).

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Constant Members

    They are initialized when declared.

    Code:
    public class myClass
    {
      public const string ConstantStrValue = "some const string";
      public const int ConstantIntValue = 100;
    }
    Last edited by Arjay; October 17th, 2007 at 01:19 PM.

  4. #4
    Join Date
    May 2007
    Posts
    1,546

    Re: Constant Members

    Quote Originally Posted by Arjay
    They must be initialized when declared.

    Code:
    public class myClass
    {
      public const string = "some const string";
      public const int = 100;
    }
    Just a little copy-edit

    Also, you can't declare constants inside a function, not that i know of anyway.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  5. #5
    Join Date
    Oct 2007
    Posts
    17

    Re: Constant Members

    You can declare constants in a class method such as the constructor however I see no reason to do so and think of it as a bad practice but it is allow see the .NET documentation for the constant keyword in C# they have an example of declaring a method level constant.

    All other constants that are to be utilized in the class are declared a constant field as pointed out before.

    They do not have to be marked as public and can be marked as private.

    It would be inappropriate to create a public instance of a constant field if it were to not be utilized outside of the object's methods.

    Only mark constants on values that you be 100% certain will never change during the duration of your applications development life cycle. For all other things that are considered a constant but could be changed by a configuration setting use the web.config or app.config file and create a key to hold this value that way if they ever are changed during setup you will not have to recompile your application.

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Constant Members

    Quote Originally Posted by Mutant_Fruit
    Just a little copy-edit

    Also, you can't declare constants inside a function, not that i know of anyway.
    Thanks, I fixed it.

  7. #7
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Constant Members

    Just small but important addition:

    1. Constats are initialized at compile-time, so they can be only of types which value are know at this time, like numbers and strings. You cannot declare constant of reference type, you have to use static readonly rather.

    2. Constants are not referenced, but copied directly to place where they are used. If you change a constant value, you have to rebuild the whole solution and all project where is is used. So be careful of using constant, especialy as public members of class libraries.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  8. #8
    Join Date
    May 2004
    Location
    Greenville, NC
    Posts
    193

    Re: Constant Members

    Or, if you want to initialize the constants at runtime you can use the readonly attribute and initialize the constants in the constructor.

    http://msdn2.microsoft.com/en-us/lib...b7(VS.71).aspx
    Jason Isom
    .NET 2.0 / VS2005 Developer

  9. #9
    Join Date
    Dec 2006
    Posts
    203

    Re: Constant Members

    Just a minor side note, but I've always learned that constants should be written in full caps.
    Sincerely,

    Martin Svendsen

  10. #10
    Join Date
    Oct 2007
    Posts
    17

    Re: Constant Members

    Actually that is the old way of doing things not to say that it is wrong but with the VS IDE there is no reason really to write constants in all caps since you can easily mouse over the type and see that it is a constant.

    All the .NET framework documentation marks constants just like any other field in camel case example firstName.

    However it all boils down to how your team is use to identifying coding standards.

    As long as it is consistent there is nothing wrong with all caps separated by underscores. I don't like looking at code this way but as long as the team is consistent then it is fine.

  11. #11
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Constant Members

    I prefer constants in Pascal case. I generally prefix my field names with an underscore as well since I like to distinguish between a local variable and a class field (ie. _firstName). I find writing 'this.' everywhere effects my sense of style. My attitute is probably a holdover from C++, but I feel since the field is already a member of 'this' class, there is no need to explicitly write 'this.' everywhere when referencing a class member. But this is strickly my preferences.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured