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
Sam OZAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
how many winforms do you have?

is your String List always static?
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
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.
Sam OZAuthor 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
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
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 DeveloperCommented:
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

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
anarki_jimbelSenior DeveloperCommented:
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_saigeDeveloperCommented:
@anarki,

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

-saige-
Jayadev NairApplication DeveloperCommented:
To the question, using a Shared member should help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.