Any substitutes for #include in C#?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm working of several projects that use many of the same constants and I
wanted to put them all into a single file and include them in all the
projects that need them so I can maintain them in one place. I assume C# has
some mechanism similar to the #include since it's so handy. Do you just
create a class that contains the definitions outside the projects in a common
structure then include the class in all the projects?

Any suggestions would be greatly appreciated.
 
Byron said:
I'm working of several projects that use many of the same constants and I
wanted to put them all into a single file and include them in all the
projects that need them so I can maintain them in one place. I assume C#
has
some mechanism similar to the #include since it's so handy. Do you just
create a class that contains the definitions outside the projects in a
common
structure then include the class in all the projects?

Any suggestions would be greatly appreciated.

One way to accomplish what you want is to place your reusable stuff in a
class library, then in projects that need it, add a reference to the
library.
 
You can include a source file in multiple projects by doing:
Project
Add Existing Item
browse to the file and select it
instead of doing Open, click the small arrow near it and click Link File
 
As a C/C++ programmer turned C# programmer I had the same question. One
think that I have often seen is to create a class named Globals (filename
Globals.cs) and put the information you're talking about in that class. You
could subsequently "compile" it into a class library if it's used in multiple
apps, or you could simply include it somewhere in your project.

After having seen this method used a couple of times, I came across
information endorsing this in a recently-published (March '05) book entitled
"Practical Guidelines and Best Practices for Microsoft Visual Basic and
Visual C# Developers." The following is a quote from the book:

"Use Globals for the type that contains all the global variables of the
current application. Global variables are implemented as static fields of
this type....

"Why: References to those variables are in the form Globals.VariableName and
are therefore more readable and easier to spot."

..ARN.
 
Back
Top