Searching Array in .txt File VB.net -
i'm doing school project vb , have been asked write program creates single dimension string array consisting of names of last 6 people win golf tournament. user has enter name in text box search array. if name appear, message box should display players name , winning year if doesn't appear message should appear explaining also.
i've written out code have come across error {'i' not declared. may inaccessible due it's protection level}
i have researched error , have tried multiple things code still wont work. woul appreciate thank
imports system.io public class form1 dim player() string = {"tim wood", "vince singer","donal clark", "patrick hartnett", "peter nicholson", "chris montgomery"} dim winningyear() integer = {2002, 2003, 2004, 2005, 2006, 2007} private sub btnsearch_click(sender object, e eventargs) 'searches array player dim strplayer string = txtplayer.text dim intyearwon integer integer = 0 6 if = 6 messagebox.show(strplayer & " has not won tournament in last 6 years") end if next intyearwon = winningyear(i) if strplayer = player(i) messagebox.show(strplayer & " won in " & intyearwon) end if end sub end class
well here's how you'd fix current btnsearch_click()
method:
private sub btnsearch_click(sender object, e eventargs) 'searches array player dim strplayer string = txtplayer.text dim intyearwon integer dim success boolean = false integer = 0 players.count - 1 if strplayer.equals(player(i)) intyearwon = winningyear(i) success = true messagebox.show(strplayer & " won in " & intyearwon) end if next if not success messagebox.show(strplayer & " has not won tournament in last 6 years") end sub
however, using dictionary(of int32, string)
years , names. notice choose year key , name value. that's because 1 person can win each year, each person can win multiple years. here's how use dictionary(of int32, string)
in code:
public class form1 dim playerdict new dictionary(of int32, string) {{2002, "tim wood"}, {2003, "vince singer"}, {2004, "donal clark"}, {2005, "patrick hartnett"}, {2006, "peter nicholson"}, {2007, "chris montgomery"}} private sub btnsearch_click(sender object, e eventargs) dim strplayer string = txtplayer.text dim years ienumerable(of string) = kvp in playerdict kvp.value = strplayer select kvp.key.tostring if years.count <> 0 messagebox.show(strplayer & " has won in folowing years: " & string.join(", ", years)) else messagebox.show(strplayer & " has not won tournament in last " & playerdict.keys.count & " years") end if end sub end class
Comments
Post a Comment