Find item in array of structure

て烟熏妆下的殇ゞ 提交于 2021-01-28 05:45:47

问题


I have a question. Imagine that in vb.net, fill a array of structure with a lot of items. For example, here I declare the structure called Persons:

    Public structure Persons
         Dim name as string
         Dim age as integer
    End structure

Then, I declare a variable that is a array of persons, for make a list of friends, like this:

    Dim friends() as Persons
    friends(0).name = "Sebastian"
    friends(0).age = 19

    friends(1).name = "Michael"
    friends(1).age = 34

    ...

So, there are any form to locate where is the position of "Sebastian"?? In other words. If I would know if "Sebastian" exist in any friends(i).name, and, if exist, returns me the position (i), how I can do this??

Thanks


回答1:


Try this:

Dim i As Integer = Array.FindIndex(friends, Function(f) f.name = "Michael")

The variable i should have the position of the person named "Michael".



来源:https://stackoverflow.com/questions/28998204/find-item-in-array-of-structure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!