0

I have the following class that is used in conjunction with the data object for PropertyGrid:

public enum WebUILanguage
{
    EnglishUS,
    German,
    //Actual list is obviously longer
}

[DefaultPropertyAttribute("SaveOnClose")]
public class MyData
{
    private HashSet<WebUILanguage> _SupportedUILanguages = new HashSet<WebUILanguage>(_k_SupportedUILanguages);

    //Area to define all default values    
    const WebUILanguage[] _k_SupportedUILanguages = {   //Error CS0134: A const field of a reference type other than string can only be initialized with null.
        WebUILanguage.EnglishUS,
        WebUILanguage.German
    };

    [CategoryAttribute("User Interface"),
    DefaultValueAttribute(_k_SupportedUILanguages),
    DescriptionAttribute("Supported user interface languages.")]
    [RefreshProperties(RefreshProperties.All)]
    public WebUILanguage[] SupportedUILanguages
    {
        get { return _SupportedUILanguages.ToArray(); }
        set 
        { 
            //Do 'set' magic here
        }
    }
}

But the compiler doesn't let me initialize a constant array -- the _k_SupportedUILanguages constant in the code above. What is the syntax for that?

PS. Note that I do not need a readonly array. It must be const!

7
  • No it is not possible in c# Commented Oct 28, 2013 at 6:39
  • @SriramSakthivel: I noted in my post that I do not need readonly definition. Commented Oct 28, 2013 at 6:41
  • It doesn't matter you mention it in your post or not. Language doesn't support that. You've to believe that. Commented Oct 28, 2013 at 6:42
  • What does a const give you that a readonly doesn't? Other than compile-time constness.. what is your reasoning for not wanting to use readonly? Commented Oct 28, 2013 at 6:44
  • 2
    Your question should probably be not about initialization of const array, as it's something not possible with C#, but providing a default value for designer property which is not of a primitive type. Commented Oct 28, 2013 at 7:01

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.