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 |
| using MyDll; |
| Imports MYDLL.PeopleConstants |
| using People = MyDLL.PeopleConstants; |

| // 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"; |
| Ask a Question, or give your feedback on my articles or products by going to the KnowDotNet Forum or by clicking on My Blog. | ![]() |