473,385 Members | 1,934 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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
Dec 14 '06 #1
4 1134
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
"stephen" <st********@hotmail.comwrote in message
news:ud**************@TK2MSFTNGP06.phx.gbl...
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

Dec 14 '06 #2
"stephen" <st********@hotmail.comwrote in message
news:ud**************@TK2MSFTNGP06.phx.gbl...
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;
Dec 14 '06 #3

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
Dec 14 '06 #4
Thanks for you replies and help Peter, Samuel, Mark

Stephen
"stephen" <st********@hotmail.comwrote in message
news:ud**************@TK2MSFTNGP06.phx.gbl...
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

Dec 14 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.