It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming Visual Basic Forum

2 dimensional array list problem

Moderator: dafunkymunky

2 dimensional array list problem

Postby ciiure on Fri Nov 18, 2011 7:55 pm

Hello there, i have been working on a simple program but cant get my head around it. It does not show any errors at the moment but when i run it does not do the things i would like it to do. I will paste in the code first and explain it below.


Public Class Form1
Const limit = 3
'Dim Countries(17, 17) As Byte
'Dim Capitals(17, 17) As Byte
Dim Countries(,) = {{"England", 0}, {"Russia", 1}, {"France", 2}, {"Portugal", 3}, {"Poland", 4}, {"New Zealand", 5}, {"Germany", 6}, {"Spain", 7}, {"Argentina", 8}, {"China", 9}, {"Japan", 10}, {"Monaco", 11}, {"Italy", 12}, {"Austria", 13}, {"Switzerland", 14}, {"Norway", 15}, {"Turkey", 16}}
Dim Capitals(,) = {{"London", 0}, {"Moscow", 1}, {"Paris", 2}, {"Lizbona", 3}, {"Warsaw", 4}, {"Wellington", 5}, {"Berlin", 6}, {"Madrid", 7}, {"Buenos Aires", 8}, {"Beijing", 9}, {"Tokyo", 10}, {"Monaco", 11}, {"Rome", 12}, {"Vienna", 13}, {"Bern", 14}, {"Oslo", 15}, {"Ankara", 16}}
'Dim All(16, 1) As String
Dim x As Integer
Dim y As Integer

Private Sub ShuffleItems()

Dim Random As New System.Random

lstCountries.BeginUpdate()
lstCapitals.BeginUpdate()

Dim ArrayList As New System.Collections.ArrayList(Countries)
Dim ArrayList1 As New System.Collections.ArrayList(Capitals)

lstCountries.Items.Clear()
lstCapitals.Items.Clear()

While ArrayList.Count > 0

Dim Index As System.Int32 = Random.Next(0, ArrayList.Count)

lstCountries.Items.Add(ArrayList(Index))

ArrayList.RemoveAt(Index)

End While

While ArrayList1.Count > 0

Dim Index As System.Int32 = Random.Next(0, ArrayList1.Count)

lstCapitals.Items.Add(ArrayList1(Index))

ArrayList1.RemoveAt(Index)

End While

lstCountries.EndUpdate()
lstCapitals.EndUpdate()

End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim count As Integer = -1
'Dim shuffle As New Random
'All(0, 0) = "England"
'All(0, 1) = "London"
'All(1, 0) = "Russia"
'All(1, 1) = "Moscow"
'All(2, 0) = "France"
'All(2, 1) = "Paris"
'All(3, 0) = "Germany"
'All(3, 1) = "Berlin"
'All(4, 0) = "Spain"
'All(4, 1) = "Madrid"


'Countries = {("England",0),( "Russia",1), ("France",2),( "Portugal",3), ("Poland",4), ("New Zealand",5), ("Germany",6), ("Spain",7), ("Argentina",8), ("China",9), ("Japan",10), ("Monaco",11), ("Italy",12), ("Austria",13), ("Switzerland",14), ("Norway",15), ("Turkey",16)}
'Capitals = {("London",0),( "Moscow",1),( "Paris",2),( "Lizbona",3),( "Warsaw",4),( "Wellington",5),( "Berlin",6), ("Madrid",7),( "Buenos Aires",8),( "Beijing",9),( "Tokyo",10),( "Monaco",11),( "Rome",12), ("Vienna",13) ,("Bern",14), ("Oslo",15), ("Ankara",16)}
'Countries(0) = "England"
'Countries(1) = "Russia"
'Countries(2) = "France"
'Countries(3) = "Portugal"

'Capitals(0) = "London"
'Capitals(1) = "Moscow"
'Capitals(2) = "Paris"
'Capitals(3) = "Lizbona"

ShuffleItems()

'lstCountries.SelectedIndex = shuffle.Next(0, lstCountries.Items.Count - 1)
'Do While count < Countries.Length - 1
' count = count + 1
' lstCountries.Items.Add(Countries(count))
' lstCapitals.Items.Add(Capitals(count))
'Loop

End Sub

Private Sub btnCheck_Click(sender As System.Object, e As System.EventArgs) Handles btnCheck.Click
Dim x As Integer
Dim y As Integer
For x = 0 To Countries.Length - 1
x += 1
If Countries(y, x) = lstCountries.SelectedItem.ToString And Countries(y, x) = lstCapitals.SelectedItem.ToString Then
lstCapitals.Items.RemoveAt(lstCapitals.SelectedIndex)
lstCountries.Items.RemoveAt(lstCountries.SelectedIndex)
End If

Next
'If lstCapitals.SelectedIndex = lstCountries.SelectedIndex Then
' lstCapitals.Items.RemoveAt(lstCapitals.SelectedIndex)
' lstCountries.Items.RemoveAt(lstCountries.SelectedIndex)
' MsgBox("Well done")
'ElseIf lstCountries.SelectedIndex <> lstCapitals.SelectedIndex Then
' MsgBox("Incorrect match")
' lstCapitals.ClearSelected()
' lstCountries.ClearSelected()
'End If
End Sub

Private Sub btnQuit_Click(sender As System.Object, e As System.EventArgs) Handles btnQuit.Click
Dim intResponse As Integer

intResponse = MsgBox("Are you sure you want to quit?", _
vbYesNo + vbQuestion, _
"Quit")

If intResponse = vbYes Then
End
End If
End Sub

Private Sub btnHelp_Click(sender As System.Object, e As System.EventArgs) Handles btnHelp.Click
Me.Hide()
Form2.Show()
End Sub
End Class


So basically the form has help button to show instructions, quit to quit the program and check to check whether the country from listbox lstCountries matches the correct capital in lstCapitals.
The idea of the program is to shuffle the items in two arraylists and then add them to seperate listboxes called lstCountries and lstCapitals.
At start i used 1 dimensional array and it worked like a charm for mixing up items in arrays and then adding them to listboxes, but then the problem had to match the correct Country with correct Capital and it only worked if i picked the items on the same index such as 1 and 1(straight line).
It all sounds complicated and confusing, but any help will help.
P.S alot of code is commented out, because i have tried many ways to make it work.
ciiure
 
Posts: 5
Joined: Fri Nov 18, 2011 7:45 pm

Re: 2 dimensional array list problem

Postby ciiure on Fri Nov 18, 2011 8:05 pm

More details:

Two lists:
lstCountries, to which all the Countries gonna be added
lstCapitals to which all the Capitals gonna be added
2 Dimensional Arraylist with Capitals
2 Dimensional Arraylist with Countries

Function to shuffle the items in the arraylists
Once the items are shuffled they are getting added to the listboxes, which worked fine when i used simple arraylists, now it doesnt add anything to listboxes.
Once the items are in the listboxes user has to pick item from lstCountries and match it with correct item from lstCapitals. Once the user picks 1 item from each listbox he/she clicks check and the values have to be compared to see if they match. If they match then messagebox appears saying thats correct and they both dissappear from the listboxes. If they dont much and button check is clicked mesage appears sayin its incorrect.
ciiure
 
Posts: 5
Joined: Fri Nov 18, 2011 7:45 pm

Re: 2 dimensional array list problem

Postby sanjon on Sun Nov 20, 2011 8:18 pm

Hello,

I don't think you need to use a 2 dimensional array for this. And even if you did, it wouldn't be (16, 16). It would be (16, 1). This is because you have 16 countries, but only one index for every countries. If I made this program I would probably find some way to add items from the array to the list boxes randomly. This way the countries and capitals don't need to be stored twice.

I hope this post helped at least a bit. If you still have any questions let me know.
sanjon
 
Posts: 40
Joined: Sun Dec 05, 2010 6:20 pm


Who is online

Users browsing this forum: No registered users and 5 guests