Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++/CLI shorthand properties

Tags:

How does a developer do the equivalent of this in managed c++? :

c# code

public String SomeValue {   get;   set; } 

I've scoured the net and found some solutions, however it is hard to distinguish which is the correct (latest, .NET 3.5) way, given the colourful history of getters/setters and managed c++.

Thanks!

like image 550
DanDan Avatar asked Dec 15 '09 14:12

DanDan


1 Answers

Managed C++ does not support automatic properties. You should manually declare a backing field and the accessors:

private: String* _internalSomeValue; public: __property String* get_SomeValue() { return _internalSomeValue; } __property void set_SomeValue(String *value) { _internalSomeValue = value; } 

C++/CLI supports automatic properties with a very simple syntax:

public: property String^ SomeValue; 

Update (reply to comment):

In C++/CLI, you cannot control the accessibility of each accessor method separately when you use the automatic property syntax. You need to define the backing field and the methods yourself:

private: String^ field; property String^ SomeValue {     public: String^ get() { return field; }    private: void set(String^ value) { field = value; } } 
like image 139
mmx Avatar answered Oct 28 '22 09:10

mmx