Getting value of instances of variable.

Joined
Mar 25, 2023
Messages
1
Reaction score
0
Hi, I'm making a wpf app in C#.
I'm trying to figure out a way to get live values of an instance of a boolean variable into another method

In this code, I want to grab the values of bouncerX and bouncerY for each instance of it in a new onClick() Method, so every time the button is clicked the current values of a particular instance are grabbed. I can't figure it out.

I've commented IMPORTANT on the parts i think are most relevant.


C#:
private void Loadinitaldata(object sender, RoutedEventArgs e)
        {
            //Store data from file as List<>
            List<string> lines = fileManager.ReadDataFromFile("ImageData.txt");
            if (lines != null)
            {
                //New instance of MyClass, passing through data to it.
                MyClass myClass = new MyClass(lines);
                //Fetching data from MyClass
                myDict = myClass.GetData();
                //Iterating through each entry
                foreach (KeyValuePair<string, List<string>> entry in myDict)
                {
                    string animal = entry.Key + ": " + string.Join(", ", entry.Value);
                    //Checking if animal allready exists in ListBox1.
                    if (!ListBox1.Items.Contains(animal))
                    {
                        ListBox1.Items.Add(animal);
                        var imageControl = Canvas1.Children.OfType<Image>().FirstOrDefault(i => i.Name == entry.Key);
                        if (imageControl != null)
                        {
                            Canvas.SetLeft(imageControl, int.Parse(entry.Value[0]));
                            Canvas.SetTop(imageControl, int.Parse(entry.Value[1]));
                       
                        }
//IMPORTANT
                        AnimationIndicators indicators = new AnimationIndicators { bouncerX = true, bouncerY = true };
                        ImageAnimation(entry.Key, indicators);
                    }
                }
            }
            else
            {
                MessageBox.Show("Failed to read data from file.");
            }
        }
//IMPORTANT
        public class AnimationIndicators
        {
            public bool bouncerX { get; set; }
            public bool bouncerY { get; set; }
        }

        //A method to animate the image
//IMPORTANT
        public void ImageAnimation(string key, AnimationIndicators animationIndicators)
        {
            if (!imageTimers.TryGetValue(key, out DispatcherTimer timer))
            {
                // Create a new DispatcherTimer if one doesn't exist
                timer = new DispatcherTimer();
                timer.Tick += new EventHandler((sender, e) => Timer_y(sender, e, key, animationIndicators));
                timer.Tick += new EventHandler((sender, e) => Timer_x(sender, e, key, animationIndicators)); // use a lambda to pass in the key
                timer.Interval = new TimeSpan(0, 0, 0, 0, 2);
                imageTimers.Add(key, timer);
            }

            // Start the DispatcherTimer
            timer.Start();
        }

        private void Timer_x(object sender, EventArgs e, string key, AnimationIndicators animationIndicators)
        {
            string imageName = key.ToString();
            int xspeed = int.Parse(myDict[key][2]);

            var imageControl = Canvas1.Children.OfType<Image>().FirstOrDefault(i => i.Name == imageName);
            if (imageControl != null)
            {
                imageControl.Visibility = Visibility.Visible;
            }
            if (imageControl != null)
            {
              
                long positionX = Convert.ToInt64(Canvas.GetLeft(imageControl));
//IMPORTANT
                if (positionX >= 450)
                {
                    animationIndicators.bouncerX = true;
                }
                if (animationIndicators.bouncerX)
                {
                    Canvas.SetLeft(imageControl, positionX - xspeed);
                }
                if (positionX <= 0)
                {
                    animationIndicators.bouncerX = false;
                }
                if (!animationIndicators.bouncerX)
                {
                    Canvas.SetLeft(imageControl, positionX + xspeed);
                }
            }
            else
            {
                MessageBox.Show("whoopsie... error");
            }
        }
[/CODE]


Thanks!
 
Last edited:
Joined
Mar 31, 2023
Messages
95
Reaction score
8
To get live values of an instance of a boolean variable into another method, you can consider the following approach:

  1. Declare a class-level variable to hold the boolean variable value.
  2. Pass this class-level variable to the method that requires the boolean value.
  3. Update the class-level variable whenever the boolean variable changes.
Here's an example code snippet that demonstrates this approach:

C#:
public partial class MainWindow : Window
{
    private bool bouncerX = false; // Declare class-level variable
    private bool bouncerY = false; // Declare class-level variable

    private void Loadinitaldata(object sender, RoutedEventArgs e)
    {
        // Your code here
    }

    private void ImageAnimation(string key)
    {
        // Pass the boolean variables to the Timer_x method
        Timer_x(key, bouncerX, bouncerY);
    }

    private void Timer_x(string key, bool bouncerX, bool bouncerY)
    {
        // Your code here
        if (positionX >= 450)
        {
            bouncerX = true; // Update the class-level variable
        }
        // Your code here
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        // Call the ImageAnimation method and pass the current values of the boolean variables
        ImageAnimation("key", bouncerX, bouncerY);
    }
}
In the Loadinitaldata method, you can initialize the class-level boolean variables based on your requirements. Then, in the Button_Click event handler, you can call the ImageAnimation method and pass the current values of the boolean variables. In the ImageAnimation method, you can pass the boolean variables to the Timer_x method. Finally, in the Timer_x method, you can update the class-level boolean variables whenever their values change.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top