cancel
Showing results for 
Search instead for 
Did you mean: 

How to get index of an item in the DropDownByKey control?

former_member182374
Active Contributor
0 Kudos

Hi,

I'm new to PDK for .NET and I have a simple question about the "IndexOf" method of the items collection.

Attached a small test program:

Dim li As New DropDownListItem

li.Key = "A"

li.Text = "Zero"

DropDownByKey1.Items.Add(li)

li = New DropDownListItem

li.Key = "B"

li.Text = "One"

DropDownByKey1.Items.Add(li)

li = New DropDownListItem

li.Key = "C"

li.Text = "Two"

DropDownByKey1.Items.Add(li)

li = New DropDownListItem

li.Key = "D"

li.Text = "Three"

DropDownByKey1.Items.Add(li)

li = New DropDownListItem

li.Key = "E"

li.Text = "Four"

DropDownByKey1.Items.Add(li)

Dim liForSearch As New DropDownListItem

liForSearch.Key = "C"

liForSearch.Text= "Two"

Write(" Index --> " & DropDownByKey1.Items.IndexOf(liForSearch))

The output should be 2 but instead I get -1 (Not Found).

Why?

thanks,

Omri

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

HI Omri,

you have created a new object, liForSearch, with the same Text and Key properties, but the object is differant than the one in the DropDownByKey.Items collection so the returned index is -1;

former_member182374
Active Contributor
0 Kudos

Thanks Tsachi,

I understands now that it compares references (like in Java).

Former Member
0 Kudos

Hi Omri,

Please mark this thread as answered.

Regards,

Tsachi

Answers (1)

Answers (1)

Former Member
0 Kudos

Hi,

Since you have the Key of the listItem you are looking for you should get the item by applying:

liToSearch = DropDownListItem.Items[key];

int index = DropDownListItem.Items.IndexOf(liToSearch);

Regards,

Tsachi