use of exceptions or compolex classes instead of parameters by ref

  • Thread starter Thread starter Sankar Nemani
  • Start date Start date
S

Sankar Nemani

When I am designing a method that returns a value of a certain type (say Type1) and then occassionally has to return value of certian other type (say Type2), I can design the method in the following ways (I am not sure if there are more ways).
1.. Type1 foo(ref Type2 x){ //populate x occassionally}
2.. Type1 foo(){//throw an instance of Type2 instead of populating a ref parameter}
3.. Type3 foo(){} //where Type3 is a class that encapsulates a variable of Type1 and a variable of Type2
Which one is better or more elegant and why?
My initial take on this considering that the exception handling in .NET is expensive is that,

If returning a Type2 is very rare then implement use method 2 (using exception handling) and otherwise use method 3.
Especially languages such as Java do not have pass by ref (they do have reference types pass by val) so I was wondering if method 1 is less elegant or less preferred as far as good design goes.


TIA
Sankar Nemani
 
Sankar Nemani said:
[returning Type1 or Type2]

Your return type should not be a big mystery. Making your return type
inherit from Exception and "throwing" it seems quite insane. What are
you trying to do here?

P.
 
Hi Sankar,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to know the best way to return
an object. If there is any misunderstanding, please feel free to let me
know.

I think the best way to do this is to use the return value. Return value is
the most elegant way. However, if you have several objects to return, you
can try to use both return value and a reference parameter.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Back
Top