0

I'm trying to display the following passwords in a message box. What I want to happen is to give to the user the first password, which is 111111, by means of message box. After 111111 has been given, the next time the user asks for another password, it will be 222222, and so on and so forth. If all the passwords have been used up, the loop will simply return to 111111, 222222, 333333, etc... How do I do it? Thanks for any help.

Here's my code:

                    int[] password;
                    password = new int[10];
                    password[0] = 000000;
                    password[1] = 111111;
                    password[2] = 222222;
                    password[3] = 333333;
                    password[4] = 444444;
                    password[5] = 555555;
                    password[6] = 666666;
                    password[7] = 777777;
                    password[8] = 888888;
                    password[9] = 999999;

                    MessageBox.Show("Your password is ...");
                    return;
1
  • Just keep a count variable initialized to 1, so the first time the user requests you can display password[count], and so on, all you'll then need to check is if count is == array length. Commented Jul 21, 2012 at 7:08

3 Answers 3

7

It sounds like you're not really looping here - you just want to maintain an index. After all, the "provide the user a password" sounds like it's event-driven rather than just repeated by our code.

Two options:

  • Keep a list of passwords, and retain the "current index" into it. Increment the index each time, and loop back to 0 when you reach the end.

  • Use a Queue<T> instead, and whenever you display a password, add a new one to the end.

As asides:

  • int is a really bad type to pick for passwords, unless you're really constraining users to numeric keypads. Even then, you should really be distinguishing between "0001" and "1", which int values can't.

  • Learn to use array initializers. The bulk of your sample code is equivalent to:

    int[] password = { 0, 111111, 222222, 333333, 444444, 555555, 666666,
                       777777, 888888, 999999 };
    
  • A hard-coded list of passwords which are reused... really?

Sign up to request clarification or add additional context in comments.

1 Comment

+1 "A hard-coded list of passwords which are reused... really?"
2

You could use an iterator method which basically will return the next password each time it is called and loop on the password list (to start back from first one when all passwords have been listed)

IEnumerable<int> GetPassword()
{
    while (true)
    {
       yield return 000000;
       yield return 111111;
       yield return 222222;
       yield return 333333;
       yield return 444444;
       yield return 555555;
       yield return 666666;
       yield return 777777;
       yield return 888888;
       yield return 999999;
    }
}

Each call to this method will return the next password

So if you call

MessageBox.Show(String.Format("Your password is {0}", GetPassword());

multiple times, you will get the next password each time in your message box and once if reaches 999999 it will go back to 000000.

EDIT : Usage of the iterator is wrong (see John Skeet comment below) One of the way arround if keeping the exact same iterator block would be to get the enumerator (once per user or same for all users, depending of the usage) by calling GetPassword().GetEnumerator() and then providing the enumerator to another method GetNextPassword actually returning the password.

int GetNextPassword(IEnumerator<int> enumerator)
{ 
    enumerator.MoveNext();
    return enumerator.Current;
}

But not getting very pretty at this point.

1 Comment

No it won't. Each call to MoveNext()/Current on the value returned from a single call to GetPassword() will return the next password. That's a big difference - you'd need to call this method once and stash the return value.
0

For your specific scenario, I would recommend using a Queue:

using System.Collections;

Queue passwords = new Queue();

foreach (int p in new int[]{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 })
{
   passwords.Enqueue(p);
}

//just to show you that it's FIFO (first-in, first-out)
while (passwords.Count > 0)
{
   MessageBox.Show("Your password is " + passwords.Dequeue();
}   

7 Comments

@CantHandleMeBabe Yeah, don't actually use a while loop. I just wanted to show you how Dequeue behaves.
@CantHandleMeBabe Depends. How does a user ask for another password? BTW, did you read the asides in Jon Skeet's answer? I can't tell if you are really doing this or if you just made up an example.
I'm doing this, and the user will press a button to get the password, let's say 111111. If he pressed that button again, the message box will display 222222, and so on and so forth.
@CantHandleMeBabe in the method for the button click put MessageBox.Show("Your password is " + passwords.Dequeue();.
The password is always 111111... I placed the MessageBox.Show("Your password is " + passwords.Dequeue(); outside the foreach loop.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.