views:

158

answers:

2

Hi All,
This is a question for my curiosity really, as i know there are other ways to work around the problem.

I have a property on my "Item" class - "MyProperty" - that I want to evaluate. I want to iterate through the collection - "MyItemCollection" - , and if there is an "Item" class whos property "MyProperty" is not nothing i want to set a Boolean flag to indicate that the collection contains a non null MyProperty on any of its "Item" Objects.

Private ContainsPOF = Function() (From thisItem As Item In MyItemCollection Where Item.MyProperty IsNot Nothing Select item).Count > 0

This gives me a warning of "Variable decleration without an 'As' clause; type of Object assumed", so i tried

Private ContainsPOF As Boolean = Function() (From thisItem As Item In MyItemCollection Where Item.MyProperty IsNot Nothing Select item).Count > 0 subc).Count > 0

This however gives me the error of "Lamda expression cannot be converted to 'Boolean' As 'Boolean' is not a delegate type"

Is there anyway to make the return of this Function type safe, or should I just use a different method (an old style Function)?

Thanks.

A: 

this might be clearer as a linq expression matching any items in the collection that are not null eg:

ContainsPDF = ThisCollection.Any(x=>x.MyClass IsNot Nothing);
gum411
+3  A: 

I think you want to declare it as Func(Of Boolean) instead:

Private ContainsPOF As Func(Of Boolean) = [...]

As far as I can see that's not creating a property (as per your first paragraph) though. Why not declare it as a normal property?

Jon Skeet
Apologies, I wasn't really clear in my question, I have re-worded the question to hopefully make it slightly clearer. Your answer does however work perfectly. Why can't you declare the code as i did in my second example however? Thanks
Ben
@Ben: Because your second example is trying to declare a Boolean field which will just have a true or false value. The value you want to store is a *function* which can return a true or false value when called.
Jon Skeet