Use the same void between multiple forms in the same project?

Ken Ekholm 151 Reputation points
2023-05-17T17:05:55.3766667+00:00

Is possible to use the same void between multiple forms in the same project?

If it possible, how is it done?

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,606 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,249 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Castorix31 81,726 Reputation points
    2023-05-17T17:36:51.7766667+00:00
    0 comments No comments

  2. Karen Payne MVP 35,036 Reputation points
    2023-05-17T21:33:16.0266667+00:00

    One option is using a thread safe singleton

    public sealed class AppOperations
    {
        private static readonly Lazy<AppOperations> Lazy =
            new(() => new AppOperations());
    
        private const int _max = 5;
        public static AppOperations Instance => Lazy.Value;
    
        public int Index { get; set; }
    
        public void Increment()
        {
            if (Index + 1 < _max)
            {
                Index++;
            }
            else
            {
                Index = 0;
            }
        }
    
        public void Decrement()
        {
            Index--;
            if (Index < 0)
            {
                Index = _max - 1;
            }
        }
    }
    
    

    Usage in one form

    AppOperations.Instance.Increment();
    
    

    In another form var value = AppOperations.Instance.Index;

    0 comments No comments