-1

I want to build a const string with a default value from database.

So when I do such:

private const String nifInvalid = MensagensCacheManager.getMensagem(
        MensagensCacheManager.GetLanguage(), 
        "empresa.nif.invalid");

So I have the following error:

'nifInvalid' must be constant

I could use static readonly but i want to use it for a custom validation message such as:

[RegularExpression(@"(^[0-9]+$)|(\d{8})([-]?)([A-Z]{1})", ErrorMessage=nifInvalid)]
public String nif { set; get; }

How could i build a custom validation and set custom validation message?

langeles86
  • 162
  • 1
  • 12

2 Answers2

3

You can't. consts value should be specified during compile time.

From MSDN:

Constant fields and locals aren't variables and may not be modified. Constants can be numbers, Boolean values, strings, or a null reference. Don’t create a constant to represent information that you expect to change at any time.

The type of a constant declaration specifies the type of the members that the declaration introduces. The initializer of a constant local or a constant field must be a constant expression that can be implicitly converted to the target type. A constant expression is an expression that can be fully evaluated at compile time. Therefore, the only possible values for constants of reference types are string and a null reference.

There is a workaround which Darin has mentioned in his answer but you should now the difference between static readonly and const.

If you want to have attributes with dynamic values then you can use this trick.

Community
  • 1
  • 1
Hamid Pourjam
  • 20,441
  • 9
  • 58
  • 74
  • To expand on this correct answer; a constant value is replaced at compile time with the constant value, so in no cases can it be derived at runtime, even if the value will not change during the exection of the program it cannot be permitted to change from the point of compilation onward - so deriving it from an external resource is not possible or desirable. – PhillipH May 02 '16 at 11:34
2

You can't use a const in this case because fetching a value from a database by definition happens at runtime, so you cannot be talking about a constant. A constant is something that is known at compile time. In your case you could use static readonly field:

private static readonly string nifInvalid = MensagensCacheManager.getMensagem(MensagensCacheManager.GetLanguage(), "empresa.nif.invalid");
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Sure i could, but i want to use this String for: [RegularExpression(@"(^[0-9]+$)|(\d{8})([-]?)([A-Z]{1})", ErrorMessage=nifInvalid)] public String nif {set; get;} And for this it must be a const. Any help? – langeles86 May 02 '16 at 11:32
  • 2
    Then why is this so important information not part of your question? You can't do that because by definition a constant value must be known at compile time. You will have to write your own custom attribute to achieve this effect. Is this for an ASP.NET MVC application or something else? Please provide the relevant information if you want your question to make sense. So now please go ahead and edit your question by providing the required context. – Darin Dimitrov May 02 '16 at 11:34
  • It doesn't matter what you intend to use it for, a const string cannot be calculated at runtime, it has to be a constant *literal* in the source code, or an expression only involving other const members. – Lasse V. Karlsen May 02 '16 at 11:35
  • Edited with all information – langeles86 May 02 '16 at 11:46