-
March 23rd, 2005, 07:07 AM
#1
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,
-
March 23rd, 2005, 07:22 AM
#2
Re: header file of constants
You can make a namespace and declare enums that contain values inside it.
-
March 23rd, 2005, 07:23 AM
#3
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.
-
March 23rd, 2005, 07:24 AM
#4
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.
-
March 23rd, 2005, 07:30 AM
#5
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
-
March 23rd, 2005, 07:41 AM
#6
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.
-
March 23rd, 2005, 07:59 AM
#7
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.
-
March 23rd, 2005, 10:32 AM
#8
Re: header file of constants
Finally, goodbye 'C' hello object orientation.
Darwen.
-
March 24th, 2005, 05:33 AM
#9
Re: header file of constants
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?
-
March 24th, 2005, 05:39 AM
#10
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
-
March 24th, 2005, 06:18 AM
#11
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.
-
March 24th, 2005, 03:40 PM
#12
Re: header file of constants
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?
-
March 24th, 2005, 04:48 PM
#13
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|