CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Mar 2003
    Posts
    97

    header file of constants

    Hi,


    I'm a new user of .Net, and my background is from c++.
    I want to add to my C# project a Header of global constants.

    It there a way to do it, except of declaring it in a class? (I want
    to do it like a header file of constants in C++)

    I opened a code file(constans.cs) and I tried to declare const
    and the compiler didn't approve it.


    Thanks in advanced,

  2. #2
    Join Date
    Apr 2002
    Location
    Egypt
    Posts
    2,210

    Re: header file of constants

    You can make a namespace and declare enums that contain values inside it.
    Hesham A. Amin
    My blog , Articles


    <a rel=https://twitter.com/HeshamAmin" border="0" /> @HeshamAmin

  3. #3
    Join Date
    Mar 2005
    Location
    DELHI
    Posts
    10

    Re: header file of constants

    well you can do this try something like this

    using System;

    namespace projectName
    {
    /// <summary>
    /// Summary description for constants
    /// </summary>
    public class Constants
    {
    internal const string ATT_TYPE_STRING = "AttType_String";
    internal const string ATT_TYPE_STRING1= "String";

    internal const string ATT_TYPE_DAY = "Typ_Day";
    internal const string ATT_TYPE_DAY_RESIDNAME = "Day";

    public Constants()
    {
    //
    // TODO: Add constructor logic here
    //

    }
    }
    }

    that's it.
    Work hard for a better tomorrow,
    Live life as if there is no tomorrow.

  4. #4
    Join Date
    Oct 2001
    Posts
    80

    Re: header file of constants

    As far as I know, you have to put it into a class, like this:
    Code:
      public class Globals
      {
        public static int HOURS_IN_DAY = 24;
        public static int MY_IQ = 150;
        
        private Globals() 
        {
        }
      }
    The contructor is private so noone can create an instance of the class, because that would be silly.

  5. #5
    Join Date
    Mar 2003
    Posts
    97

    Re: header file of constants

    Yeah, I know how to get it done like that (all example given).
    But I don't like the way u have to access the constants

    Code:
    Globals.HOURS_IN_DAY
    Constants.ATT_TYPE_STRING
    enum.nameofitem
    It be nice if you could just do

    Code:
    HOURS_IN_DAY
    ATT_TYPE_STRING
    nameofitem
    Without these constants being declared in the same class

  6. #6
    Join Date
    Oct 2001
    Posts
    80

    Re: header file of constants

    I personnally prefer the notation Globals.HOURS_IN_DAY. That way it is obvious where the variable is declared and initialized.

    I don't think you can do want you want to do, which - to my feeling - is logical, because C# is 100% object oriented.

  7. #7
    Join Date
    Dec 2003
    Location
    http://map.search.ch/zuerich.en.html
    Posts
    1,074

    Re: header file of constants

    Hi Zoltan,

    welcome to the world of real OO programming!

    Well, C# is closer than C++ anyway.
    Useful? Then click on (Rate This Post) at the top of this post.

  8. #8
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: header file of constants

    Finally, goodbye 'C' hello object orientation.

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  9. #9
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: header file of constants

    Quote Originally Posted by zoltan_ie
    Yeah, I know how to get it done like that (all example given).
    But I don't like the way u have to access the constants

    Code:
    Globals.HOURS_IN_DAY
    Constants.ATT_TYPE_STRING
    enum.nameofitem
    Why? Do you have a good reason? Are you so attached to C that cannot accept other ways?
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  10. #10
    Join Date
    Mar 2003
    Posts
    97

    Re: header file of constants

    I guess the less code you write the clearer it looks and the easier it is to read.

    Are you so attached to C that cannot accept other ways?
    nope, it just looked neater

  11. #11
    Join Date
    Nov 2004
    Location
    Poland
    Posts
    1,355

    Re: header file of constants

    I feel need to share my private opinion :

    I guess the less code you write the clearer it looks and the easier it is to read.
    Less code - maybe.
    But it not looks clearer and certainly it is not easier to read!
    if I see "ATT_TYPE_STRING" used somwhere after 1 year I wrote it - than it means nothing to me!

    But when I see "Constants.ATT_TYPE_STRING" than it is more self-explanatory - isn't it?

    And it prevents mixing different constans from different assembly...

    Best regards,
    Krzemo.

  12. #12
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: header file of constants

    Quote Originally Posted by zoltan_ie
    I guess the less code you write the clearer it looks and the easier it is to read.
    That is SO wrong. So wrong. Here are some examples from C, special for you .

    This is short enought. Is it easier to read?
    Code:
    void duff(register char *to, register char *from, register int count)
    {     
          register int n=(count+7)/8;
          switch(count%8){
          case 0: do{ *to++ = *from++;
          case 7:  *to++ = *from++;
          case 6: *to++ = *from++;
          case 5: *to++ = *from++;
          case 4: *to++ = *from++;
          case 3: *to++ = *from++;
          case 2: *to++ = *from++;
          case 1: *to++ = *from++;
                  }while( --n >0);
          }
    }
    Code:
    #include <stdio.h>
    int main()  
    {
        int a=3, b = 5;
        printf(&a["Ya!Hello! how is this? %s\n"], &b["junk/super"]);
        printf(&a["WHAT%c%c%c  %c%c  %c !\n"], 1["this"],
           2["beauty"],0["tool"],0["is"],3["sensitive"],4["CCCCCC"]);
        return 0;
    }
    What more?
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  13. #13
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: header file of constants

    Are you going for a prize in the obfuscation awards Cilu *laugh*.

    Actually I think it's widely recognised these days that readability is desired over just about everything. Even speed to a certain degree. I've always said that if an application has unreadable code then that's worse than having no application at all.

    There are other advantages of course : it eliminates conflicts, which you could potentially get around using namespaces of course but everything really should be wrapped up in a class of some form.

    At any rate : C# won't allow it and that's that. Because everything in C# is an object (even basic types, which C++ doesn't have) - and therefore conceptually you can't have a 'floating' value which doesn't belong to an object of some form : be it a class, struct or enum.

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

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