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

Stuck in the virtual methods task.

I can't seem to find what is wrong with my code. The compiler error states "Use of unassigned local variable `IsThereRepeats' ". Any help is greatly appreciated.

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 IsThereRepeats;                //Variable that will be changed depending on input
            for(int i=0;i<sequence.Length;i++)  //loops through sequence array
            {
                if(sequence[i]==sequence[i++])   // checks if for instance sequence[1] number is the same as sequence[2] number
                {
                    IsThereRepeats = true;      //if it is then IsThereRepeats = true; so there is a repeat
                }
                else
                {
                    IsThereRepeats = false;     //otherwise it returns false;no repeats
                }
            }
            return IsThereRepeats;     //finally returns the boolean value
        }
    }
}

It's also a good idea to use "break;" right after IsThereRepeats = true; statement. We are supposed to return true if ANY value repeats so there is no sense to test the rest of the array when we found what we were looking for.

1 Answer

Justin Horner
STAFF
Justin Horner
Treehouse Guest Teacher

Hello Pedro,

It's always a good idea to initialize a variables with a default value. In this case, I would set IsThereRepeats to false on line 7. The main problem with the code is that you are setting IsThereRepeats to false anytime the current element is not equal to the next element in the sequence.

In this case, what's happening is it is finding a repeat and setting IsThereRepeats to true, but if the next check is not a repeat it's setting IsThereRepeats to false. This logic is incorrect because we want to return true if there is at least one repeat. In this case, you simply need to default the variable to false, as mentioned above, and remove the else from the if statement so that it will return false if no repeats are found and true if at least one repeat is found.

I hope this helps.