-1

So I have this class that holds 3 counters:

    public class Files
    {
        private static ObservableCollection<Files> _files = new ObservableCollection<Files>();
        private static int _inProcess;
        private static int _finished;
        private static int _inQueue;

        public static ObservableCollection<Files> List
        {
            get { return _files ; }
            set { _files = value; }
        }

        public static int InProcess
        {
            get { return _inProcess; }
            set
            {
                _inProcess = value;
            }
        }

        public static int Finished
        {
            get { return _finished; }
            set
            {
                _finished = value;
            }
        }

        public static int InQueue
        {
            get { return _inQueue; }
            set
            {
                _inQueue = value;
            }
        }
}

And from another class I want to add value to this fields:

Interlocked.Increment(ref Files.InProcess);

But got this error:

A property or indexer may not be passed as an out or ref parameter.

This works fine:

Files.InProcess++;

How can i fix it ?

Clemens
  • 123,504
  • 12
  • 155
  • 268

1 Answers1

2

The error is pretty straightforward. You can't pass a property as ref. In this case the best option is to create a method

public static void IncrementInProcess() { Interlocked.Increment(ref _inProcess); }

  • I can put this inside my Files class where _inProcess is member of this class ? – Danny Nitman Jan 23 '18 at 17:52
  • @DannyNitman Yes. While I'm at it, your properties are pretty pointless since the get and set are both public and no check is done in either case. You should either a) not use them at all or b) use auto-implemented properties – Corentin Brossutti Jan 23 '18 at 17:58