Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Passing method returned values between class

    Is there a way to pass the value of a specific method from one class to another? For instance if i wanted to take the results of this:
    Code:
     void withdraw(double amt){
    balance = balance - amt;
     if (balance < 0) { 
    sendWarning();
    applyOverdraftFee();
    }
    }
    And use it in another class like this:
    Code:
     public String errorDescription(double balance, BankAccount toAccount)
    	  {
     BankAccount method = new BankAccount(balance, acctNumber, acctNumber, acctNumber);
    		this.toAccount = toAccount;
    if(method.balance >0){ 				
    return "ERR2556B - insufficient funds"; 			} 			
    else 
    return "ERR2312A - not authorized to make this transfer amount";
    Basically I am trying to take what ever value is returned from the withdraw method and use it in my other method which is located in another class. If the withdraw method returns a value less then 0, the errorDescription will take that value and print ERR2556B - insufficient funds.
    The second class can not be extended if that is an option since its already being extended from another class.

  • #2
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Set up a get property in the class that has the withdraw method to return the balance and pass that into the class that has the error description method.

    I don't do Java but the logic should be straight forward:
    PHP Code:
    // not sure if Java has properties like c#, back in the day when I used Java it was getter/setter methods
    public double getBalance()
    {
        return 
    this.balance;
    }

    // pass that into your class that has error description 
    Good luck;

  • #3
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you i figured out what the problem was using another way. Thank you for your help though!

  • #4
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Cool, what did you do?

  • #5
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I called a get method from its parent class that has the same values returned.
    Now all i have to do is remember how to convert my array implementation to ArrayList


  •  

    Posting Permissions

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