Shaitan00 Posted February 21, 2004 Posted February 21, 2004 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? Quote
*Experts* Bucky Posted February 21, 2004 *Experts* Posted February 21, 2004 I'd 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 it's -1 before continuing. Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
Shaitan00 Posted February 22, 2004 Author Posted February 22, 2004 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. Quote
*Experts* Bucky Posted February 23, 2004 *Experts* Posted February 23, 2004 If that's 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. Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
Hamburger1984 Posted February 23, 2004 Posted February 23, 2004 ... 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 Bucky's solution is the best (throw an Exception) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.