class Variables
private static string[] paths;
private static object syncRoot = new object();
internal static string[] Paths {
get {
if (paths == null) {
lock(syncRoot) {
if (paths == null) {
ArrayList pathsBuilder = new ArrayList();
... add paths to pathsBuilder
paths = (string[])pathsBuilder.ToArray(typeof(string));
}
}
}
return paths;
}
}
}
Key points:
1. use private variable for storage and public/internal read-only
property.
2. Use double-check locking when generating the value the first
time--check if it's null, if so lock a sync object, then check again.
3. Don't declare public/internal/protected methods as accepting or
returning an ArrayList if it's possible to avoid. In your case you
only need the arraylist when it's building and from then on the array
is static so use string[] instead. It's always safer to use typed
arrays as parameters/returns from public/internal/protected members.
In 2.0 you can used generics so it's a lot better and List<stringis
ok, but ArrayList is bad 'cause there's no guaranetee what the
ArrayList will have. besides string[] is more self documenting.
4. If you want to put the actual building of the array in a separate
method or class that's ok and even cleaner.
HTH,
Sam
------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.
On Thu, 14 Dec 2006 09:50:04 -0700, "stephen" <st********@hotmail.com>
wrote:
>Hi all,
VB .NET has modules in which you can create an object like this:
Module modVariables
Friend getPath As ArrayList = New ArrayList
End Module
and this arraylist is available anywhere in the project. the way I use it is
to populate it with the (directory) paths I use for file and fill it up in a
class like this:
Public Class HelperClass
Friend Function populatePaths() As Boolean
nvc = CType(ConfigurationSettings.GetConfig(pathArray(1) ),
NameValueCollection)
//something something
getPath.Add(nvc(key))
End Function
End Class
and retrive this getPaths() info by index in any other class
How can I do this in C#?
whats the concept of module in C# where I can declare global varibles (like
arraylist) populate it and then use it any class?
Appreciate your help,
Stephen