ComboBox ItemIndex and ID

Hi folks

Just a short question about the ComboBox class.
What is the difference between the ItemIndex and the ID.

Thanks for the help!

LukeS

the index is the place of the item in the list, in the range 0 to getNumItems() - 1

the ID is an identifier you specify when you add an item, it can be whatever you want except 0 (0 is returned by getSelectedId() when nothing is selected).

When you write the code for handling a response to an Item selection, it’s better to use the ID so that it doesn’t depend on the order in which elements were added to the list.

I wish juce would use -1 for errors and not 0. It would be a lot cleaner in my gui code.

1 Like

When dealing with IDs, I always think of 0 as being the only number that should be avoided, as you could legitimately have valid negative IDs. How would it help make your code neater?

When dealing with IDs, I always think of 0 as being the only number that should be avoided, as you could legitimately have valid negative IDs. How would it help make your code neater?[/quote]

I usually use ids to map back to the index in an array of the source data. Since you can’t use zero, it always means +1 to the index and -1 to the id every time you are using it. I usually forget somewhere.

I usually use 0 for success, and anything else being an error, with the number indicating what error
 Blame it on my old C days without exception handling
 :slight_smile:

Really? Even in this kind of thing:

if ( doSomething() ){
doSomethingElse();
}

Dude. Trippy.

It was usually in the ways of something like this:

errorEnum result; if(FAILED(result=someFunc())) switch(result) { case SomeFailureType: //whatever break; case AnotherFailureType: //other things break; // so on... }

Old fashioned ‘exception’ handling, gotta love it. Usually had plenty of macro’s on hand to simplify near all of that code though.

And no, I don’t do that anymore (mostly), so don’t worry


EDIT: And yes, for most things I passed back I did it using references

[code]errorEnum result;
int theID=-1;
if(FAILED(result=getID(theID)))
switch(result)
{
case SomeFailureType:
//whatever
break;
case AnotherFailureType:
//other things
break;
// so on

}

// Where getID was like:
errorEnum getID(int &return0);[/code]

OK, That makes more sense. A little. :smiley:

Otherwise, it’d be a kick ass obfuscation method.

That was a standard interface in many libraries. DirectX used to do it perfectly (still might through that nasty COM interface now).