Question Build errors in my code

levelizard

New member
Joined
Oct 26, 2020
Messages
2
Programming Experience
1-3
Hi,

I am a beginner with C# and I am doing a task in VS, trying to execute the following code, there are no errors but it still does not build successfully:
C#:
namespace ConsoleApp1
{
    using System;
    using System.Linq;
    public class Program
    {
        public int[] SwitchLights(int[] a)
        {
            var count = a.Count(t => t == 1);
            for (int i = 0; i < a.Length; i++)
            {
                var current = count;
                if (a == 1) count--;
                if (current % 2 == 1) a = a == 1 ? 0 : 1;
            }
            return a;
        }
    }
}
Does someone have an idea how to run it? Thank you in advance.
 
Last edited by a moderator:

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,190
Location
Sydney, Australia
Programming Experience
10+
The title says that there are build errors and then your post says that there aren't build errors and then I copied your code and there are build errors. You need to make up your mind which it is and provide accurate information.

As for the issue, the error messages tell you exactly what the issue is and where.
Operator '==' cannot be applied to operands of type 'int[]' and 'int'
Why do you think that you should be able to compare an int array to an int value for equality? That's like trying to see whether an egg carton is the same as an egg. You need to think about what you're actually trying to achieve there and then write code to do that. Given that you are in a for loop based on the length of that array, I would assume that would want to get the array element at the current index and do something with it. If so, that's what you should do. Where there are you referring to an element of that array?
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
4,027
Location
Chesapeake, VA
Programming Experience
10+
there are no errors but it still does not build successfully
There are errors, but I think that you had accidentally filtered out the errors in the Errors List pane of Visual Studio, by clicking on the Errors button within the pane.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,190
Location
Sydney, Australia
Programming Experience
10+
By no errors, I meant that the compiler shows me "no issues found" and that is why I cannot find the problem.
I honestly don't see how that's possible. When I build a project containing that code I get three build errors, two of which lead to code being underlined. Does the compiler even use that particular language? Can you show us a screenshot of the code window and the build output because, if your IDE is not flagging those issues, there's something wrong with it.
 
Top Bottom