Click here to Skip to main content
15,891,372 members
Home / Discussions / C#
   

C#

 
GeneralRe: C# using get set. Why is this failing? Pin
Dave Kreskowiak29-May-19 16:23
mveDave Kreskowiak29-May-19 16:23 
GeneralRe: C# using get set. Why is this failing? Pin
kalberts29-May-19 4:38
kalberts29-May-19 4:38 
GeneralRe: C# using get set. Why is this failing? Pin
Richard MacCutchan29-May-19 5:58
mveRichard MacCutchan29-May-19 5:58 
GeneralRe: C# using get set. Why is this failing? Pin
Brian_TheLion29-May-19 14:44
Brian_TheLion29-May-19 14:44 
GeneralRe: C# using get set. Why is this failing? Pin
Dave Kreskowiak29-May-19 16:26
mveDave Kreskowiak29-May-19 16:26 
GeneralRe: C# using get set. Why is this failing? Pin
Brian_TheLion29-May-19 18:23
Brian_TheLion29-May-19 18:23 
GeneralRe: C# using get set. Why is this failing? Pin
lmoelleb2-Jun-19 22:46
lmoelleb2-Jun-19 22:46 
AnswerRe: C# using get set. Why is this failing? Pin
OriginalGriff28-May-19 20:41
mveOriginalGriff28-May-19 20:41 
Look at your code.
C#
public int Gold
    {
    get
        {
        return Gold;
        }
    ...
    }
So now lets use it:
C#
int g = myClassInstance.Gold;
It enters the Gold getter, which returns the value of ... the Gold getter. Which returns the value of ... the Gold getter. Which returns the value of ... the Gold getter. ... until the stack blows and your app crashes.

The same happens when you try to use the setter: it infinitely recurses trying to set itself!

Create a private backing field:
C#
private int _Gold = 0;
public int Gold
    {
    get
        {
        return _Gold;
        }
        
    set
        {
        if (value == 20)
            {
            value += 5;
            _Gold = value;
            }
        }
    }
And it'll all work.
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!

GeneralRe: C# using get set. Why is this failing? Pin
Brian_TheLion29-May-19 0:36
Brian_TheLion29-May-19 0:36 
GeneralRe: C# using get set. Why is this failing? Pin
Pete O'Hanlon29-May-19 0:43
mvePete O'Hanlon29-May-19 0:43 
GeneralRe: C# using get set. Why is this failing? Pin
Brian_TheLion29-May-19 14:41
Brian_TheLion29-May-19 14:41 
GeneralRe: C# using get set. Why is this failing? Pin
Pete O'Hanlon29-May-19 18:57
mvePete O'Hanlon29-May-19 18:57 
GeneralRe: C# using get set. Why is this failing? Pin
Brian_TheLion29-May-19 19:55
Brian_TheLion29-May-19 19:55 
GeneralRe: C# using get set. Why is this failing? Pin
OriginalGriff29-May-19 1:11
mveOriginalGriff29-May-19 1:11 
GeneralRe: C# using get set. Why is this failing? Pin
BillWoodruff29-May-19 3:30
professionalBillWoodruff29-May-19 3:30 
GeneralRe: C# using get set. Why is this failing? Pin
Brian_TheLion29-May-19 20:14
Brian_TheLion29-May-19 20:14 
GeneralRe: C# using get set. Why is this failing? Pin
BillWoodruff29-May-19 22:48
professionalBillWoodruff29-May-19 22:48 
GeneralRe: C# using get set. Why is this failing? Pin
Brian_TheLion30-May-19 1:20
Brian_TheLion30-May-19 1:20 
GeneralRe: C# using get set. Why is this failing? Pin
BillWoodruff30-May-19 4:33
professionalBillWoodruff30-May-19 4:33 
GeneralRe: C# using get set. Why is this failing? Pin
BillWoodruff31-May-19 20:12
professionalBillWoodruff31-May-19 20:12 
AnswerRe: C# using get set. Why is this failing? Pin
Gerry Schmitz29-May-19 3:52
mveGerry Schmitz29-May-19 3:52 
GeneralRe: C# using get set. Why is this failing? Pin
Brian_TheLion29-May-19 14:52
Brian_TheLion29-May-19 14:52 
QuestionWinForms, Window with FormBorderStyle.None and Taskbar moved doesn't adjust to new size Pin
Ken Guru28-May-19 4:46
Ken Guru28-May-19 4:46 
AnswerRe: WinForms, Window with FormBorderStyle.None and Taskbar moved doesn't adjust to new size Pin
Eddy Vluggen28-May-19 5:42
professionalEddy Vluggen28-May-19 5:42 
GeneralRe: WinForms, Window with FormBorderStyle.None and Taskbar moved doesn't adjust to new size Pin
Ken Guru28-May-19 22:00
Ken Guru28-May-19 22:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.