We help IT Professionals succeed at work.

Store String List globally in VB.NET

I want to store a string list globally in VB.NET  winform application ( Similar to how an enumerated list is kept . But the value has to be string and not numeric)

C# example is also fine

Thanks
Comment
Watch Question

Ryan ChongSoftware Team Lead

Commented:
how many winforms do you have?

is your String List always static?
Ryan ChongSoftware Team Lead

Commented:
I would probably store the info in a Config file, load it when necessary

you can also define a static class for the loading if necessary.

Author

Commented:
It is a static list . There is only one form .  Even if I keep it in config , I hope I still need some list for getting the string
Ryan ChongSoftware Team Lead

Commented:
if you want it to be "static" and only have one form, why not just declare it as a List (Of String) ? like:

Public Class Form1

    Dim myList As New List(Of String)

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        myList.Add("Item 1")
        myList.Add("Item 2")
        myList.Add("Item 3")
        myList.Add("Item 4")
        myList.Add("Item 5")
       
    End Sub

End Class

Open in new window


then access it like:

myList.Item(n) 

Open in new window

which n is a zero base integer.
Jayadev NairApplication Developer

Commented:
Please use MemoryCache for this purpose. Faster for retrieval and can globally access within application domain even if you add more forms later. Add System.Runtime.Caching reference to your project.

   
 List<string> GetStringList()
        {
            var cache = MemoryCache.Default;
            var content = cache["stringList"] as List<string>;

            if (content == null)
            {
                var policy = new CacheItemPolicy
                    { AbsoluteExpiration = ObjectCache.InfiniteAbsoluteExpiration };

                content = new List<string>();
                content.Add("Item1");
                content.Add("Item2");

                cache.Set("stringList", content, policy);
            }

            return content;
        }

Open in new window

High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
If you want it to be Global, then either declare it as...
a Public Variable in a Module, or make it
a Shared Member in a Class.
anarki_jimbelSenior Developer
Commented:
The only comment I agree is from Mike Tomlinson. Declare this list as a shared member in a class, specifically, in the form you have.
Really, if you have one form only you do not need to make it shared, however if you add forms in future - this may help.
Where you store this list, in a config file, in a database, in some cache - does not matter. Personally I won't use VB Modules - it is very specific to VB, I'd say this is old VB structure, if you will want later to convert your code to C# or any other language - you may have problems.
it_saigeDeveloper
Distinguished Expert 2019

Commented:
@anarki,

Just to clarify, in VB.NET a Module is the same as a static class in C#.

-saige-
Jayadev NairApplication Developer

Commented:
To the question, using a Shared member should help.