Thắc mắc về event

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test
{


    class MyApplication
    {
        private Button openButton;
        private Button saveButton;
        private string fileName;

        public MyApplication()
        {
            this.openButton = new Button("Open File");
            unsafe
            {
                TypedReference tr = __makeref(this.openButton);
                IntPtr ptr = **(IntPtr**)(&tr);
                Console.WriteLine(ptr + " constructor default");// 123
            }

            this.saveButton = new Button("Save File");
            unsafe
            {
                TypedReference tr = __makeref(this.saveButton);
                IntPtr ptr = **(IntPtr**)(&tr);
                Console.WriteLine(ptr + " constructor default");// 456
            }
            //gán method OpenButtonClicked vào event
            this.openButton.OnButtonClick += this.OpenButtonClicked;

            //gán method SaveButtonClicked vào event
            this.saveButton.OnButtonClick += this.SaveButtonClicked;
        }

        private void OpenButtonClicked(Button source, int x, int y)
        {
            unsafe
            {
                TypedReference tr = __makeref(source);
                IntPtr ptr = **(IntPtr**)(&tr);
                Console.WriteLine(ptr + " OpenButtonClicked");// 123
            }
            // Mô phỏng mở ra một cửa sổ để chọn File để mở.
            Console.WriteLine("Open Dialog to Select a file");
         
            this.fileName = "File" + x + "_" + y + ".txt";
            Console.WriteLine("Openning file: " + this.fileName);
        }

        private void SaveButtonClicked(Button source, int x, int y)
        {
            unsafe
            {
                TypedReference tr = __makeref(source);
                IntPtr ptr = **(IntPtr**)(&tr);
                Console.WriteLine(ptr + " SaveButtonClicked");// 456
            }
            if (this.fileName == null)
            {
                Console.WriteLine("No file to save!");
                return;
            }
            // Save File
            Console.WriteLine("Saved file: " + this.fileName);
        }


        public static void Main(string[] args)
        {
            MyApplication myApp = new MyApplication();// tạo 2 button trong constructor default

            Console.WriteLine("\nUser Click on Open Button ....");
            myApp.openButton.Clicked();

            Console.WriteLine();

            Console.WriteLine("User Click on Save Button ....");
            myApp.saveButton.Clicked();


            Console.Read();

        }
    }
    class Button
    {
        private string label;
        public delegate void ClickHander(Button source, int x, int y);

        // Định nghĩa một sự kiện, nó chưa được gán giá trị -> (null)
        // Giá trị của nó được gán từ bên ngoài (public để có thể thao tác từ bên ngoài)
        public event ClickHander OnButtonClick;

        public Button(string label)
        {
            this.label = label;
        }

        public void Clicked()
        {
            Random random = new Random();
            int x = random.Next(1, 100);
            int y = random.Next(1, 20);
            Console.WriteLine(x + " " + y);
            if (OnButtonClick != null)
            {
                unsafe
                {
                    TypedReference tr = __makeref(OnButtonClick);
                    IntPtr ptr = **(IntPtr**)(&tr);
                    Console.WriteLine(ptr);// con trỏ -> A(method open) và B(method save)
                }
                Button a = this;
                unsafe
                {
                    TypedReference tr = __makeref(a);
                    IntPtr ptr = **(IntPtr**)(&tr);
                    Console.WriteLine(ptr + " 2 button");// con trỏ -> 123(openButton) và 456(saveButton)
                }
                OnButtonClick(this, x, y);
            }
        }
    }
}

làm sao mà nó lại tạo 2 con số random giống hệt nhau như vậy nhỉ ? mình đã chạy code và test nơi con trỏ trỏ đến nhưng nó ra 2 con số như vậy làm mình phân vân ko biết tại sao lại như vậy ?
kết quả ra như vậy,2 button là khác nhau ko thể nào có chuyện nó lại ra 1 con số giống nhau như vậy đc.


1 Like
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?