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

Pedro Gonzalez
Pedro Gonzalez
2,097 Points

Help with Virtual Methods Objective.

I am not sure what I am doing wrong. The error I received was:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.IndexOutOfRangeException: Index was outside the bounds of the array. at Treehouse.CodeChallenges.RepeatDetector.Scan (System.Int32[] sequence) <0x405205d0 + 0x00049> in :0 at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&) at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00038] in /builddir/build/BUILD/mono-4.4.2/mcs/class/corlib/System.Reflection/MonoMethod.cs:295 --- End of inner exception stack trace --- at Treehouse.CodeChallenges.Helpers.ReflectionHelpers.Call[T] (System.Reflection.MethodInfo method, System.Object instance, System.Object[] args) <0x4051fde0 + 0x00027> in :0 at MonoTester.Run () [0x00118] in MonoTester.cs:95 at MonoTester.Main (System.String[] args) [0x00013] in MonoTester.cs:29

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

1 Answer

If the array has 5 items then they would be from position[0] - [4]. Your for loop is counting to <= sequence.Length which is 5 and beyond the index which is causing your exception. You don't need the else clause as it doesn't accomplish anything. Another option is to get rid of the variable and just return true inside the if statement and return false if the loop doesn't find a match.