How to not Return a value [C#]

Shaitan00

Well-known member
Joined
Aug 11, 2003
Messages
343
Location
Hell
A Function [FindIndex] is given a string that it must match in an Array and return the Index [integer]. However if the Index is not found I do not want to return an integer.

I have tried returned false, null [give errors indicating that cannot convert false, null to integer] and even either omitting return or putting an EXIT command [gives errors indicating not all paths return a value].

However any value returned will cause an error because it does not correspond properly.
How would I go about handling such a situation?
 
Id reccomend returning -1 for an index that could not be found.
This is how index-returning methods of the .NET framework
handle the situation. Just check the return value to see if its -1
before continuing.
 
Wish it were that simple, currently all intger values are mapped [including -1], it is not a normal array [handmade which is indexed from - to + infiniti.

Currently I return -100 which consequently removes array[-100] from the list of indexes I can use [not the best idea] so I wanted to know if there was another approach that would not require loosing one integer value as a return trap.
 
If thats the case, the best method I can think of is to throw an
exception (preferrably create your own) inside the method and
then just catch it outside the method call.
 
... or you return Integer.MaxValue/.MinValue - not that beautiful but also a possibility.. double.NaN would also be a good value.. but not working when using integers...

I guess Buckys solution is the best (throw an Exception)
 
Back
Top