Click here to Skip to main content
14,391,572 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hello coders, i am trying to transfer the value of a randomly generated variable into another on click method but have been unsuccessful with the error "this name does not exist in the current context" popping up, please help if you can, thanks.

What I have tried:

public void RandomImage()
        {
            string[] flags = { "Afghanistan", "Albania", "Algeria", "Andorra"};

            Random num = new Random();
            String correctFlag = flags[num.Next(0, flags.Length)];
        }

        private void Button1_clicked( object sender, RoutedEventArgs e)
        {
            if (Button1.Content == correctFlag) //correctFlag does not exist
            {
                 //do something
            }
        }
Posted
Updated 13-Jun-19 2:45am
Rate this:
Please Sign up or sign in to vote.

Solution 1

public void RandomImage()
        {
            string[] flags = { "Afghanistan", "Albania", "Algeria", "Andorra"};

            Random num = new Random();

            // correctFlag is defined in this function so is only available in this function
            String correctFlag = flags[num.Next(0, flags.Length)];
        }

        private void Button1_clicked( object sender, RoutedEventArgs e)
        {
            if (Button1.Content == correctFlag) //correctFlag does not exist
            {
                 //do something
            }
        }


private string CorrectFlag {get; set;} // define it at class-level as a property

public void RandomImage()
        {
            string[] flags = { "Afghanistan", "Albania", "Algeria", "Andorra"};

            Random num = new Random();
            CorrectFlag = flags[num.Next(0, flags.Length)]; // now you can use it anywhere in that class
        }

        private void Button1_clicked( object sender, RoutedEventArgs e)
        {
            if (Button1.Content == CorrectFlag) // now you can use it anywhere in that class
            {
                 //do something
            }
        }
   
Comments
QuantumNova 13-Jun-19 7:29am
   
Thanks, this really helped :)
[no name] 13-Jun-19 10:44am
   
Wow, exactly what I need, thanks
Rate this:
Please Sign up or sign in to vote.

Solution 2

Just to add to what F-ES Sitecore said: don;t create a new random instance each time you want to use it: create one (possibly static instance and use that instead.

Because Random instances are initialized with the current system Tick count when they are created, generating a new one inside a method can give you problems, like the same value sequence being generated each time - and the problem gets worse as processors get faster.

private static Random num = new Random();
public void RandomImage()
        {
            string[] flags = { "Afghanistan", "Albania", "Algeria", "Andorra"};
            CorrectFlag = flags[num.Next(0, flags.Length)]; 
        }
   
Comments
QuantumNova 13-Jun-19 8:36am
   
thank you :)
OriginalGriff 13-Jun-19 8:41am
   
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100