如何使用属性设置和获取? [英] How to use property Set and Get?

查看:85
本文介绍了如何使用属性设置和获取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用属性设置和获取数组?

How do I use property to set and get an array?

ref class test{
private:
  stactic array<string^>^my=gcnew array<string^>(5);

public:
  static property array<string^>^MY{//what i need to do here?} 
  
};

推荐答案

public ref class Class1
{
private:
    static array<String^>^ _myProperty;

public:
    static property array<String^>^ MyProperty
    {
        array<String^>^ get()
        {
            return _myProperty;
        }

//private: // probably a good idea, depending on your requirements
        void set(array<String^>^ value)
        {
            _myProperty = value;
        }

        //void set(array<String^>^ value);
    }
};

//void Class1::MyProperty::set(array<String^>^ value)
//{
//    _myProperty = value;
//}



注释的代码是另一种有效的方法. string在C ++/CLI中不是C#中的关键字,因此您必须使用类名String.您可能还考虑不使用该属性的公共"集合访问器,因为这样做是允许其他实体替换整个数组引用,而您可能只想让它们访问数组项(假设使用set方法)设置背景字段).有关C ++/CLI中属性的更多信息,请在此处 [



The commented code is another valid way to do it. string is not a keyword in C++/CLI as in C#, so you have to use the class name String. You may also consider not having a "public" set accessor for the property since by doing so you''re allowing other entities to replace the whole array reference while you probably want to just give them access to the array items (assuming the set method to be setting the backing field). More information regarding properties in C++/CLI is here[^].


这篇关于如何使用属性设置和获取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆