VC.NET 2005中关于Property的用法
在新的VC2005中,增加了不少关于property的新的语法,害的我百度了半天才找到解决方法,现在列出来以供参考。
标准property
定义:
ref class DefProperty
{
private:
String^ mData;
public:
property String^ MyData
{
String^ get();
void set(String^);
}
};
实现:
String^ DefProperty::MyData::get()
{
return mData;
}
void DefProperty::MyData::set(String^ value)
{
mData = value;
}
简化的property
ref class EasyProperty
{
public:
property String^ MyData;
};
静态property
定义:
ref class StaticProperty
{
private:
static String^ mData;
public:
static property String^ Data1;
static property String^ Data2
{
String^ get();
void set(String^);
}
};
实现:
String^ StaticProperty:Data2::get()
{
return mData;
}
void StaticProperty:
ata2::set(String^ value)
{
mData = value;
}
虚拟property
定义:
interface class IProperty
{
public:
property String^ Data1;
property String^ Data2
{
String^ get();
void set(String^);
}
};
ref class VirtualProperty : public IProperty
{
protected:
String^ mData2;
String^ mData3;
public:
property String^ Data1;
property String^ Data2
{
String^ get();
void set(String^);
}
property String^ Data3
{
virtual String^ get();
virtual void set(String^);
}
};
ref class VirtualPropertyNew : public VirtualProperty
{
public:
property String^ Data2
{
String^ get();
void set(String^);
}
property String^ Data3
{
virtual String^ get();
virtual void set(String^);
}
};
实现:
String^ VirtualProperty:
ata2::get()
{
return mData2->Insert(0,"Base2:");
}
void VirtualProperty:
ata2::set(System::String ^ value)
{
mData2 = value;
}
String^ VirtualProperty:
ata3::get()
{
return mData3->Insert(0,"Base3:");
}
void VirtualProperty:
ata3::set(System::String ^ value)
{
mData3 = value;
}
String^ VirtualPropertyNew:
ata2::get()
{
return mData2->Insert(0,"New2:");
}
void VirtualPropertyNew:
ata2::set(System::String ^ value)
{
mData2 = value;
}
String^ VirtualPropertyNew:
ata3::get()
{
return mData3->Insert(0,"New3:");
}
void VirtualPropertyNew:
ata3::set(System::String ^ value)
{
mData3 = value;
}
索引property
ref class IdxProperty
{
public:
property int default[int]
{
int get(int idx)
{
return 0;
}
void set(int idx, int value)
{
}
}
property int default[String^]
{
int get(String^ idx)
{
return 0;
}
void set(String^ idx, int value)
{
}
}
property int ItemSlot[int]
{
int get(int idx)
{
return 0;
}
void set(int idx, int value)
{
}
}
property int Data3D[int,int,int]
{
int get(int x,int y,int z)
{
return 0;
}
void set(int x,int y,int z, int v)
{
}
}
};
标准property
定义:
ref class DefProperty
{
private:
String^ mData;
public:
property String^ MyData
{
String^ get();
void set(String^);
}
};
实现:
String^ DefProperty::MyData::get()
{
return mData;
}
void DefProperty::MyData::set(String^ value)
{
mData = value;
}
简化的property
ref class EasyProperty
{
public:
property String^ MyData;
};
静态property
定义:
ref class StaticProperty
{
private:
static String^ mData;
public:
static property String^ Data1;
static property String^ Data2
{
String^ get();
void set(String^);
}
};
实现:
String^ StaticProperty:Data2::get()
{
return mData;
}
void StaticProperty:

{
mData = value;
}
虚拟property
定义:
interface class IProperty
{
public:
property String^ Data1;
property String^ Data2
{
String^ get();
void set(String^);
}
};
ref class VirtualProperty : public IProperty
{
protected:
String^ mData2;
String^ mData3;
public:
property String^ Data1;
property String^ Data2
{
String^ get();
void set(String^);
}
property String^ Data3
{
virtual String^ get();
virtual void set(String^);
}
};
ref class VirtualPropertyNew : public VirtualProperty
{
public:
property String^ Data2
{
String^ get();
void set(String^);
}
property String^ Data3
{
virtual String^ get();
virtual void set(String^);
}
};
实现:
String^ VirtualProperty:

{
return mData2->Insert(0,"Base2:");
}
void VirtualProperty:

{
mData2 = value;
}
String^ VirtualProperty:

{
return mData3->Insert(0,"Base3:");
}
void VirtualProperty:

{
mData3 = value;
}
String^ VirtualPropertyNew:

{
return mData2->Insert(0,"New2:");
}
void VirtualPropertyNew:

{
mData2 = value;
}
String^ VirtualPropertyNew:

{
return mData3->Insert(0,"New3:");
}
void VirtualPropertyNew:

{
mData3 = value;
}
索引property
ref class IdxProperty
{
public:
property int default[int]
{
int get(int idx)
{
return 0;
}
void set(int idx, int value)
{
}
}
property int default[String^]
{
int get(String^ idx)
{
return 0;
}
void set(String^ idx, int value)
{
}
}
property int ItemSlot[int]
{
int get(int idx)
{
return 0;
}
void set(int idx, int value)
{
}
}
property int Data3D[int,int,int]
{
int get(int x,int y,int z)
{
return 0;
}
void set(int x,int y,int z, int v)
{
}
}
};