Newbie help with arrays/loops (solved)

I’m having trouble figuring out how to loop through an array for a very basic flashcard program (Sorry… I’m not just new to Xojo, I’m new to programming altogether). Anyway, the user interface would consist of only three buttons: one for going forward through the list of words, one for going backwards, and one for showing the answer.

Right now, I’m just trying to work through how I go forward through the list. For this, I put the array in a pushbutton meant to move forward through the list…

dim cards(5) as String

cards(0) = “final”
cards(1) = “conclusion”
cards(2) = “similar”
cards(3) = “definition”
cards(4) = “area”

For Each name As String In cards
label1.text = (name)
Next

But it only shows me the last element, “area.”

I also tried this…

Dim i as integer
For i As Integer = 0 To 4 Step 1
Label1.text = cards(i)
next

… thinking that it would step through the cards each time I pushed the button. But this doesn’t work either. I’m not even sure if this is the kind of loop I should use. ugh

Any hints or suggestions would be greatly appreciated.

It seems that if you press the button your program starts the loop and is running through it. So you have always the last item as result. No wonder because you are at the end of the loop.

I would try to add one to a counter and check whether the counter is less than 5 (then counter equals 0) or something like this.

You need your program to remember its “state” between pushes of the buttons. To do that, you need to user Properties of the window rather than local variables. (DIM creates a local variable. It is destroyed as soon as the button push code finishes.)

Add 2 properties to the window.

cards(4) as String
cardIndex as Integer

In the window Open event, initialize the values

cards(0) = "final"
cards(1) = "conclusion"
cards(2) = "similar"
cards(3) = "definition"
cards(4) = "area"

cardIndex = -1

In the button for going forward, put

cardIndex = cardIndex + 1
Label1.Text = cards(cardIndex)

Notes:

  1. When you specify the size of an array, you specify the upper bound, which gives you N+1 values (0 through N).
  2. You’ll need to provide some error checking so cardIndex doesn’t go higher than 4 or lower than 0.

You’re very close – you’ve got the right idea, but there are flaws in your implementation.

What you need to do is create a way to remember which card is currently displayed. You’d do that with an integer property that represents the index of the string array. Each time a “next” button is pushed, you’d add 1 to the integer. If the “back” button is pushed, you’d subtract 1. You’d need to add code to make sure the number doesn’t go below zero or above the last item in the array (the array’s ubound value).

To make this work, you’d need to add the string array of cards and the current index as properties of the window so that all parts of your program can access them… In the window’s Open event you can initialize the array’s contents. Then add your index adjustment code to the pushbuttons. After the index is adjusted, you’d need to call an update routine that displays the current card.

Foes that get you started?

Thank you Tim for your help. Unfortunately, I’m still having the same problem (only now it’s showing me the first card in the stack, rather than the last.) Either way, it still doesn’t go through the list. Thanks for your help, though.

Marc, thanks also for your suggestion. I will try this too…

[quote=185045:@Daniel Ball]Thank you Tim for your help. Unfortunately, I’m still having the same problem (only now it’s showing me the first card in the stack, rather than the last.) Either way, it still doesn’t go through the list. Thanks for your help, though.

Marc, thanks also for your suggestion. I will try this too…[/quote]
Are you locally dim’ing cardIndex as it appears not to be incrementing? You must make cardIndex a property on the window for example to prevent it from going out of scope in order to allow it to actually increment (cardIndex = cardIndex + 1)

HTH,
Mike

Ah… I hadn’t done that.

Now I’m getting error messages that say, “Item does not exist” for both the cards and cardsIndex.

When adding a property to the window, I didn’t know if I should have a property for both “cards” and “cardsIndex.” I tried it with them typed together in the same property (which didn’t work), so I tried it with them typed in separate properties, which also didn’t work.

Then I noticed the above code in Tim’s post doesn’t say “dim,” so I tried it both with and without the dim statement, but it didn’t change anything.

Next, noticed there was a little text box in the property settings in the top right-hand corner with the caption, “TYPE.” So, I made sure that the “Type” was set for both integer and string for each property respectively.

No dice.

[quote=185105:@Daniel Ball]cards(4) as String
cardIndex as Integer
In the window Open event, initialize the values

cards(0) = “final”
cards(1) = “conclusion”
cards(2) = “similar”
cards(3) = “definition”
cards(4) = “area”

cardIndex = -1
In the button for going forward, put

cardIndex = cardIndex + 1
Label1.Text = cards(cardIndex)
[/quote]

Daniel please post your code so we can help?

Thanks!

Daniel I’m having trouble visualising what you want but maybe this may help

[quote=185105:@Daniel Ball]So, I made sure that the “Type” was set for both integer and string for each property respectively.

No dice.[/quote]
Sounds like you’re adding a constant, not a property.

What I do is: Right-click anywhere in the left-hand Contents menu, then click on Module. Then right-click on the Module, move to ‘Add to modulename’ and click Property. Then in the right-hand panel fill in your variable’s name and type, and set the Scope to Global. Repeat this for each variable you need. This allow your entire app to access the variable(s) entered here.

The code Tim gave you should work well from there and should eliminate the “item does not exist” errors.

Well, I’m kind of embarrassed to say that the problem was mostly me and the fact that I wasn’t setting my window properties correctly. It appears to be working now, and I sincerely appreciate the help you guys have given me on this.

Just so I understand what’s happening here: why doesn’t it work when I locally dim the variable but it works when I globally declare them? Even when I do it locally, it seems to cycle through the array… Just not one by one. What’s actually going on here? Thanks again for your help.

Actually, you can scratch that. I think I got it now. Before I was using a loop… Which makes sense now.

Yes, and you might want to read a bit in the LR about scope. With your cards() array being creating inside the button event, it simply does not exist outside of this event. That’s why label.text always showed the last entry: The array way created, the loop run through and the last item of the array copied to label.text. Then with leaving the event the array was removed from memory.