We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,325 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

For Each Loop

Hi,

Why does this piece of code throw me an error saying: cannot return a value from method whose result type is void?

public void orderTotal()
       {
       int total;

       for (Order filename: Orders) {

         total = filename.getQuantity(); 


        }

        return total;
   }
2
Contributors
4
Replies
30 Minutes
Discussion Span
6 Months Ago
Last Updated
5
Views
Question
Answered
KRUX17
Light Poster
44 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Please post the full text of the error message that shows where the error occurs.
What statement causes the exception?

method whose result type is void?

The posted method is defined with return type of void on line 1. It can not return a value on line 12.
Change its return type to be the same type as total if you want to return a value.

BTW total won't be an accumulation of values, it will hold the last value received in line 7

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

The error occurs on the return total; The getQuantity() method is being called from another class.

KRUX17
Light Poster
44 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

We cross posted. See my last post.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

Thank you, I can't believe I didn't see that.

KRUX17
Light Poster
44 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 6 Months Ago by NormR1

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0656 seconds using 2.65MB