Avatar of Sam OZ
Sam OZ
Flag for Australia asked on

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
Visual Basic.NET.NET ProgrammingC#

Avatar of undefined
Last Comment
Jayadev Nair

8/22/2022 - Mon
Ryan Chong

how many winforms do you have?

is your String List always static?
Ryan Chong

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 OZ

ASKER
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
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Ryan Chong

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 Nair

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

ASKER CERTIFIED SOLUTION
Mike Tomlinson

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Dmitry G

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
it_saige

@anarki,

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

-saige-
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Jayadev Nair

To the question, using a Shared member should help.