-
C++/CLI Property
Suppose I declare some variables in a class...
Code:
public:
String ^name;
int ilImageName;
int id;
TEX_TYPE type;
TEX_FORMAT format;
I can set these variables in the class and whatnot. However, I can also create property types to set these class varibles, like this:
Code:
protected:
String ^name;
int ilImageName;
int id;
TEX_TYPE type;
TEX_FORMAT format;
public:
property String ^Name{
String ^get() { return(name); }
void set(String ^value) { name = value; }
}
property int ID{
int get() { return(id); }
void set(int value) { id = value; }
}
property TEX_TYPE Type{
TEX_TYPE get() { return(type); }
void set(TEX_TYPE value) { type = value; }
}
property TEX_FORMAT Format{
TEX_FORMAT get() { return(format); }
void set(TEX_FORMAT value) { format = value; }
}
My question here is; is there any real point to using properties? And if not this, what exactly would you use properties for?
Thanks.
-
Sorry about putting this in the wrong forum first. I wasn't sure whether to put it here in the Windows forum, or in the C++ forum. At least i'll know for next time. :)
-
I think all of us are trying to figure out just where to put CLI. I know where I'd like to 'put' it.
-
The good thing about properties is that you can do additional processing of values that users store or retrieve. For example, say you have a property called Age. If you would make Age a public member, then any value could be stored there, which might lead to errors. You don't want people to store negative values there or numbers like 1560. So your Set() function might look something like this:
Code:
void Set(String^ value) {
if (value < 0 || value > 120)
value = 0;
age = value;
}
This is as simple as it can get. In a more a complex applications, there would always be need for checking of values and additional processing.
-
So it's basically like writing a custom handler for variable assigment?
If so, then in my particular case, shown in my first post, using properties would probably be overkill, since I have no need of additional processing.
-