0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

C# delegateを使ってみる

Posted at

delegateについて学んだので備忘録。

using System;
using System.Windows.Forms;

namespace _test
{
    public partial class Form1 : Form
    {
        public int count = 0;
        delegate void DelegatePutsMessage(int cnt);     // DelegatePutsMessage型のdelegateを定義
        DelegatePutsMessage PutsCount = null;           // DelegatePutsMessage型のPutsCount変数を定義

        public Form1()
        {
            InitializeComponent();
            PutsCount = new DelegatePutsMessage(FuncPutsMessage);   //DelegatePutsMessage型PutsCount変数へ、FuncPutsMessage関数を代入する
        }


        private void button1_Click(object sender, EventArgs e)
        {
            this.count++;
            PutsCount(this.count);      // PutsCount変数に代入されているFuncPutsMessage関数を呼び出す
        }

        private void FuncPutsMessage(int cnt)
        {
            textBox1.Text = "カウント+1= " + cnt;
        }
    }
}
0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?