Use Namespace Alias To Get Intellisense on Consts in DLLIntellisense in C# and VB.NET for DLL Constants | | Why can't I see public const in a DLL from a C# application? You can, but you need to use a Namespace Alias.
In an application I was writing, I needed to use a large number of constants, which are used as XML Element Names. The problem was remembering, and typing thiem without mispelling them. So I decided to include them as public constants in the DLL. So, I wrote a class within the DLL that has only constants. The following code shows the sample class, although the real one has well over 100 constants in it, but you can get the idea.
namespace MyDLL
{
/// <summary>
///
///
public class CodeMethods
{
} // class
public class PeopleConstants
{
public const string lastName = "LAST_NAME";
public const string firstName = "FIRST_NAME";
public const string ssn = "SSN";
public const string address = "CUR_ADDRESS";
public const string city = "CUR_CITY";
} // class
} // namespace
|
Now, in my application, where I wanted to reference the constants, I first used the following using directive.
In C# the Namespace was all that I could reference in the using directive. In VB.NET I could have used the following:
Imports MYDLL.PeopleConstants
|
But in C# you must use an Alias to see the constants and get intellisense as follows;
using People = MyDLL.PeopleConstants;
|
The picture displayed below shows the Intellisense resulting from the code previously discussed.

Contrast the two greatly different styles of coding. Not only are they greatly different in style, the use of aliasing negates the creation of the extra strings in memory.
// using aliasing
string lastName = People.lastName;
string firstName = People.firstName;
string ssn = People.ssn;
// contrast to this old fashioned error prone coding style
lastName = "LAST_NAME";
firstName = "FIRST_NAME";
ssn = "SSN";
|
I hope this little tip will make your coding more productive, readable, and maintainable.
Have you tried our newest product, Visual Class Organizer? You'll be amazed how easy it is to keep the code in your code windows organized. TRY IT FREE FOR 30 DAYS BY CLICKING HERE.
If you are developing in C# and haven't tried CSharpCompleter, you are wasting valuable time typing hundreds of braces {} daily needlessly. Try CSharpCompleter for 30 DAYS FREE.
| Ask a Question, or give your feedback on my articles or products by going to the KnowDotNet Forum or by clicking on My Blog. |  |
|