LoginSignup
4

More than 5 years have passed since last update.

posted at

updated at

C#でオブジェクトからオブジェクトへ変数を渡すには

ここでは落ち物ゲームの落ち物が一番下まで落ちたときにFallenBottomというイベントをつくる(ここは別にどうでもいい)。

変数を渡す元のオブジェクトで

変数を渡すために、変数を格納するクラスをつくる

00.cs
public class FallenBottomEventArgs : EventArgs
{
    public int FallenX;
    public FallingRect FallingRect;
}

デリゲートをつくる

01.cs
public delegate void FallenBottomEventHandler(object sender, FallenBottomEventArgs e);

イベントデリゲートをつくる

02.cs
public event FallenBottomEventHandler FallenBottom;

メソッドをつくる

03.cs
protected virtual void OnFallenBottom(FallenBottomEventArgs e)
{
    if (FallenBottom != null)
    {
        FallenBottom(this, e);
    }
}

返すデータの設定

04.cs
FallenBottomEventArgs e = new FallenBottomEventArgs();
e.FallenX = fallingRect.X;
e.FallingRect = fallingRect;

変数を渡す先のオブジェクトで

最後にイベントの発生を受け取る

05.cs
OnFallenBottom(e);

ブログやってます:PAPA-tronix !

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
What you can do with signing up
4