Click here to Skip to main content
15,914,400 members
Home / Discussions / C#
   

C#

 
GeneralRe: C# using get set. Why is this failing? Pin
Brian_TheLion28-May-19 20:37
Brian_TheLion28-May-19 20:37 
AnswerRe: C# using get set. Why is this failing? Pin
lmoelleb28-May-19 20:39
lmoelleb28-May-19 20:39 
GeneralRe: C# using get set. Why is this failing? Pin
Brian_TheLion29-May-19 0:24
Brian_TheLion29-May-19 0:24 
GeneralRe: C# using get set. Why is this failing? Pin
lmoelleb29-May-19 2:27
lmoelleb29-May-19 2:27 
AnswerRe: C# using get set. Why is this failing? Pin
Member 1356638329-May-19 5:30
Member 1356638329-May-19 5:30 
GeneralRe: C# using get set. Why is this failing? Pin
Dave Kreskowiak29-May-19 7:18
mveDave Kreskowiak29-May-19 7:18 
GeneralRe: C# using get set. Why is this failing? Pin
Brian_TheLion29-May-19 14:50
Brian_TheLion29-May-19 14:50 
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 

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.