CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Nov 2010
    Posts
    13

    Getting the value of a variable in another class

    Hey codegurus,

    I'm quite new to programming, and i have a small problem. I have an application where i want to declare a variable in a function, and then use the value from that variable in a function in a different class.


    I have made a very simple example so it's easy for you to give feedback.
    It's programmed in Qt (that's the reason for MainWindow, Widgets etc.)




    I hope it's not too simple for you guys,
    Thanks in advance.

    Best regards,

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Getting the value of a variable in another class

    Quote Originally Posted by 99999 View Post
    I'm quite new to programming, and i have a small problem. I have an application where i want to declare a variable in a function, and then use the value from that variable in a function in a different class.

    I have made a very simple example so it's easy for you to give feedback.
    It's programmed in Qt (that's the reason for MainWindow, Widgets etc.)
    If you don't know how to pass a simple value from one class to another and you are programming GUI application, you are in over your head.

    You can't return a value from a function if you declare the return type as void. Declare the return type as int instead and you can store the value when calling the function.
    Code:
    int foo()
    {
        return 12;
    }
    
    void bar()
    {
        int x = foo();
        // do something with x
    }
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #3
    Join Date
    Nov 2010
    Posts
    13

    Re: Getting the value of a variable in another class

    Thanks for a quick reply.

    But, my function calls a gps to get a longtitude and a latitude where it puts the values into two variables. I want to be able to use both of these values in another function in another class.
    How do i do that? As i see it i can't just make an int and return two values.

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Getting the value of a variable in another class

    Talking about "another class" is beside the point. If you need to use state from one object inside an operation for a different object----whether the two objects are of the same class or not----then you (a) need to pass a copy, reference, const reference, or pointer to the object with the relevant state into the method which needs it, and (b) the object with the relevant state must have an accessor method to get at the relevant state, or that state must be public. (In the special case when the two objects are of the same type, you can get at private state directly. However it's still often a good idea to use the public interface if it's available.)
    Last edited by Lindley; December 10th, 2010 at 10:02 AM.

  5. #5
    Join Date
    Nov 2010
    Posts
    13

    Re: Getting the value of a variable in another class

    Hey, i just tried something which compiled. It just didn't get the value returned from "me".
    If i want to use 2 values from 2 variables from another function how do i do that.
    As an example, what is wrong with the code in the picture if i want to be able to use the values from x and from y in the function meto.



    Im very thankfull you take the time to help a noob like me on the right way!

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Getting the value of a variable in another class

    Why not just make x and y members of the MainWindow class, rather than having them local to me()?

  7. #7
    Join Date
    Nov 2010
    Posts
    13

    Re: Getting the value of a variable in another class

    Quote Originally Posted by Lindley View Post
    Why not just make x and y members of the MainWindow class, rather than having them local to me()?
    Hey, good idea. But even if i did that, how would i return the two values they get in me and and use them in meto?

    I believe i still have to make an object, but when i make an object i believe i can't just set int y = obj since me returns two variables. So, lets say that in the function me, x and y gets the value 10 and 12. How do i get these values into meto and assign them to two new variables?

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Getting the value of a variable in another class

    Quote Originally Posted by 99999 View Post
    Hey, good idea. But even if i did that, how would i return the two values they get in me and and use them in meto?

    I believe i still have to make an object, but when i make an object i believe i can't just set int y = obj since me returns two variables. So, lets say that in the function me, x and y gets the value 10 and 12. How do i get these values into meto and assign them to two new variables?
    Both me() and meto() are methods of a MainWindow object. If you assign values to variable members of the object in me(), then those members will of course still be available in meto(). No return is required. The only danger is that you're imposing a dependency between the two methods; you need to carefully design your program so that calling them in the wrong order won't crash your code or cause other undesirable behavior.

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

    Re: Getting the value of a variable in another class

    Quote Originally Posted by 99999 View Post
    Hey, i just tried something which compiled. It just didn't get the value returned from "me".
    If i want to use 2 values from 2 variables from another function how do i do that.
    As an example, what is wrong with the code in the picture if i want to be able to use the values from x and from y in the function meto.



    Im very thankfull you take the time to help a noob like me on the right way!
    Once you hit the first return statement the function is done. You can't get to the second one. If you need to return two variables, one option is to use output parameters.

    Output parameters are arguments that are passed into a function by the calling function and populated by the called function. They have to be passed in by reference.

    Code:
    void FunctionWithOutputParameters(int& Param1, int&Param2)
    {
        Param1 = 5;
        Param2 = 6;
    }
    
    //then call to call it
    int n1, n2;
    FunctionWithOutputParameters(n1, n2);

  10. #10
    Join Date
    Nov 2010
    Posts
    13

    Re: Getting the value of a variable in another class

    Quote Originally Posted by GCDEF View Post
    Output parameters are arguments that are passed into a function by the calling function and populated by the called function. They have to be passed in by reference.

    Code:
    void FunctionWithOutputParameters(int& Param1, int&Param2)
    {
        Param1 = 5;
        Param2 = 6;
    }
    
    //then call to call it
    int n1, n2;
    FunctionWithOutputParameters(n1, n2);
    Perfect, this is exactly what i needed!!! Now my app is up running

    Thanks again!

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