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 Methods

RepeatDetector.Scan doesn't detect repetitions. Lost, completely. Why? Everything looks just perfect.

It should be working but it's not. Please, help!

SequenceDetector.cs
namespace Treehouse.CodeChallenges
{
    class SequenceDetector
    {
        public virtual bool Scan(int[] sequence)
        {
            return true;
        }         
    }          
}
RepeatDetector.cs
using System;
namespace Treehouse.CodeChallenges
{    
class RepeatDetector : SequenceDetector
{
    public override bool Scan(int[] sequence)
    {
        bool isSame = false;
        for(int i = 0 ; i > sequence.Length; i++)
        {
            if(i>0) {             
                isSame = sequence[i] == sequence[i-1] ? true:false;
                if(isSame)
                {
                break;
                }
            }
        }
        return isSame;


    }         

}
}

3 Answers

Steven Parker
Steven Parker
231,248 Points

Here's a few hints:

  • the loop should start at 1 instead of 0 (because of the reference to "i-1")
  • the loop should run while the index is less than the length instead of greater
  • the ternary is redundant in "sequence[i] == sequence[i-1] ? true:false;"

Thank you, Steven. I managed to solve the problem. Why is ternary redundant? I think it saves some space.

Steven Parker
Steven Parker
231,248 Points

The conditional expression of the ternary resolves to "true" or "false". But then the rest of it substitutes "true" and "false".

So "sequence[i] == sequence[i-1] ? true:false;" can be replaced by just "sequence[i] == sequence[i-1]".

This is a new information for me. Thank you, Steven!