Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Public Constants 1

Status
Not open for further replies.

Mongr1l

Programmer
Mar 6, 2006
179
US
I'm developing a VB.NET solution. So far, I'm doing all of my development under one project. I'm currently contemplating modularizing by the solution by maybe doing any further develepment on seperate projects.

Here's my question: There are certain constants I want accessible to all the projects, such as connections strings.

I don't want to have to keep re-declaring whenever I create a new class or new form.

Where do I declare it?

If you say, "solution namespace", how do I access that?

I've already know if declare them in a class, I have to use the complete namespace of that class to access the public constant in another class.

Thoughts?

- mongril
 
I have a project called General. It is a DLL. In side of it, there are numerous name spaces. In the case of Constants, I have a class called "GlobalConstants". In that class I declare and document each of the constants. I also enclose all of those constants in a name space. For example:

Code:
namespace CorporateIdentifier.General
  public Class GlobalConstants

    '''<summary>this constant is used for something</summary>
    public const THINGY = 2

  end class

end namespace

Then, from the other projeect I add a reference to the General.DLL (or a project reference). Then I can access CorporateIdentifier.General.GlobalConstants.THINGY or I can add "Imports CorporateIdentifier.General.GlobalConstants" to the top of the class that I want to use the constants in and then I can directly access THINGY.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Thanks, Rick. That's really what I was looking for, which is a "best practice" approach.


- mongril
 
Add a new class library prokect in your solution. Do something like:

Public Class Class1

Public Shared ConnStr as Sring = "...."

End Class

The Shared is required so you do not have to create an instance of that class. Also prefer not to you the Const, because it is easy to retrieve the string from the dll.

Just add a reference to this project or to this dll, and then use it like:

ClassLibrary1.Class1.ConnStr

You also refered to the namespace... You can clear the root namespace from the class' properties, to eliminate the 'ClassLibrary1'

Hope this helps!
 
Actually, Tip, your comment on Shared accessability answered another one of my questions, which was how do I use a Public or Friend without having to instantiate the class.

Also, it makes me feel more secure when there are points that experts agree on.


- mongril
 
Hi Rick,... I was editing-writing my post for more that 6 minutes! Wrote the half, then when went to the kitchen and when i remembered that i had a not completed my post... came back to complete. Hehe

I would not say it as a catch, because when i posted at last, you had already replied, same the mongirl and had even given you a star! Pheww Thank God it was not archived while i was posting. :)
 
constants are implicity shared (so sayeth the msdn)
 
Yes it may be, but i use Public Shared instead of Public Const. The reason is that the enctyption (protecting .net assemblies) is a lot better.
(The equivalent of shared in C# is static)
 
I don't understand what encryption you mean. If you need to encrypt something then an obfuscator will encrypt the constants too.
If you don't want to use a constant, then at least use a readonly porperty. Your Public Shared variable just ignores the whole reason for using a constant in the first place - the value doesn't change - and so it should not be changeable.
 
Friend Class ProjectConstants

Private Shared m_numerOfBeersInASixPack = 6

Public Shared ReadOnly Property NumerOfBeersInASixPack() As Integer
Get
Return m_numerOfBeersInASixPack
End Get
End Property

End Class
 
Yes, i use an obfuscator... but to have better encryption you should not use Const expressions. I have a dll to protect and after obfuscation i could still see the queries and connection strings within it. The company prompted me to do that kind of change.

Your way could work! As i see you do not either use Const.
And one question, why private shared and not just private ?
 
I was wondering why to declare as shared, when there is a property (shared) to get the value of m_numerOfBeersInASixPack ..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top