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?
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 ChongCommented:
how many winforms do you have?

is your String List always static?
0
Ryan ChongCommented:
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.
0
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
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Ryan ChongCommented:
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.
0
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

0
Mike TomlinsonMiddle School Assistant TeacherCommented:
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.
0

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.
0
it_saigeDeveloperCommented:
@anarki,

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

-saige-
1
Jayadev NairApplication DeveloperCommented:
To the question, using a Shared member should help.
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.