Link to home
Create AccountLog in
Avatar of Sam OZ
Sam OZFlag 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
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

how many winforms do you have?

is your String List always static?
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.
Avatar of 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
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.
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
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
@anarki,

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

-saige-
To the question, using a Shared member should help.