簡體   English   中英

無法將 C# 代碼移植到實現 IDataErrorInfo 的托管 C++

[英]Can't port C# code to managed C++ implementing IDataErrorInfo

這部分。 注釋代碼是cs。 在編譯時捕獲錯誤:

Error   78
error C3766: 'Sample::NoteInfo' must provide an implementation for the interface method 'System::String ^System::ComponentModel::IDataErrorInfo::default::get(System::String ^)'
d:\dev\docstreet\sample\pointInfoStoreClass.h   100

什么不正確? 為什么VS編譯器認為這沒有實現?

//Returns an error description set for the item's property
//String^ IDataErrorInfo.this[String^ columnName] {
//  get {
//      return GetColumnError(columnName);
//  }
//}

public: property String^ IDataErrorInfo[String^] {
    virtual String^ get(String^ index) override {
        return GetColumnError(index);
    }
}

完整代碼:

public ref class NoteInfo : IDataErrorInfo {
        int fDay;
        int fMonth;
        int fYear;
        int fNoteID;
        //References the item's owner
        ProjectNotes^ owner;
        //Stores error descriptions for the Day, Month, Year and NoteID properties
        Hashtable^ propertyErrors;
        //Stores an error description for the item
        String^ fNoteError;


    public: NoteInfo(int noteID, int day, int month, int year) {
            fNoteID = noteID;
            fDay = day;
            fMonth = month;
            fYear = year;
            //Set errors to empty strings
            propertyErrors = gcnew Hashtable();
            propertyErrors->Add('Day', '');
            propertyErrors->Add('Month', '');
            propertyErrors->Add('Year', '');
            propertyErrors->Add('NoteID', '');
            fNoteError = '';
            Owner = nullptr;
        }

    public: property int NoteID {
            int get() { return fNoteID; }
            void set(int value) {
                if(fNoteID == value) return;
                fNoteID = value;
            }
        }

    public: void ClearErrors() {
            SetColumnError('Day', '');
            SetColumnError('Month', '');
            SetColumnError('Year', '');
            fNoteError = '';
        }

        //Sets an error for an item's property
    public: void SetColumnError(String^ elem, String^ error) {
            if(propertyErrors->ContainsKey(elem)) {
                if((String^)propertyErrors[elem] == error) return;
                propertyErrors[elem] = error;
            }
        }

        //Gets an error for an item's property
    public: String^ GetColumnError(String^ elem) {
            if(propertyErrors->ContainsKey(elem))
                return (String^)propertyErrors[elem];
            else
                return '';
        }

        //The owner collection
    internal: property ProjectNotes^ Owner {
            ProjectNotes^ get() { return owner; }
            void set(ProjectNotes^ value) { owner = value; }
        }


#pragma region IDataErrorInfo Members

        //Returns an error description set for the item's property
        //String^ IDataErrorInfo.this[String^ columnName] {
        //  get {
        //      return GetColumnError(columnName);
        //  }
        //}

        public: property String^ IDataErrorInfo[String^] {
            virtual String^ get(String^ index) override {
                return GetColumnError(index);
            }
        }

        //Returns an error description set for the current item
    public: property String^ Error {
            virtual String^ get() { return fNoteError; }

        }
#pragma endregion
    };

C# 和 C++/CLI 語言都需要一個類的索引器的特殊名稱,以允許編譯器將其識別為索引器。 C# 使用this ,C++/CLI 使用default關鍵字。 MSDN 庫將其稱為“項目”以增加混淆,這在某種程度上是不可避免的,因為名稱取決於語言。 所以它必須看起來像這樣:

public: property String^ default[String^] {
            virtual String^ get(String^ index) {
                // etc...
            }
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM