2016-07-02 16 views
0

的属性我有2 REF类在C++/CLI:C++/CLI:如何写入属性

>第一类:

public ref class wBlobFilter 
    { 
     int mMin; 
     int mMax; 
     bool mIsActive; 

    public: 

     // min value 
     property int Min 
     { 
      int get() {return mMin;} 
      void set(int value) {mMin = value;} 
     } 

     // max value 
     property int Max 
     { 
      int get(){return mMax;} 
      void set(int value){mMax = value;} 
     } 

     // set to true to active 
     property bool IsActive 
     { 
      bool get() {return mIsActive;} 
      void set(bool value){mIsActive = value;} 
     } 
    }; 

>第二类:

public ref class wBlobParams 
    { 
     wBlobFilter mFilter; 

    public: 

     property wBlobFilter Filter 
     { 
      wBlobFilter get() {return mFilter;} 
      void set(wBlobFilter value) { mFilter = value; } 
     } 
    }; 

当我把它在C#我得到一个错误信息:

 Params.Filter.Min = 0; 

所以,我怎样才能设置类wBlobFilter的成员变量的值“因为它不是一个变量不能修改返回值” class wBlobParams的属性?对不起,我的英语不好。谢谢!!!

+2

您不应该在cli代码中使用'wBlobFilter ^'而不是'wBlobFilter'吗? – stijn

+0

如果继承,那么过滤器的属性将可用。 public ref class wBlobParams:public wBlobFilter {...};我无法分辨这是你以后的样子。 – tukra

+0

我试过了,但它不是我的问题。 \t 我不想从wBlobFilter继承wBlobParams,wBlobFilter只是wBlobParams的成员,但我想通过wBlobParams的属性为它设置值。像这样: 'Params.Filter.Min = 0' – DungTv

回答

0

很难知道你到底想要发生什么。如果它继承,则过滤器的属性将可用。

public ref class wBlobParams : public wBlobFilter 
{}; 

void f(wBlobParams^ params) { 
    auto max = params->Max; 
} 

或复制在wBlobParams属性访问:

public ref class wBlobParams { 
public: 
    wBlobFilter^ mFilter; 

    property int Max { 
     int get() { return mFilter->Max; } 
    } 
}; 

void f(wBlobParams^ params) { 
    auto max = params->Max; 
} 

编辑1:
看这个。你在做什么都很好。只是你使用gc句柄的语法是错误的。

public ref class cA { 
    int x; 

public: 
    cA() : x(0) {} 

    property int X { 
     int get() { return x; } 
     void set(int _x) { x = _x; } 
    } 
}; 

public ref class cB { 
    cA^ a; 
public: 
    cB() : a(gcnew cA()) {} 

    property cA^ A { 
     cA^ get() { return a; } 
     void set(cA^ _a) { a = _a; } 
    } 
}; 

void main() { 
    cB^ b = gcnew cB(); 
    b->A->X = 5; 
    Console::WriteLine(b->A->X); 
} 
+0

我不想从wBlobFilter继承wBlobParams,wBlobFilter只是wBlobParams的成员,但我想通过wBlobParams的属性为它设置值。像这样: 'Params.Filter.Min = 0;' – DungTv

+0

查看我的编辑 – tukra

+0

@tuka:非常感谢。我明白了。 它是错误的如果我改变类cA从ref到值? – DungTv