Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

C# Compiling Error?

Update:

Try to compile the code @ http://rextester.com/RAUBYA459 I'm not gonna ask why ' list ' has to be a constant, but is there a way for the compiler to overlook this, or something. I need a solution

3 Answers

Relevance
  • david
    Lv 7
    5 years ago

    Your link doesn't work. Could you correct it? If not, could you give the exact error message, and the line producing it?

    One possible reason you'd get this sort of error is that, in C#, constants must be determined at compile-time. So if you try to take a non-constant value and put it into a constant, it will yell at you. One way to get rid of the error, then, is to get rid of the "const" keyword on the thing you want to assign to. For example:

    const int MY_NUMBER = x; // doesn't work unless x is constant

    could be changed to

    int MY_NUMBER = x;

    Of course, then you lose the assurance that the MY_NUMBER doesn't change. And this might not be the reason you're getting the error - I'm guessing since I can't see your code.

  • 5 years ago

    The link to your code doesn't work, so I can only guess based on the error message. There are certain places in C# code where you are only allowed to use a constant value, not a variable or expression. For example setting the default value for a parameter in a function. The '10' has to be a constant you cannot put a variable in there.

    public void Foo(int x = 10)

  • 5 years ago

    Look at the error message, read it, then look at the source file line it's complaining about very carefully. That's how you find out what's wrong.

    If you don't understand the error message, google with exact pasted phrases (omitting names defined in your program, of course) to see who else had this problem and how it was solved by or for them.

    ---Edit: Okay, you added some detail, but that link doesn't work here.

Still have questions? Get your answers by asking now.