Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Wrapper Class

 
Will Carpenter
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to pass a short to a method and get the short back, modified by the method. I know that won't work (call-by-value vs call-by-reference), so,
I know I need to somehow put my short into the Short Wrapper class. How?
Given:
short ansr = 1;
getTask(ansr);
switch(ansr)
{ case 1: .....etc
It's not really important, but getTask() is just a method that prompts
the user for what task they want to perform.
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not:
 
Will Carpenter
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Nathaniel! That worked.
I guess all of those initials after your name aren't just a recipe for alphabet soup!
 
Eddie Vanda
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Will,
Don't even think about using a wrapper to return a value by reference! The wrapper classes are great for conversions and Objectising values, but they don't have setter methods. That means that even though the number is passed in by reference, you cannot change it. If you point it at a new wrapper, then that wrapper's location will not be the same as that of the caller's wrapper. The new wrapper will be lost on exit from the called method.
Nathanial's answer is perfect for your situation but if you want to get values back through the argument list then you would need to write your own wrapper class where you can set and modify values. But then you would think about OOP and maybe that wrapper class would also contain other things to do with getting and validating the value???
Just something to think about.
Ed
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic