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

Ken Ekholm 211 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?

Developer technologies | Visual Studio | Other
Developer technologies | Visual Studio | Other
A family of Microsoft suites of integrated development tools for building applications for Windows, the web, mobile devices and many other platforms. Miscellaneous topics that do not fit into specific categories.
Developer technologies | C#
Developer technologies | 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.
{count} votes

2 answers

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

  2. Karen Payne MVP 35,596 Reputation points Volunteer Moderator
    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

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.