Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# Intermediate C# Polymorphism Accessor Access Modifiers

help plz

Challenge Task 1 of 1

SequenceDetector now has a LastScannedSequence property. Change the access of the LastScannedSequence property so that only SequenceDetector and its subclasses can set the LastScannedSequence property. Other classes should still be able to get the last scanned sequence. Important: In each task of this code challenge, the code you write should be added to the code from the previous task. Restart Preview Get Help Check Work SequenceDetector.cs RepeatDetector.cs

1 namespace Treehouse.CodeChallenges 2 { 3 class SequenceDetector 4 { 5 public virtual string Description => ""; 6 ā€‹ 7 public int[] LastScannedSequence { get; private set; } 8 ā€‹ 9 public virtual bool Scan(int[] sequence) 10 { 11 LastScannedSequence = sequence; 12 return true; 13 } 14 } 15 } 16 ā€‹

SequenceDetector.cs
namespace Treehouse.CodeChallenges
{
    class SequenceDetector
    {
        public virtual string Description => "";

        public int[] LastScannedSequence { get; private set; }

        public virtual bool Scan(int[] sequence)
        {
            LastScannedSequence = sequence;
            return true;
        }
    }
}
RepeatDetector.cs
namespace Treehouse.CodeChallenges
{
    class RepeatDetector : SequenceDetector
    {
        public override string Description => "Detects repetitions";

        public override bool Scan(int[] sequence)
        {
            LastScannedSequence = sequence;

            if(sequence.Length < 2)
            {
                return false;
            }

            for(int i = 1; i < sequence.Length; ++i)
            {
                if(sequence[i] == sequence[i-1])
                {
                    return true;
                }
            }

            return false;
        }
    }
}

1 Answer

Steven Parker
Steven Parker
229,785 Points

The term you're looking for is "protected".

When "private", only the class itself can set the property. But "protected" allows the class and its subclasses to set the property.