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

need help with this override method challenge iv already tried finding answers else where help please

so okay I don't know if you guys can see what I alredy did here but I'm not getting any compiler error just that the index is out of bounds but how can I stop this from happning like I don't know how I could solve this only thing I can think of is to create a nother loop but then that just would brake the whole thing I really don't know where I'm going wrong so this is why I don't know how to solve it

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) 
        { 
            int seqCompare = 0;

            foreach(int i in sequence)
            {       
               bool result = sequence[i] == seqCompare;

                if(result)
                { 
                return true;
                }
                seqCompare = sequence[i];
            }

            return false;
        } 
    } 
}
/*namespace Treehouse.CodeChallenges
{
    class RepeatDetector : SequenceDetector
    {
        private bool z;
        new public virtual bool Scan(int[] sequence)
        {
            for (int i = 1; i < sequence.Length; i++)
            { 

                if (sequence[i - 1] == sequence[i])
                {
                z = true
                }   
                return z;
            }  
        }
    }
}*/

Okay so I figured out a way to get around it before anyone had a chance to reply. Just wanted to update you guys but please if you do have a different solution than mine do share it because I feel like there should be a simpler solution than the one I came up with. Okay so my solution to this issue was to take the for loo[ and wrap it in a try catch statement and catch any System.Exception being return and upon an exception being thrown I return false meaning that one the loop checked every value in the index for repetitions it would go out of bounds and that means that in the array there are no repetitions so upon going out of bounds the exception is thrown and I return false and if the loop dose catch the repetition it will return true there and then and that is that. But I feel there should be a way to check the index inside the actual loop and still check the repetitions all in more simple code and I really am not that far in this course so if there are simpler way and you would like to share then please feel free and thank you Community, for always helping to find solutions when we get stuck somewhere it really is helpful :)

1 Answer

Simon Coates
Simon Coates
28,694 Points

It's been a while since i did this exercise, but it seemed to accept

namespace Treehouse.CodeChallenges
{
    class RepeatDetector: SequenceDetector
    {
        public override bool Scan(int[] sequence)
        {
            int? prev = null;
            foreach(int x in sequence)
            {
                if(prev == x)
                {
                    return true;
                }
                prev = x;
            }
            return false;
        }    
    }    
}

the ? after int makes prev nullable.

The following also seemed to pass:

namespace Treehouse.CodeChallenges
{
    class RepeatDetector: SequenceDetector
    {
        public override bool Scan(int[] sequence)
        {
            int prev = 0;
            int iteration = 0;
            foreach(int x in sequence)
            {
                iteration++;
                if(iteration > 1 )
                {
                    if(x == prev)
                    {
                        return true;
                    }                
                }
                prev = x;
            }        
            return false;
        }
    }
}

Thanks Simon I appreciate the feed back I will take detail notes on this way to approach the problem. The solutions you came up with are brilliant and I'm really putting all my effort to learn as much as I can so knowing that there is a simpler way to solve this is absolutely and plus. Thanks again.