Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Simple array use, no looping = StackOverflowException?

Simple array use, no looping = StackOverflowException?

Scheduled Pinned Locked Moved C#
questiondata-structures
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Christian Bailey
    wrote on last edited by
    #1

    Why does the simple exercise below throw a StackOverflowException? I didn't think I was looping or doing anything recursive.

    namespace ArrayTests
    {
    class HasArray
    {
    public int[] ExampleArray
    {
    set
    {
    ExampleArray = value;
    }
    }
    public HasArray()
    {
    int [] ExampleArray = new int[2] { 1, 2 };
    }
    }

    class Program
    {
        static void Main(string\[\] args)
        {
            HasArray concreteHasArray = new HasArray();
            int\[\] otherArray = new int\[2\] { 3, 4 };
           concreteHasArray.ExampleArray = otherArray;
        }
    }
    

    }

    M N I C 4 Replies Last reply
    0
    • C Christian Bailey

      Why does the simple exercise below throw a StackOverflowException? I didn't think I was looping or doing anything recursive.

      namespace ArrayTests
      {
      class HasArray
      {
      public int[] ExampleArray
      {
      set
      {
      ExampleArray = value;
      }
      }
      public HasArray()
      {
      int [] ExampleArray = new int[2] { 1, 2 };
      }
      }

      class Program
      {
          static void Main(string\[\] args)
          {
              HasArray concreteHasArray = new HasArray();
              int\[\] otherArray = new int\[2\] { 3, 4 };
             concreteHasArray.ExampleArray = otherArray;
          }
      }
      

      }

      M Offline
      M Offline
      Mali Perica
      wrote on last edited by
      #2

      But, you were doing something recursive. In ExampleArray property, through set method, you were setting value to the same property. To see this by yourself, set breakpoint on beginning of Main() method and press F11 key several times. You can correct your HasArray class by adding one attribute of integer array type to it which holds the value for ExampleArray property. This is what I mean:

      class HasArray
      {
          private int\[\] \_exampleArray;
      
          public int\[\] ExampleArray
          {
              get { return \_exampleArray; }
              set { \_exampleArray = value; }
      
          }
          public HasArray()
          {
              \_exampleArray = new int\[2\] { 1, 2 };
          }
      }
      
      1 Reply Last reply
      0
      • C Christian Bailey

        Why does the simple exercise below throw a StackOverflowException? I didn't think I was looping or doing anything recursive.

        namespace ArrayTests
        {
        class HasArray
        {
        public int[] ExampleArray
        {
        set
        {
        ExampleArray = value;
        }
        }
        public HasArray()
        {
        int [] ExampleArray = new int[2] { 1, 2 };
        }
        }

        class Program
        {
            static void Main(string\[\] args)
            {
                HasArray concreteHasArray = new HasArray();
                int\[\] otherArray = new int\[2\] { 3, 4 };
               concreteHasArray.ExampleArray = otherArray;
            }
        }
        

        }

        N Offline
        N Offline
        NassosReyzidis
        wrote on last edited by
        #3

        Hi Cristian, The problem is here: public int[] ExampleArray { set { ExampleArray = value; } } In the Setter curly Brackets you reset the property (Same Name), so when you call concreteHasArray.ExampleArray = otherArray; you conccurently setting the ExampleAray. Solution: namespace ArrayTests { class HasArray { private int[] _ExampleArray ; //Properties Expose a private member!! public int[] ExampleArray { set { _ExampleArray = value;//Set in the private member!! } get{return _ExampleArray;}//You need this so you can call concreteHasArray.ExapleArray and return the Array. } public HasArray() { ExampleArray = new int[2] { 1, 2 }; } } class Program { static void Main(string[] args) { HasArray concreteHasArray = new HasArray(); int[] otherArray = new int[2] { 3, 4 }; concreteHasArray.ExampleArray = otherArray; } } } Hope this helps Nassos

        GanDad

        1 Reply Last reply
        0
        • C Christian Bailey

          Why does the simple exercise below throw a StackOverflowException? I didn't think I was looping or doing anything recursive.

          namespace ArrayTests
          {
          class HasArray
          {
          public int[] ExampleArray
          {
          set
          {
          ExampleArray = value;
          }
          }
          public HasArray()
          {
          int [] ExampleArray = new int[2] { 1, 2 };
          }
          }

          class Program
          {
              static void Main(string\[\] args)
              {
                  HasArray concreteHasArray = new HasArray();
                  int\[\] otherArray = new int\[2\] { 3, 4 };
                 concreteHasArray.ExampleArray = otherArray;
              }
          }
          

          }

          I Offline
          I Offline
          Ian Shlasko
          wrote on last edited by
          #4
              public int\[\] ExampleArray
              {
                  set
                  {
                      ExampleArray = value;
                  }
              }
          

          Basically, what you're saying here is: "In order to assign a value to ExampleArray, you first assign the value to ExampleArray by assigning a value to ExampleArray by assigning a ......" The "standard" property format is as follows: private int _myVar;

          public int MyProperty
          {
          get
          {
          return _myVar;
          }
          set
          {
          _myVar = value;
          }
          }

          You see, the variable is what holds the actual data... The property is just a way to access it.

          R 1 Reply Last reply
          0
          • I Ian Shlasko
                public int\[\] ExampleArray
                {
                    set
                    {
                        ExampleArray = value;
                    }
                }
            

            Basically, what you're saying here is: "In order to assign a value to ExampleArray, you first assign the value to ExampleArray by assigning a value to ExampleArray by assigning a ......" The "standard" property format is as follows: private int _myVar;

            public int MyProperty
            {
            get
            {
            return _myVar;
            }
            set
            {
            _myVar = value;
            }
            }

            You see, the variable is what holds the actual data... The property is just a way to access it.

            R Offline
            R Offline
            revi22
            wrote on last edited by
            #5

            What method do you think is better when there are many threads? Regards, Analizzatore Cloro

            1 Reply Last reply
            0
            • C Christian Bailey

              Why does the simple exercise below throw a StackOverflowException? I didn't think I was looping or doing anything recursive.

              namespace ArrayTests
              {
              class HasArray
              {
              public int[] ExampleArray
              {
              set
              {
              ExampleArray = value;
              }
              }
              public HasArray()
              {
              int [] ExampleArray = new int[2] { 1, 2 };
              }
              }

              class Program
              {
                  static void Main(string\[\] args)
                  {
                      HasArray concreteHasArray = new HasArray();
                      int\[\] otherArray = new int\[2\] { 3, 4 };
                     concreteHasArray.ExampleArray = otherArray;
                  }
              }
              

              }

              C Offline
              C Offline
              Christian Bailey
              wrote on last edited by
              #6

              Ooooooh.... I thought I was just saving myself the time to set up private and public parts. Now I see it is recursive, of course! Thanks!

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups