Results 1 to 11 of 11

Thread: Doing something when a variable is changed

  1. #1

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827

    Doing something when a variable is changed

    I have a CGButton class again and then I have four variables (left, top, width, height). When the user changes any of these variables, I want to move the button using API. Is it even possible or will I have to use a function like "Move", "Resize", or something else?
    Baaaaaaaaah

  2. #2
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    You can do it with MoveWindow I guess, look it up at MSDN...
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  3. #3
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    Make sure you use Properties rather than just a public variable...
    than you should be fine...
    you know how to call MoveWindow right, so make a property like this:

    PHP Code:
    property void set_Left (int value){
    //MoveWindow with the new value of Left

    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  4. #4

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    The keyword "property" doesn't exist.
    Baaaaaaaaah

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    __declspec(property) comes into it somewhere.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  6. #6
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771
    You should make a class with get_variable(value) and set_variable(value).

    The problem with __declspec(property) is that it is MSVC specific, and thus not portable.

    You can use this macro, to make the code portable (and make it look a little more ugly):
    Code:
    #include <iostream>
    
    // The PPLG macro allows you to 
    //  make public memember variables
    //  with custom get/let functions
    #define PPLG(TYPE, VARNAME) \
    private: \
        class VARNAME{ \
    	public: \
            inline TYPE operator = (TYPE param) {return let(param);} \
    		inline operator TYPE () {return get();}; \
    	private: \
    	    TYPE val; \
    		TYPE let (TYPE param); \
    		TYPE get (); \
    	}; \
    public: \
    	VARNAME VARNAME;
    
    
    // A test class
    class a{ 
    public:
    	PPLG(int,b); //We have a member 'b', of type int
    };
    
    
    // The get & let functions
    int a::b::get(){
    	return val;
    }
    int a::b::let(int param){
    	std::cout << "Variable set to " << param << std::endl;
    	return val=param;
    }
    
    // A sample main function
    int main(){
    	a test_a;
    	test_a.b = 10;
    	test_a.b = test_a.b + 1;
    	return 0;
    }

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    I always found my preferred method (non-declspec) to be:
    Code:
    class test {
    public:
        inline test() : m_propname(0) { }
    
        inline int propname() const { return m_propname; }
        void propname(int pn) { m_propname = pn; }
    
    private:
        int m_propname;
    }
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  8. #8
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771
    parksie: Something like that is probably better. My macro-using solution was only to show that it's posible to do it. It will make the program harder to understand, since it uses sub-classes hidden away in macros.

  9. #9
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Anything is possible Whether you should do it is another matter

    I think macros are "discouraged" in C++ for the reasons you gave.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  10. #10

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    Ok thanks. I guess I'll the use the method of set_property or get_property because it's pretty straight forward.
    Baaaaaaaaah

  11. #11
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    hmm I suggest Parksie's way to do it... that looks almost like a property...
    the thing I suggested was for Managed C++ (.NET), I didn't realize it was the Managed version... don't use it
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width