Export (0) Print
Expand All
Expand Minimize

Compiler Error CS1545

Property, indexer, or event 'property' is not supported by the language; try directly calling accessor methods 'set accessor' or 'get accessor'

Your code is consuming an object that has a default indexer and tried to use the indexed syntax. To resolve this error, call the property's get or set accessor method.

The following sample generates CS1545:

// CPP1545.cpp
// compile with: /clr /LD
#using <mscorlib.dll>
using namespace System;

public __gc class Employee {
public:
   Employee(String * s, int d) {
      _name = s;
      _dept = d;
   };

   __property String * get_name() {
      return _name;
   }

   __property int get_dept() {
      return _dept;
   }

private:
   String * _name;
   int _dept;
};

public __gc class Manager {
public:
   __property Employee * get_Report(String * s) {
      for (pEmp = Reports ; pEmp && (pEmp -> emp -> name != s);
         pEmp = pEmp -> next);
      if (pEmp)
         return pEmp -> emp;
      else
         return 0;
   }

   __property void set_Report(String* s, Employee* e) { 
     for(pEmp = Reports; pEmp && (pEmp -> emp -> name != s);
      pEmp = pEmp -> next);
      if (!pEmp) {
         EmpList * emp1 = new EmpList; 
         emp1 -> emp = e; 
         emp1 -> next = Reports; 
         Reports = emp1;
      }
   }
private:
   static Manager() {
      Reports = 0;
   }
   __gc struct EmpList {
   Employee * emp;
   EmpList * next;
   };
   EmpList * pEmp;
   static EmpList * Reports;
};

And then the C# program:

// CS1545.cs
// compile with: /r:CPP1545.dll
class x {
   public static void Main() {
      Manager Ed = new Manager();
      Employee Bob = new Employee("Bob Smith", 12);

      // track Ed's reports
      Ed.Report( Bob.name ) = Bob;   // CS1545
      // try the following line instead
      // Ed.set_Report( Bob.name, Bob);
   }
}
Show:
© 2014 Microsoft