在对象数组中查找项目 VB.NET
我正在创建一个函数,用于搜索 Customers 数组并按给定 ID 返回 Customer 对象。 我正在尝试使用 Lambda 表达式来做到这一点,这就是我到目前为止所拥有的:
Public Shared Function FindCustomer(ByVal ID As Integer) As cCustomer
Dim customer as New cCustomer = _ _
Array.Find(arrCustomers, Function(c as cCustomer) c.ID = ID)
Return customer
End Function
但是,这会在“c”处引发空引用异常,我不知道如何解决这个问题,欢迎任何帮助:)。
编辑:当我调用如下函数时出现错误:
dim cust as New cCustomer
cust = FindCustomer(5)
MsgBox(cust.Name)
'BANG
Thx。
I'm making a function that searchs an array of Customers and returns a Customer object by a given ID.
I'm trying to do so with Lambda Expressions,and this is what I have so far:
Public Shared Function FindCustomer(ByVal ID As Integer) As cCustomer
Dim customer as New cCustomer = _ _
Array.Find(arrCustomers, Function(c as cCustomer) c.ID = ID)
Return customer
End Function
However,this throws a Null Reference exception at 'c',and I have no idea how to solve this,any help is welcome :).
EDIT: I get the error when I call the function like:
dim cust as New cCustomer
cust = FindCustomer(5)
MsgBox(cust.Name)
'BANG
Thx.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的数组中的客户之一为空。
One of the customers in your array is null.