Click here to Skip to main content
11,633,969 members (75,817 online)

MarqW asked:

Open original thread
I've got a weird problem with some code. If I use this code, it crashes on there custParts.Where(Function(cv), with the error:
Delegate to an instance method cannot have null 'this'

Dim custParts As CustomerPartNumbers = New CustomerPartNumbers() ' Inherits ComponentModel.BindingList(Of CustomerPartNumber), which is just another data class
Dim queryString As String = "Wallpaper"
custParts.Load("DummyCustomer")
If Not custParts Is Nothing Then
  Dim qS As String = queryString
  Dim cp As CustomerPartNumber
  For Each cp In custParts.Where(Function(cv) cv.CustomerPartNumber = qS)
    Dim cpPN As String = cp.PartNumber
    If prodResults.Where(Function(z) z.PartNumber = cpPN).Count = 0 Then
      prodResults.Add(New Hathaway.Library.Part(cpPN))
    End If
  Next
End If

However, if I put the lambda into a variable, it seems to work without a problem.

Dim custParts As CustomerPartNumbers = New CustomerPartNumbers() ' Inherits ComponentModel.BindingList(Of CustomerPartNumber), which is just another data class
Dim queryString As String = "Wallpaper"
custParts.Load("DummyCustomer")
If Not custParts Is Nothing Then
  Dim qS As String = queryString
  Dim xxx As Object = custParts.Where(Function(cv) cv.CustomerPartNumber = qS)
  Dim cp As CustomerPartNumber
  For Each cp In xxx
    Dim cpPN As String = cp.PartNumber
  Next
End If

Does anybody have any ideas why it's doing this? and why I can't just for Ror Each against my Lambda??

Thanks

Edit:
Bizarrely, this works:

Dim zxcustParts As CustomerPartNumbers = New CustomerPartNumbers()
zxcustParts.Load("DummyCustomer")
Dim zxqueryString As String = "Wallpaper"
For Each zxcustPart In zxcustParts.Where(Function(zxcp) zxcp.CustomerPartNumber = zxqueryString)
  Dim cpPN As String = zxcustPart.PartNumber
Next

Edit 2:
Okay, I've actually figured out what this is... My code paste of the failing code actually missed the bit which was causing the error (though I'm sure I just C/P'd straight from the source). So I've updated that.

The problem is trying to do a second ".Where" inside a For Each of the first.
Tags: VB9.0, VB.NET, .NET

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).



Advertise | Privacy | Mobile
Web03 | 2.8.150728.1 | Last Updated 26 Mar 2009
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100