Search code examples
c#static-functions

Access to a defined property from static function


I would like to define a property in a static class, then access to this property from another static class. For this aim I have defined such a property:

public static class First
{
    public static void Run(string name)
      {
         xzFileName=name
         //my code here
       }
    public static string xzFileName
      {
         get
          {
            return xzFileName;
          }
         set
          {
            xzFileName=value;
          }   
       }
}

But I get an exception in this row xzFileName=value. Can someone tell me what is wrong with my code?


Solution

  • You have a circular reference. You're trying to set the property from within the property's setter, which causes an infinite loop.

    Change it to use auto-implemented properties:

    public static class First
    {
        public static void Run(string name)
          {
             xzFileName=name;
             //my code here
           }
        public static string xzFileName { get; set;  }
    }