[英]Can't port C# code to managed C++ implementing IDataErrorInfo
This part.这部分。 Commented code is cs.
注释代码是cs。 On compile catch an error:
在编译时捕获错误:
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
What's incorrect?什么不正确? Why VS compiler consider this not implemented?
为什么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);
}
}
Full code:完整代码:
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
};
Both the C# and the C++/CLI languages require a special name for the indexer of a class to allow the compiler to recognize it as the indexer. C# 和 C++/CLI 语言都需要一个类的索引器的特殊名称,以允许编译器将其识别为索引器。 C# uses this , C++/CLI uses the default keyword.
C# 使用this ,C++/CLI 使用default关键字。 The MSDN Library calls it "Item" to add to the confusion, somewhat inevitably since the name depends on the language.
MSDN 库将其称为“项目”以增加混淆,这在某种程度上是不可避免的,因为名称取决于语言。 So it must look like this:
所以它必须看起来像这样:
public: property String^ default[String^] {
virtual String^ get(String^ index) {
// etc...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.