tags:

views:

431

answers:

3

I know this is probably just a terminology mismatch but if i'm not mistaken i believe c# is? unless i'm missing something obvious??

...
    private const uint URL_COUNT = 18;
    private string[] _urls;

    public Redirector()
    {
        this._urls = new string[URL_COUNT];
        ...
    }
...

Results in “A constant value is expected “ and underlines URL_COUNT in the array definition??

Whats URL_COUNT if it isn’t a const -ant value?!?!

EDIT Phew, i thought for a second then i was going mad. I'm glad no one could repro this as that means it's just a local thing. Thanks for your help guys.

+5  A: 

It is a constant and should work fine. The following code compiled fine for me with the C# 3 compiler:

using System;

class Foo
{
    private const uint URL_COUNT = 18;
    private string[] _urls;

    public Foo()
    {
     this._urls = new string[URL_COUNT];
    }
}
Andrew Hare
Yep, can't repro this one either.
Steve Haigh
you need to supply both the initializer and a dimension length. I think the author accidentily edited the example code so it works :)
Razzie
+2  A: 

This works too, without any complaints from the compiler.

class Foo {
    private const uint URL_COUNT = 18;
    private readonly string[] _urls = new string[URL_COUNT];
}
Simon Svensson
+4  A: 

This will only fail to compile when you supply both the dimension lengths and an array initializer. For example:

this._urls = new string[URL_COUNT];

will be fine, but:

this._urls = new string[URL_COUNT] { "One", "Two" };

will not. The latter requires a constant expression. Note that a const variable is not a constant expressions, just a constant value. From the C# specification (3.0) par 12.6:

When an array creation expression includes both explicit dimension lengths and an array initializer, the lengths must be constant expressions and the number of elements at each nesting level must match the corresponding dimension length.

Razzie
+1 This is a plausible explanation for the error. I can't see any other way that this particular error could have been generated.
Andrew Hare
I removed the initialiser to simplify to example, i had no idea it could have been the cause of the problem!
Adam Naylor
Thought as much! Glad to have helped.
Razzie