Click here to Skip to main content
14,305,681 members
   

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
Member 798912229-May-19 4:38
memberMember 798912229-May-19 4:38 
GeneralRe: C# using get set. Why is this failing? Pin
Richard MacCutchan29-May-19 5:58
protectorRichard MacCutchan29-May-19 5:58 
GeneralRe: C# using get set. Why is this failing? Pin
Brian_TheLion29-May-19 14:44
memberBrian_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
memberBrian_TheLion29-May-19 18:23 
GeneralRe: C# using get set. Why is this failing? Pin
lmoelleb2-Jun-19 22:46
memberlmoelleb2-Jun-19 22:46 
AnswerRe: C# using get set. Why is this failing? Pin
OriginalGriff28-May-19 20:41
protectorOriginalGriff28-May-19 20:41 
Look at your code.
public int Gold
    {
    get
        {
        return Gold;
        }
    ...
    }
So now lets use it:
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:
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
memberBrian_TheLion29-May-19 0:36 
GeneralRe: C# using get set. Why is this failing? Pin
Pete O'Hanlon29-May-19 0:43
protectorPete O'Hanlon29-May-19 0:43 
GeneralRe: C# using get set. Why is this failing? Pin
Brian_TheLion29-May-19 14:41
memberBrian_TheLion29-May-19 14:41 
GeneralRe: C# using get set. Why is this failing? Pin
Pete O'Hanlon29-May-19 18:57
protectorPete O'Hanlon29-May-19 18:57 
GeneralRe: C# using get set. Why is this failing? Pin
Brian_TheLion29-May-19 19:55
memberBrian_TheLion29-May-19 19:55 
GeneralRe: C# using get set. Why is this failing? Pin
OriginalGriff29-May-19 1:11
protectorOriginalGriff29-May-19 1:11 
GeneralRe: C# using get set. Why is this failing? Pin
BillWoodruff29-May-19 3:30
mveBillWoodruff29-May-19 3:30 
GeneralRe: C# using get set. Why is this failing? Pin
Brian_TheLion29-May-19 20:14
memberBrian_TheLion29-May-19 20:14 
GeneralRe: C# using get set. Why is this failing? Pin
BillWoodruff29-May-19 22:48
mveBillWoodruff29-May-19 22:48 
GeneralRe: C# using get set. Why is this failing? Pin
Brian_TheLion30-May-19 1:20
memberBrian_TheLion30-May-19 1:20 
GeneralRe: C# using get set. Why is this failing? Pin
BillWoodruff30-May-19 4:33
mveBillWoodruff30-May-19 4:33 
GeneralRe: C# using get set. Why is this failing? Pin
BillWoodruff31-May-19 20:12
mveBillWoodruff31-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
memberBrian_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
memberKen 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
mveEddy 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
memberKen 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.