Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello coders, I am having a issue were when I attempt to transfer a randomly generated value in a variable to a on_click function the value appears to be blank and does not transfer over like it should, I really cannot figure out why this issue is occurring so if someone could please help it would be very much appreciated, thank you.

(ps sorry for posting pretty much the entire code but I really could not figure out where I have gone wrong)

What I have tried:

public MainWindow()
        {
            InitializeComponent();
            RandomImage();
        }

        private string correctFlag { get; set; }
        public void RandomImage()
        {
            string[] flags = { "Afghanistan", "Albania", "Algeria", "Andorra", "Angola", "Antigua and Barboda", "Argentina",
                "Armenia", "Australia", "Austria", "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh", "Barbados"};

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

            String FlagUri = $"flags/flags 3.0/{correctFlag}.png";

            flagBox.Source = new BitmapImage(new Uri(FlagUri, UriKind.Relative));
            RandomButttons(correctFlag, flag2, flag3, flag4);
    }

        public void RandomButttons(string correctFlag, string flag2, string flag3, string flag4)
        {
            Random rnd = new Random();
            int RndButton = rnd.Next(1, 5);
            Console.WriteLine(RndButton);

            if (RndButton == 1)//this works as it should
            {
                Button1.Content = correctFlag;
                Button2.Content = flag2;
                Button3.Content = flag3;
                Button4.Content = flag4;
            }
            else if (RndButton == 2)//this works as it should
            {
                Button2.Content = correctFlag;
                Button1.Content = flag2;
                Button3.Content = flag3;
                Button4.Content = flag4;
            }
            else if (RndButton == 3)//this works as it should
            {
                Button3.Content = correctFlag;
                Button2.Content = flag2;
                Button1.Content = flag3;
                Button4.Content = flag4;
            }
            else if (RndButton == 4)//this works as it should
            {
                Button4.Content = correctFlag;
                Button2.Content = flag2;
                Button3.Content = flag3;
                Button1.Content = flag4;
            }
        }


        private void Button1_clicked(object sender, RoutedEventArgs e)
        {
            //MessageBox.Show(correctFlag); // here the message box just shows a blank box
            if ((string)Button1.Content == correctFlag) // this does not activate because of the issue mentioned above
            {
                MessageBox.Show("good job");
            }
        }

        private void Button2_clicked(object sender, RoutedEventArgs e)
        {
            if ((string)Button2.Content == correctFlag) // this does not activate because of the issue mentioned above
            {
                MessageBox.Show("good job");
            }
        }

        private void Button3_clicked(object sender, RoutedEventArgs e)
        {
            if ((string)Button3.Content == correctFlag) // this does not activate because of the issue mentioned above
            {
                MessageBox.Show("good job");
            }
        }

        private void Button4_clicked(object sender, RoutedEventArgs e)
        {
            if ((string)Button4.Content == correctFlag) // this does not activate because of the issue mentioned above
            {
                MessageBox.Show("good job");
            }
        }
Posted
Updated 14-Jun-19 1:59am

1 solution

You have defined a local variable correctflag with this line in
String correctFlag = flags[num.Next(0, flags.Length)];
in public void RandomImage().

This is not the same as assigning a value to
private string correctFlag { get; set; }
Learn about Scope of Variables in C#[^].

Change that first line to be
correctFlag = flags[num.Next(0, flags.Length)];
and while you are about it change the declaration for the random button function to
public void RandomButttons(string flag2, string flag3, string flag4)
 
Share this answer
 
Comments
QuantumNova 14-Jun-19 8:28am    
thank you very much, this helped me a lot :)

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