0

I have define the constant on constant.cs file, Please find below code.

public const string MessageVryPhone = "xyz";

Now I have created the object on my controller and take the value in variable from constant.

static MessageDM messageDmObj = new MessageDM();

public string abc= messageDmObj.GetContextualMessage(Helper.Constants.MessageVryPhone);

While I put the abc in regular experssion ErrorMessage I am getting

an object reference is required to access non-static field, method or property error.

[RegularExpression("^\\([1-9][0-9]{2}\\)[ ][0-9]{3}[ ][-][ ][0-9]{4}$", ErrorMessage = abc)]

How to resolve this issue?

CodeNotFound
  • 22,153
  • 10
  • 68
  • 69
  • Because `RegularExpression` is an *attribute*. It's created just once, like static fields, so it can't access instance fields. In fact, the attribute is initialized during *compilation* and stored in the IL itself. It becomes part of the type's metadata – Panagiotis Kanavos Jun 22 '18 at 09:08
  • Hi @PanagiotisKanavos, Please let me know how can i access. – Sandeep Verma Jun 22 '18 at 09:13
  • I already did. Check the duplicate. You *can't* access anything at runtime from an attribute. The attribute is created at *compile time*. You can pass a type and simple values though. `RegexExpressionAttribute` uses this mechanism to load error messages from resources – Panagiotis Kanavos Jun 22 '18 at 09:14

0 Answers0