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 Virtual Properties

help plz

Challenge Task 1 of 1

SequenceDetector now contains a Description property. In RepeatDetector, override the Description property to return "Detects repetitions". Important: In each task of this code challenge, the code you write should be added to the code from the previous task. Restart Get Help Check Work RepeatDetector.cs SequenceDetector.cs

1 namespace Treehouse.CodeChallenges 2 { 3 class RepeatDetector : SequenceDetector 4 { 5 public override bool Scan(int[] sequence) 6 { 7 if(sequence.Length < 2) 8 { 9 return false; 10 } 11 ā€‹ 12 for(int i = 1; i < sequence.Length; ++i) 13 { 14 if(sequence[i] == sequence[i-1]) 15 { 16 return true; 17 } 18 } 19 ā€‹ 20 return false; 21 } 22 } 23 } 24 ā€‹

RepeatDetector.cs
namespace Treehouse.CodeChallenges
{
    class RepeatDetector : SequenceDetector
    {
        public override bool Scan(int[] 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;
        }
    }
}
SequenceDetector.cs
namespace Treehouse.CodeChallenges
{
    class SequenceDetector
    {
        public string Description => "";

        public virtual bool Scan(int[] sequence)
        {
            return true;
        }
    }
}

2 Answers

First, make the Description property in SequenceDetector overridable by adding the virtual keyword:

public virtual string Description => "";

then in the RepeatDetector class, add the property by overriding it's parent's property:

public override string DescriptIon => "Detects Repetitions";
Calvin Secrest
Calvin Secrest
24,815 Points

I seem to not be able to compile this syntax I used the same code in my challenge

Ah yes, that must be because i had a typo in the override property. Instead of 'DescriptIon' make sure you put 'Description' (lowercase 'i')

Calvin Secrest
Calvin Secrest
24,815 Points

Ok thanks and I'm confused on the RepeatDetector class, where do I add the property?

Anywhere except in the scan method.

thank's tj