none
why stack over flow excepton is thrown in this code? RRS feed

  • Question

  • public int MovieID
            {
                set
                {
                    value = r1.Next(1, 10);
                    MovieID = value;
                }
                get
                {
                    return MovieID;
                }
            }
    Friday, May 31, 2019 10:10 AM

All replies

  • The getter does "return MovieID". This in turn calls the getter of MovieID, which calls the getter of MovieID.... and so indefinitely until the Stack overflows.

    You probably meant to write "return movieID" (with a lowercase m), where movieID would be a private variable used as the backing store for the property (with a capital M).

    private int movieID;
    public int MovieID
             {
                 set
                 {
                     value = r1.Next(1, 10);
                     movieID = value;
                 }
                 get
                 {
                     return movieID;
                 }
             }


    EDIT: Note that assigning a value to "value" in the setter is not a good idea. The property will probably not work as intended.

    Friday, May 31, 2019 11:39 AM
    Moderator
  • Hi

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 3, 2019 1:28 AM
    Moderator