Thursday 15 September 2011

vb.net - Using IEnumberable(Of String) to read from different kinds of data sources -



vb.net - Using IEnumberable(Of String) to read from different kinds of data sources -

i using below variable store list of user id strings. utilize list search each user using ldap query.

dim userids ienumerable(of string) = {"testid1", "testid2", "testid3", "testid4", "testid5"}

that works, id's hard-coded. how create read id's listbox command instead? like:

dim userids ienumerable(of string) = listbox1???

i utilize listbox because plan load listbox bunch of id's text file.

better yet, possible utilize textbox? if textbox, re-create , paste id's need query.

i going write comment, got bit long , started involving code examples figured improve post answer, though there accepted reply good.

since i'm 1 gave code used ienumerable sense should explain why used it... ienumerable lowest level interface implemented lists, collections, dictionaries, arrays, etc. basically, stores multiple info can looped through for each loop, implements ienumerable interface. in fact, thing ienumerable interface supports is ability enumerate through contents for each loop. ienumerable interface, it's not concrete object type. therefore, when create ienumerable variable, means variable can used reference (i.e. point to) object implements interface (i.e. object can enumerated for each loop.

therefore, in next line, it's not creating ienumerable type of object. or @ to the lowest degree not in concrete type sense. it's creating specific type of object happens implement ienumerable interface , sets ids variable point it.

dim userids ienumerable(of string) = {"1", "2", "3"}

the phrase {"1", "2", "3"} literal look represent array of strings. in other words, literal look equivalent of doing following:

dim stringarray(2) string stringarray(0) = "1" stringarray(1) = "2" stringarray(2) = "3"

so, since object containing list of id's array of strings, have been done this:

dim userids() string = {"1", "2", "3"}

however, since wanted code work, regardless of info source, used more general ienumerable interface. since thing required of info enumerated for each loop, didn't want limit flexibility of code requiring input list of id's of higher-level specific object type. didn't care id's array, or list, or dictionary, or collection. long loop through, that's cared about. doing so, made code more flexible set variable enumerable info source, such items property of listbox. instance, of next have worked, without changing rest of code:

dim userids ienumerable(of string) = {"1", "2", "3"}

or

dim useridsarray() string = {"1", "2", "3"} dim userids ienumerable(of string) = useridsarray

or

dim useridsarray(2) string useridsarray(0) = "1" useridsarray(1) = "2" useridsarray(2) = "3" dim userids ienumerable(of string) = useridsarray

or

dim userids ienumerable(of string) = listbox1.items.oftype(of string)()

or

dim userids ienumerable(of string) = file.readalllines("ids.txt")

or

dim userids ienumerable(of string) = textbox1.text.split({environment.newline}, stringsplitoptions.removeemptyentries)

etc.

since of above info sources implement ienumerable interface, same userids variable can used reference of them.

vb.net

No comments:

Post a Comment