CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 29
  1. #1
    Join Date
    Jan 2016
    Posts
    61

    pass variable value from one dialogbox to other/ one class to other class in vc++ MFC

    hello,

    I am trying to pass variable of one class to other class, i did this by calling that doalogbox using DoModal method . But it give retry,Abort,ignore type of messagebox/ Exception.
    Is there any way to access variable of one class to other class.

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,923

    Re: pass variable value from one dialogbox to other/ one class to other class in vc++

    Is there any way to access variable of one class to other class.
    Class A provides a get() method, Class B provides a set() method. If anything more is needed, then details of the classes involved would be helpful.

    If Class B is derived from Class A and the variable needs to be private in A, then consider making it protected so B can access it but it still remains private outside of the classes.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Jan 2016
    Posts
    61

    Re: pass variable value from one dialogbox to other/ one class to other class in vc++

    Quote Originally Posted by 2kaud View Post
    Class A provides a get() method, Class B provides a set() method. If anything more is needed, then details of the classes involved would be helpful.

    If Class B is derived from Class A and the variable needs to be private in A, then consider making it protected so B can access it but it still remains private outside of the classes.
    if they are not reletaed by inheritance ?

  4. #4
    Join Date
    Jan 2016
    Posts
    61

    Re: pass variable value from one dialogbox to other/ one class to other class in vc++

    Quote Originally Posted by [email protected] View Post
    if they are not reletaed by inheritance ?

    if we able do this unless they are not related by inheritance means they are independent class then how to do this. means have we need to create a object of class and call get method?
    plz give one example. Thanx for quick reply

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: pass variable value from one dialogbox to other/ one class to other class in vc++

    Quote Originally Posted by [email protected] View Post
    hello,

    I am trying to pass variable of one class to other class, i did this by calling that doalogbox using DoModal method . But it give retry,Abort,ignore type of messagebox/ Exception.
    Is there any way to access variable of one class to other class.
    http://forums.codeguru.com/showthrea...se-Versa-in-VC
    http://forums.codeguru.com/showthrea...tbox-to-Dialog
    Victor Nijegorodov

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: pass variable value from one dialogbox to other/ one class to other class in vc++

    Quote Originally Posted by [email protected] View Post
    hello,

    I am trying to pass variable of one class to other class, i did this by calling that doalogbox using DoModal method . But it give retry,Abort,ignore type of messagebox/ Exception.
    Is there any way to access variable of one class to other class.
    You may also want to read this excellent J.Newcomer's essay about Dialog and Control Design as well as a lot of his other useful articles: http://www.flounder.com/mvp_tips.htm#Dialog Box Series
    Victor Nijegorodov

  7. #7
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,923

    Re: pass variable value from one dialogbox to other/ one class to other class in vc++

    plz give one example. Thanx for quick reply
    Consider this very simple example
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    template<typename T>
    class A {
    private:
    	T pa;
    
    public:
    	A(T a) : pa(a) {}
    	T geta() const {
    		return pa;
    	}
    
    	void seta(const T& a) {
    		pa = a;
    	}
    };
    
    template<typename T>
    class B {
    private:
    	T pb;
    
    public:
    	B(T b) : pb(b) {}
    	T getb() const {
    		return pb;
    	}
    
    	void setb(const T& b) {
    		pb = b;
    	}
    };
    
    int main()
    {
    	A<string> a("This is A"s);
    	B<string> b("This is B"s);
    
    	cout << "a = " << a.geta() << ", b = " << b.getb() << endl;
    	b.setb(a.geta());
    	cout << "a = " << a.geta() << ", b = " << b.getb() << endl;
    }
    This displays
    Code:
    a = This is A, b = This is B
    a = This is A, b = This is A
    as the private variable of B has been set to that of A.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  8. #8
    Join Date
    Jan 2016
    Posts
    61

    Re: pass variable value from one dialogbox to other/ one class to other class in vc++

    There is no inheretance no parent class.

  9. #9
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: pass variable value from one dialogbox to other/ one class to other class in vc++

    Quote Originally Posted by [email protected] View Post
    There is no inheretance no parent class.
    Well it was the reply to your original question where you asked nothing about "inheretance":
    Quote Originally Posted by [email protected] View Post
    hello,

    I am trying to pass variable of one class to other class, i did this by calling that doalogbox using DoModal method . But it give retry,Abort,ignore type of messagebox/ Exception.
    Is there any way to access variable of one class to other class.
    Victor Nijegorodov

  10. #10
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: pass variable value from one dialogbox to other/ one class to other class in vc++

    There's nothing magic about dialog boxes. You pass variables around the same way you do with any other objects. You can write set and get methods or if it's public, just set the member directly.

    CMyDialog dlg;
    dlg.m_intVariable = 7;

    What's the confusion?

  11. #11
    Join Date
    Jan 2016
    Posts
    61

    Re: pass variable value from one dialogbox to other/ one class to other class in vc++

    Quote Originally Posted by GCDEF View Post
    There's nothing magic about dialog boxes. You pass variables around the same way you do with any other objects. You can write set and get methods or if it's public, just set the member directly.

    CMyDialog dlg;
    dlg.m_intVariable = 7;

    What's the confusion?

    in CMyDialog1 class file:
    m_intVariable =7;

    and in CMyDialog2 class file:
    int value;
    CMyDialog1 dlg;
    value= dlg.m_intVariable ;

    if we do not call DoModal method before assignment then dlg.m_intVariable is empty.
    Last edited by [email protected]; January 17th, 2016 at 07:32 AM.

  12. #12
    Join Date
    Jan 2016
    Posts
    61

    Re: pass variable value from one dialogbox to other/ one class to other class in vc++

    Quote Originally Posted by 2kaud View Post
    Consider this very simple example
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    template<typename T>
    class A {
    private:
    	T pa;
    
    public:
    	A(T a) : pa(a) {}
    	T geta() const {
    		return pa;
    	}
    
    	void seta(const T& a) {
    		pa = a;
    	}
    };
    
    template<typename T>
    class B {
    private:
    	T pb;
    
    public:
    	B(T b) : pb(b) {}
    	T getb() const {
    		return pb;
    	}
    
    	void setb(const T& b) {
    		pb = b;
    	}
    };
    
    int main()
    {
    	A<string> a("This is A"s);
    	B<string> b("This is B"s);
    
    	cout << "a = " << a.geta() << ", b = " << b.getb() << endl;
    	b.setb(a.geta());
    	cout << "a = " << a.geta() << ", b = " << b.getb() << endl;
    }
    This displays
    Code:
    a = This is A, b = This is B
    a = This is A, b = This is A
    as the private variable of B has been set to that of A.

    I want to perfom as I asked my previous question how to get text of selected item in list control

    1] Select one iteam in list control
    2] store it into variable
    3] access that variable with it's containing value into other class (which I have to pass to SQLquery in code )

    now just want to know that how pass that value and get into other class ?

  13. #13
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,923

    Re: pass variable value from one dialogbox to other/ one class to other class in vc++

    1) and 2) have already been discussed in your other thread.

    For 3), for the class that stores the text variable, provide a get method to return the value. For the other class access the value via this get method. If this can't be done, then further details of the classes would be useful.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  14. #14
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,923

    Re: pass variable value from one dialogbox to other/ one class to other class in vc++

    Code:
    void Cselect_product::OnItemchangedListCategory(NMHDR* pNMHDR, LRESULT* pResult)
    {
    	POSITION pos = m_klist.GetFirstSelectedItemPosition();
    	if (pos == NULL)
    		TRACE0("No items were selected! \n");
    	else
    		while (pos)
    		{
    			int nItem = m_klist.GetNextSelectedItem(pos);
    			TRACE1("Item %d was selected! \n", nItem);
    			//you could do your own processing on nItem here
    
    			strText = m_klist.GetItemText(nItem, 0);
                            //Update the edit control with this value ??
    		}
    
    	*pResult = 0;
    }
    From the other thread, this is the code to get the value of the selected item(s). Rather than just store the value in strText (which doesn't seem right if multiple items are selected?), don't you need to have code here that updates your edit control with the value??
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  15. #15
    Join Date
    Jan 2016
    Posts
    61

    Re: pass variable value from one dialogbox to other/ one class to other class in vc++

    Quote Originally Posted by 2kaud View Post
    1) and 2) have already been discussed in your other thread.

    For 3), for the class that stores the text variable, provide a get method to return the value. For the other class access the value via this get method. If this can't be done, then further details of the classes would be useful.

    in CMyDialog1 class file:
    m_intVariable =7;

    and in CMyDialog2 class file:
    int value;
    CMyDialog1 dlg;
    value= dlg.m_intVariable ;

    if we do not call DoModal method before assignment then dlg.m_intVariable is empty.

    please give me simple solution for this.

Page 1 of 2 12 LastLast

Tags for this Thread

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

Featured