Hello again.

I have a property 'F' in class 'Club'. I realized that it doesn't function properly while going through debugging and locals. At first look it had normal, wanted values.
BUT than every time I hovered over class->property F it increased by *2 or seomthing like that.

Here's the code:

class formation
    {
        private int[] frm = new int[7];
        private int def, dmf, sb, smf, mid, amf, st;

        public int[] Formation
        {
            get {
                frm[0] = Sb;
                frm[1] = Def;
                frm[2] = Dmf;
                frm[3] = Smf;
                frm[4] = Mid;
                frm[5] = Amf;
                frm[6] = St;
                return frm; 
            }
        }
        public int Smf
        {
            get { return smf; }
            set { smf = value; }
        }
        public int Sb
        {
            get { return sb; }
            set { sb = value; }
        }
        public int St
        {
            get { return st; }
            set {
                st = value; 
                }
        }
        public int Amf
        {
            get { return amf; }
            set {
                amf = value; 
                }
        }
        public int Mid
        {
            get { return mid; }
            set {
                mid = value; 
                }
        }
        public int Dmf
        {
            get { return dmf; }
            set {
                dmf = value; 
                }
        }
        public int Def
        {
            get { return def; }
            set {
                def = value; 
                }
        }
        
    }
//////////////////////////////////////
    class Club
    {
        private formation f = new formation();
        public formation F
        {
            get
            {
                for (int i = 0; i <= 10; i++)
                {
                    switch (PlayersBL[i].Position)
                    {
                        case "SB":
                            f.Sb++; break;
                        case "DC":
                            f.Def++; break;
                        case "DMC":
                            f.Dmf++; break;
                        case "SMF":
                            f.Smf++; break;
                        case "MC":
                            f.Mid++; break;
                        case "AMC":
                            f.Amf++; break;
                        case "ST":
                            f.St++; break;
                    }
                }
                return f;
            }
        }
    }
/////////////////////////
        private void someFunction() {
            foreach (Club club in mainForm.League)
            {
                if (club.Id == mainForm.UserClub.Id)
                {
                    continue;
                }
                else
                {
                    selectPlayerForFirst11("GK",club);
                    selectPlayerForFirst11("SB", club);
                    selectPlayerForFirst11("DC", club);
                    selectPlayerForFirst11("DMC", club);
                    selectPlayerForFirst11("SMF", club);
                    selectPlayerForFirst11("MC", club);
                    selectPlayerForFirst11("AMC", club);
                    selectPlayerForFirst11("ST", club);
                }
            }
        }

1) Not that this will help, but I would first initialize those numbers to zero in a constructor.
2) Something like this should run fine and true in the debugger.
3) What do you mean "F" increased in value?

Edited 4 Years Ago by thines01: clarity

I mean, each of variables inside F increases every time I hover over it in the locals. Like something's running in the background..

You have logic built into your 'Get' that tells it to increment every member variable by 1.... You must realize that the locals window uses this 'Get' when you try to get your values out of the class, therefore incrementing them each time you hover over it. You can fix that logic by taking out the member altogether, and recreating a new one with each 'get', OR a better way would be to take all that logic out of the 'Get':

First method:

public formation F
{
   get
   {
      formation temp = new formation();
      for (int i = 0; i<10;i++)
      {
          //increment logic/switch goes here
      }
      return temp;
   }
}

This really isn't what properties are meant for - it's more of a function/method.

A better approach would be:

class myClass
{
   formation f;
   public myClass()
   {
      f = GetFormation();
   }
   protected formation GetFormation()
   {
      formation temp = new formation();
      for (int i = 0; i < 10; i++)
      {
          //increment code/switch goes here
      }
      return temp;
   }
   public formation F
   {
       get { return f; }
       set { f = value; }
   }
}

Edited 4 Years Ago by skatamatic: n/a

This article has been dead for over six months. Start a new discussion instead.