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

error cs1525 but I think everything else is correct?

Not sure what I'm doing wrong here. Would really appreciate some pin pointing!

SequenceDetector.cs
namespace Treehouse.CodeChallenges
{
    public class SequenceDetector
    {
        public virtual bool Scan(int[] sequence)
        {
            return true;
        }
    }
}
RepeatDetector.cs
namespace Treehouse.CodeChallenges 
{
public class RepeatDetector : SequenceDetector
    {
        public override bool Scan(int[] sequence)
        {
            for(int i=0;i<sequence.Length;i++)
            {
                if (sequence[i] == (sequence[i]- 1))
                {
                return true;
                }
                else (sequence[i] != (sequence[i]- 1))
                {
                return false;
                }
            }
        }
    }
}

2 Answers

Mark Sebeck
MOD
Mark Sebeck
Treehouse Moderator 37,329 Points

First time through the loop i-1 would not exist because it would be -1. Quick and dirty fix is to not run those if statements unless i > 0. You could wrap them with another if statement.

Mark Sebeck
MOD
Mark Sebeck
Treehouse Moderator 37,329 Points

Actually looking at your code again you do it want to return false if they are not equal because you will not check the rest of the array. Returning true on the first one you find is ok because you don’t care if there are more only than there are at least one. So take the else off and only return false after the loop. That way if you don’t find it you would never return true and would instead run the code after the loop and return false. You still need to handle the i-1 index from my first answer.