Newbie question -- is there a "#include" in csharp?

  • Thread starter Thread starter Dom
  • Start date Start date
D

Dom

Teaching myself cSharp. In other languages (c++, etc), I generally
put important parameters into a single file. Then, if a module
requires those parameters, I "include" them in the module.

I have that same need in c#, but how is it done? All I can think of
is: Create a class with just consts, no attributes and no methods.
Then I use the const that I need with the namespace.class.const as a
field -- for example, nsParameters.Parameters.INITIALSTATE.

Is there a better way?
 
Hi,

| Teaching myself cSharp. In other languages (c++, etc), I generally
| put important parameters into a single file. Then, if a module
| requires those parameters, I "include" them in the module.

Nothing like that in C# though.

| I have that same need in c#, but how is it done? All I can think of
| is: Create a class with just consts, no attributes and no methods.
| Then I use the const that I need with the namespace.class.const as a
| field -- for example, nsParameters.Parameters.INITIALSTATE.
|
| Is there a better way?
|

Not really, a class with such a characteristics is the way to go. You could
also declare it as sealed to prevent derivation
 
Teaching myself cSharp. In other languages (c++, etc), I generally
put important parameters into a single file. Then, if a module
requires those parameters, I "include" them in the module.

I have that same need in c#, but how is it done? All I can think of
is: Create a class with just consts, no attributes and no methods.
Then I use the const that I need with the namespace.class.const as a
field -- for example, nsParameters.Parameters.INITIALSTATE.

Is there a better way?

Well, if they really are parameters to your program that could change
someday, you might want to look into using configuration files. The
parameters are stored on a .config file rather than hard-coded into a
class in memory.

I very rarely have reason to code something like this into a global
class. Either the constant / value logically belongs with some class
that exists for some other reason, or I put it in a config file.
 
sorry, c# does not support code reuse.

I would go back to ASP classic
 
Teaching myself cSharp. In other languages (c++, etc), I generally
put important parameters into a single file. Then, if a module
requires those parameters, I "include" them in the module.

I have that same need in c#, but how is it done? All I can think of
is: Create a class with just consts, no attributes and no methods.
Then I use the const that I need with the namespace.class.const as a
field -- for example, nsParameters.Parameters.INITIALSTATE.

Is there a better way?

I usually create a class with public constants to do something similar
 
Dom said:
Teaching myself cSharp. In other languages (c++, etc), I generally
put important parameters into a single file. Then, if a module
requires those parameters, I "include" them in the module.

I have that same need in c#, but how is it done? All I can think of
is: Create a class with just consts, no attributes and no methods.
Then I use the const that I need with the namespace.class.const as a
field -- for example, nsParameters.Parameters.INITIALSTATE.

Is there a better way?

I'd have to know more about what you're trying to accomplish in order to
give you a better answer. Database connection strings, for example, I
often put into a web.config file for ASP.NET applications, whereas for a
Windows application, I generally save them in a configuration file.
Here is a link to learn how to work with Settings, which I actually just
learned myself, yesterday.
http://msdn2.microsoft.com/en-us/library/aa730869(VS.80).aspx

When I was heavy in C and C++, I would often #define constants for
things such as memory locations, bits, etc. in a header file. I really
haven't had much of a need for this in C#; however, I'd probably declare
stuff like this in a class as a const. This is stuff that I'd rarely,
if ever, want an end user to change, so a configuration file really
wouldn't suffice for those kinds of needs. If these constants are
something that you'd like to share between applications, it would be a
good idea to put them in their own project that you can reference
whenever you need them.

Again, I'm really just speculating here because I'm not 100% clear on
what you'd like to accomplish.

Hope that helps,
 
I'd have to know more about what you're trying to accomplish in order to
give you a better answer. Database connection strings, for example, I
often put into a web.config file for ASP.NET applications, whereas for a
Windows application, I generally save them in a configuration file.
Here is a link to learn how to work with Settings, which I actually just
learned myself, yesterday.http://msdn2.microsoft.com/en-us/library/aa730869(VS.80).aspx

When I was heavy in C and C++, I would often #define constants for
things such as memory locations, bits, etc. in a header file. I really
haven't had much of a need for this in C#; however, I'd probably declare
stuff like this in a class as a const. This is stuff that I'd rarely,
if ever, want an end user to change, so a configuration file really
wouldn't suffice for those kinds of needs. If these constants are
something that you'd like to share between applications, it would be a
good idea to put them in their own project that you can reference
whenever you need them.

Again, I'm really just speculating here because I'm not 100% clear on
what you'd like to accomplish.

Hope that helps,


I really have no immediate need for this. I'm teaching myself C#,
and, as I said, in virtually every language I have something like an
include statement, but I couldn't find it here.

What include gives me, is the following:
1. Values available across methods.
2. Values expressed as constants, so they can be used in a "switch"
statement.

Thanks for the link. I'm reading through it now.
 
I really have no immediate need for this. I'm teaching myself C#,
and, as I said, in virtually every language I have something like an
include statement, but I couldn't find it here.

What include gives me, is the following:
1. Values available across methods.

These become part of object state. If you want a single value, class-
wide, then declare it as a static class variable or class-level
constant. Since you want the value to be visible from multiple
methods, or visible to all methods in a class, there's no need for a
global constant... a class-level constant will do.

If you need a global constant, usually you'll find that it logically
belongs with some class that implements some related functionality.
Put it there, then, and make it "public".
2. Values expressed as constants, so they can be used in a "switch"
statement.

I would use an "enum" type for this. Again, declared at the class
level (or, rarely, at the namespace level outside a class). Most times
these things logically belong with a class, or in a particular
namespace.

It's very rare that I have to create a class just to hold constants.

There are so many opportunities for grouping program logic and values
in C#--namespaces and classes--that you can almost always find a place
to neatly situate a constant or an enumeration. It's rare that you
have one just floating "in space" without any related classes or
methods.
 
C# is not a language, it is an infestation

dipshits in redmond robbed peter to pay paul; and I call for the death
of C#
 
In place of #include, you use "using" in C#.

No. #include is completely different from "using".

#include includes the contents of a file into the token stream for the
compiler.

"using" (in C#) imports the symbols of a namespace into the global
namespace.

My 2 cents...
Paule
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top