PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
List Of Strings Question-VBForums
Results 1 to 15 of 15

Thread: List Of Strings Question

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2003
    Location
    Wigan, UK
    Posts
    289

    List Of Strings Question

    Lets say i have a structure

    Private Structure Items
    Dim FistItem as String
    Dim SecondItem as String
    Dim ThirdItem as String
    End Structure

    Then i have a list filled with data

    Private StructureList as New List (Of Items)

    I also have another list which the user has selected items out of the structure, lets call it userlist.

    I want to loop the StrutureList and only grab the items out of the userlist

    For i as Integer = 0 To StructureList.Count - 1

    For x as Integer = 0 To userlist.COunt - 1

    msgbox StructureList(i). <<<<< userlist(x).item that belongs to the Items structure

    Next

    Next

    I basically need to get the value out of the structurelist without knowing what the user has selected ?

    I hope i have made that clear enough ?

    Thanks

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,583

    Re: List Of Strings Question

    You want to access an item of StructureList where some member of StructureList is equal to userlist(x).item???
    We'd need to see userlist structure unless it's a plain list of string. Otherwise we need to see which member of StructureList you're hoping to compare with...

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    103,444

    Re: List Of Strings Question

    It's not a given from what you posted but it sounds to me like 'userlist' is indeed a List(Of String), where the items came from one of the fields of the Items values in StructureList. I'm going to assume that that is the case and recommend that LINQ would be the best option here, because it will be extremely succinct. LINQ excels at collapsing loops:
    vb.net Code:
    1. Dim matches = StructureList.Where(Function(i) userlist.Contains(i.FirstItem) OrElse
    2.                                               userlist.Contains(i.SecondItem) OrElse
    3.                                               userlist.Contains(i.ThirdItem)).
    4.                             ToArray()
    That will give you an Items array containing all the items from StructureList where any of its field values are contained in 'userlist'.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    103,444

    Re: List Of Strings Question

    Here's a slightly different implementation that would reduce the number of times 'userlist' gets enumerated.
    vb.net Code:
    1. Dim matches = StructureList.Where(Function(i) userlist.Any(Function(s) s = i.FirstItem OrElse
    2.                                                                        s = i.SecondItem OrElse
    3.                                                                        s = i.ThirdItem)).
    4.                             ToArray()

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,583

    Re: List Of Strings Question

    @JM That's a lot of givens and assumptions for you

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    103,444

    Re: List Of Strings Question

    Quote Originally Posted by .paul. View Post
    @JM That's a lot of givens and assumptions for you
    I feel in a giving and assumpting mood.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2003
    Location
    Wigan, UK
    Posts
    289

    Re: List Of Strings Question

    Either i have not explained myself correctly or i just cant get my head around your answer, probably because i get an error with it.

    in this example, structurelist has 3 fields and userlist has any number of the field names which belong to structurelist.

    if the user selects only one field then, in a way, the structurelist becomes a smaller structure

    For i as Integer = 0 To StructureList.Count - 1

    For x as Integer = 0 To userlist.COunt - 1

    print structurelist(i).fieldname value where userlist(x).value = structurelist field name

    Next

    Next

    basically, if the structurelist(i).fieldname = "firstitem" then print the value

    I hope that explains it

    Apologies if you got that anyway and its me not understanding the answers.

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,583

    Re: List Of Strings Question

    Your title says List(Of String) question. Is userlist a List(Of String) or a list of structure?
    Your reply doesn't make much sense

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2003
    Location
    Wigan, UK
    Posts
    289

    Re: List Of Strings Question

    I am probably over complicating things.

    Private Structure Items
    Dim FistItem as String
    Dim SecondItem as String
    Dim ThirdItem as String
    Dim FourthItem as String
    Dim FifthItem as String
    End Structure

    Then i have a list filled with data

    Private StructureList as New List (Of Items)

    Also i have a list of string, lets say

    Private userlist As New List(Of String)

    and lets say that was filled with 2 items and the values of those items are the names of fieldnames from the structurelist
    userlist(0) = "SecondItem"
    userlist(1) = "FourthItem"



    For i as Integer = 0 To StructureList.Count - 1

    For x as Integer = 0 To userlist.count - 1

    do something with structurelist(i).value where the structurelist(i).fieldname = userlist(x).value

    Next

    Next

    Is that explained any better ?

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,583

    Re: List Of Strings Question

    So you want to select the structurelist members by variable member name?

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2003
    Location
    Wigan, UK
    Posts
    289

    Re: List Of Strings Question

    Haha, why didnt i just say that very thing

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,583

    Re: List Of Strings Question

    Ok that's a more complicated question. Why will the list of items get smaller?

  13. #13
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,583

    Re: List Of Strings Question

    I'm not at my computer at the moment. I'd probably put a function within the structure declaration that would return the member specified in a string argument. That would need to use reflection...

  14. #14
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,583

    Re: List Of Strings Question

    Try this...

    Code:
    Private Structure Items
        Dim FirstItem As String
        Dim SecondItem As String
        Dim ThirdItem As String
        Dim FourthItem As String
        Dim FifthItem As String
        Public Function getPropertyByName(ByVal memberName As String) As String
            Dim field As FieldInfo = Me.GetType().GetField(memberName, BindingFlags.Public Or BindingFlags.Instance)
            Return field.GetValue(Me).ToString
        End Function
    End Structure
    Code:
    Dim listOfItems As New List(Of Items)
    Code:
    listOfItems.Add(New Items With {.FirstItem = "one", .SecondItem = "two", .ThirdItem = "three", .FourthItem = "four", .FifthItem = "five"})
    MsgBox(listOfItems(0).getPropertyByName("FirstItem"))

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2003
    Location
    Wigan, UK
    Posts
    289

    Re: List Of Strings Question

    I am out at the moment so I will try it out when I get back.
    Just wanted to says thanks everybody for your time and experience.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width