-
Jul 19th, 2019, 03:19 PM
#1
Thread Starter
Hyperactive Member
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
-
Jul 19th, 2019, 06:03 PM
#2
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...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 19th, 2019, 10:42 PM
#3
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:
Dim matches = StructureList.Where(Function(i) userlist.Contains(i.FirstItem) OrElse userlist.Contains(i.SecondItem) OrElse userlist.Contains(i.ThirdItem)). ToArray()
That will give you an Items array containing all the items from StructureList where any of its field values are contained in 'userlist'.
-
Jul 19th, 2019, 10:49 PM
#4
Re: List Of Strings Question
Here's a slightly different implementation that would reduce the number of times 'userlist' gets enumerated.
vb.net Code:
Dim matches = StructureList.Where(Function(i) userlist.Any(Function(s) s = i.FirstItem OrElse
s = i.SecondItem OrElse
s = i.ThirdItem)).
ToArray()
-
Jul 19th, 2019, 11:01 PM
#5
Re: List Of Strings Question
@JM That's a lot of givens and assumptions for you
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 19th, 2019, 11:25 PM
#6
Re: List Of Strings Question
Originally Posted by .paul.
@JM That's a lot of givens and assumptions for you
I feel in a giving and assumpting mood.
-
Jul 20th, 2019, 03:43 AM
#7
Thread Starter
Hyperactive Member
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.
-
Jul 20th, 2019, 04:39 AM
#8
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 20th, 2019, 05:16 AM
#9
Thread Starter
Hyperactive Member
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 ?
-
Jul 20th, 2019, 05:26 AM
#10
Re: List Of Strings Question
So you want to select the structurelist members by variable member name?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 20th, 2019, 05:35 AM
#11
Thread Starter
Hyperactive Member
Re: List Of Strings Question
Haha, why didnt i just say that very thing
-
Jul 20th, 2019, 05:46 AM
#12
Re: List Of Strings Question
Ok that's a more complicated question. Why will the list of items get smaller?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 20th, 2019, 05:54 AM
#13
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...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 20th, 2019, 06:12 AM
#14
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"))
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 20th, 2019, 08:21 AM
#15
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|