I am trying to retrieve an object that is in an array when the user selects something in the listbox.
At the moment the listbox has a list of filenames and the way I have achieved the solution is by looping through the array and finding a matching filename and using the object reference once it has been found.
foreach (CPSImage img in objWholeLibrary)
{
if (Path.GetFileName(img.FileName).Equals(lbxImageObjects.SelectedItem.ToString()))
{
currImage = img;
break;
}
}
UpdateDisplay();
Is there a more efficient way of doing this? Instead of relying on a string to match to the object?
If you’re using .NET Framework version at least 3.5 then you can do it with LINQ:
Execution speed should be the same, just the code will look a bit cleaner.