ComboBox ItemIndex and ID


#1

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


#2

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.


#3

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


#4

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?


#5

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.


#6

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:


#7

Really? Even in this kind of thing:

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

Dude. Trippy.


#8

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]


#9

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

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


#10

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


#11