Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

  • *Experts*
Posted

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.

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

Posted

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.

  • *Experts*
Posted

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.

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

Posted

... 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)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...