1

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

3
  • 2
    Why array and not List(Of Persons)? Is this homework? Btw, you should to name the struct Person (singular) , not Persons (plural). Commented Mar 11, 2015 at 22:06
  • 2
    ...and why not a Person Class rather than a struct? Commented Mar 11, 2015 at 22:09
  • Hi Bjorn. Homework? You must be kidding. The answer why not List(of Persons) is because I think that is more clean be this like a structure, not how a list. Maybe I'm wrong, I don't know... Commented Mar 12, 2015 at 9:27

1 Answer 1

4

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".

Sign up to request clarification or add additional context in comments.

3 Comments

just keep in mind this will return the index of the first "Michael". This is probably exactly what you want but consider what if there are multiple "Michael"s.
I will try this. Well, the information of the question is a example. In the project will don't exist repeated names :)
Hey @Diego! The solution works perfectly, it returns me the position of the item that I want. Thank you very much :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.