Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 1:45am

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
            }
        }
 
Share this answer
 
Comments
QuantumNova 13-Jun-19 7:29am    
Thanks, this really helped :)
[no name] 13-Jun-19 10:44am    
Wow, exactly what I need, thanks
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.

C#
private static Random num = new Random();
public void RandomImage()
        {
            string[] flags = { "Afghanistan", "Albania", "Algeria", "Andorra"};
            CorrectFlag = flags[num.Next(0, flags.Length)]; 
        }
 
Share this answer
 
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900