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

Discussion in 'ASP .Net' started by stephen, Dec 14, 2006.

  1. stephen

    stephen Guest

    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
     
    stephen, Dec 14, 2006
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:udhDo$...
    > 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
    >
     
    Peter Bradley, Dec 14, 2006
    #2
    1. Advertising

  3. stephen

    Mark Rae Guest

    "stephen" <> wrote in message
    news:udhDo$...

    > 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;
     
    Mark Rae, Dec 14, 2006
    #3
  4. 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

    ------------------------------------------------------------
    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" <>
    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
    >
     
    Samuel R. Neff, Dec 14, 2006
    #4
  5. stephen

    stephen Guest

    Thanks for you replies and help Peter, Samuel, Mark

    Stephen


    "stephen" <> wrote in message
    news:udhDo$...
    > 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
    >
     
    stephen, Dec 14, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    595
    Flinky Wisty Pomm
    Apr 3, 2006
  2. Todd Prickett

    Conceptual question...

    Todd Prickett, Dec 22, 2003, in forum: Java
    Replies:
    3
    Views:
    711
    Sudsy
    Dec 22, 2003
  3. Thom Anderson

    Seeking a Conceptual Approach Please

    Thom Anderson, Jun 30, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    335
    Robbe Morris [C# MVP]
    Jul 6, 2006
  4. gk
    Replies:
    0
    Views:
    299
  5. bubai
    Replies:
    2
    Views:
    1,205
    Francesco S. Carta
    Jul 8, 2010
Loading...

Share This Page