1

Hello I'm using the Microsoft.VisualBasic.PowerPacks Name space to create shapes on a windows form. I've used an array to store all the objects so that i can generate new shapes and modify the properties of a collection of objects, dependent on given scenarios.

I'm attempting to perform a look-up on the array an find the lowest index that contains an Oval Shape. After trawling the internet for quite some time, I've only found statements that will accept fixed values, since every location within the array contains the same value I can't do that.

I'm looking for something along the lines of the statement below. Where i can find either the first entry that is not null or contains an "Microsoft.VisualBasic.Powerpacks.Ovalshape" the object not the type. Thanks.

// ** Object declaration
Microsoft.VisualBasic.PowerPacks.OvalShape shape = new Microsoft.VisualBasic.PowerPacks.OvalShape();    
Microsoft.VisualBasic.PowerPacks.OvalShape[] shapes;
**//


     int myIndex = Array.IndexOf(shapes, != null);
ConsultingEasy
  • 385
  • 2
  • 5
  • 11

1 Answers1

4

Simply use:

Array.FindIndex(shapes, s => s != null)
Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181
  • @NathanHotchkin There's an anonymous function taking a parameter `s` of type `Microsoft.VisualBasic.PowerPacks.OvalShape` and returning a `bool`, namely whether `s` is distinct from `null`. This is like a named method `static bool MethodWithName(OvalShape s) { return s != null; }`. The signature and return type of the anonymous function (or the named method) matches the `Predicate` delegate type that the `FindIndex` method requires. Expressions containing the arrow `=>` are called _lambda expressions_. – Jeppe Stig Nielsen Mar 04 '13 at 21:51
  • Thanks i wasn't able to make sense of how to create a Array.FindIndex expression that accepted a bool argument. – ConsultingEasy Mar 04 '13 at 21:54