Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Passing Values Between Methods 2!!!  RSS feed

 
Sam Bluesman
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK. Earlier i found out how to pass a value from one method to another. Which worked fine using the template:



But now i am faced with needing to pass more values to a single method from more than one method. For example,



So in the above example, i want to pass two values into the third method - print - but i dont know how. Any ideas?

Please note that this is just a trivial example to show you what i want to do.
[ October 31, 2005: Message edited by: Sam Bluesman ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sam,

The basic shape of the solution will involve the value computed in CalculateSequenceLength() being stored somewhere, and then CalculateSequenceLength2() (or some other code) retrieving that stored value and using it, along with additional information, to call print(). Now, that somewhere might be a local variable somewhere, a member variable of the class we're looking at, or an instance of some other class, perhaps the one that print() is in. There are lots of ways to do this, but which is the best choice depends in some detail of what exactly you're trying to do.

One simple way:

 
Sam Bluesman
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, just one question...

is ALL of what you are saying above assuming that only one value is calculated per method? I need to pass, from two methods, MORE than one calculated value from each method into a 3rd method you see...

Thanks
[ October 31, 2005: Message edited by: Sam Bluesman ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If a method needs to return several values of the same type, then you can have that method return an array:



If the method should return multiple values of different types, then that collection of return values can be encapsulated in a class. For example, if you were returning several measurements on a String instead of just its length, then you'd want to define a class called "StringMetrics", or something along those lines, and have the method return an instance of StringMetrics containing all the data. Then the caller of the method would be able to ask the StringMetrics object for each piece of information individually.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!