C++/CLI Property

This is a discussion on C++/CLI Property within the Windows Programming forums, part of the Platform Specific Boards category; Suppose I declare some variables in a class... Code: public: String ^name; int ilImageName; int id; TEX_TYPE type; TEX_FORMAT format; ...

  1. #1
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,070

    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.
    Memorial University of Newfoundland
    Computer Science

    Mac and OpenGL evangelist.

  2. #2
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,070
    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.
    Memorial University of Newfoundland
    Computer Science

    Mac and OpenGL evangelist.

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    I think all of us are trying to figure out just where to put CLI. I know where I'd like to 'put' it.

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    30
    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.

  5. #5
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,070
    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.
    Memorial University of Newfoundland
    Computer Science

    Mac and OpenGL evangelist.

  6. #6
    Registered User
    Join Date
    Jun 2006
    Posts
    30
    That's correct.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. static property
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 06-12-2008, 01:03 AM
  2. Problem with a file parser.
    By Hulag in forum C++ Programming
    Replies: 7
    Last Post: 03-17-2005, 08:54 AM
  3. Creating a modeless property sheet
    By axr0284 in forum Windows Programming
    Replies: 6
    Last Post: 01-12-2005, 05:29 AM
  4. Dialog Box & Property Sheet :: MFC
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 08-01-2002, 01:33 PM
  5. Property Sheets :: MFC
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 05-09-2002, 03:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21