VB.NET to C# conceptual conversion question ----- please advice

S

stephen

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
 
P

Peter Bradley

I guess one solution would be to create an object with static member
variables and static methods to access them (getters and setters). It has
the same problem as any global variable solution, though. You need to be
absolutely sure that only the "right" clients change it's values and only do
so for reasons that are "good" for all the clients in the system. Perhaps
you might want to make them read only (constant) once they've been set - or
something.

Personally, I'd try to avoid this: but YMMV.

HTH


Peter
 
M

Mark Rae

How can I do this in C#?

Lots of people will tell you this is a "really bad idea", but here you go
anyway...

public sealed class MyGlobals
{
public static string MyGlobalString = "Hello";
}

Then, anywhere else in your app, you can write:

string strGlobalString = MyGlobals.MyGlobalString;
 
S

Samuel R. Neff

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<string> is
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top