开发者

Is my event set up properly and efficiently?

开发者 https://www.devze.com 2023-02-18 14:44 出处:网络
I just started using events and delegates in C# and I\'m not sure if this is how I should have it set up.

I just started using events and delegates in C# and I'm not sure if this is how I should have it set up.

MenuScreen.cs

public class MenuScreen {
        public class MenuScreen {

        public List <MenuEntry> _entries = new List<MenuEntry> {};

        public void Initialize () {
            foreach (MenuEntry entry in _entries) {
                entry.EntrySelected += onEntrySelected;
            }
        }

        private void onEntrySelected (object sender, MenuEntryEventArgs e) {
            Debug.WriteLine (e._text);
        }
}

MenuEntry.cs

public class MenuEntry {
    public delegate void EventHandler (object sender, MenuEntryEventArgs e);

    public event EventHandler EntrySelected;    

    private ButtonState _clickstate = ButtonState.Released;//Left mouse button state

    public void Draw (MenuScreen menuscreen, Vector2 position) {
        if (Mouse.GetState ().LeftButton == ButtonState.Pressed) {//If left mouse button is pressed
            if (_clickstate == ButtonState.Released &&开发者_StackOverflow社区 bounds.Contains (x, y)) {//If left mouse button isn't already pressed and mouse is in entry hitbox
                onEntrySelected ();
            }

            _clickstate = ButtonState.Pressed;//Left mouse button state is pressed
        } else {//If left mouse button is released
            _clickstate = ButtonState.Released;//Left mouse button state is released
        }

    protected virtual void onEntrySelected () {
        EventHandler handler = EntrySelected;
        MenuEntryEventArgs args = new MenuEntryEventArgs (_text);

        if (handler != null) {
            handler (this, args);
        }
    }
}

I assume this must be better? I looked at the thread he posted the link to and used that as a guideline.


Please use the standard source code conventions for .NET. The code is much more readable.

public class MenuScreen {
    // the common pattern for events is method which can event raise
    // the event-raising methods should have prefix "On-"
    protected internal virtual void OnEntrySelected( EventArgs e ) {
        // delegates are immutable, so variable "handler" will not be changed
        var handler = this.EntrySelected;
        // check for null. null handler can not be raised
        if ( null != handler ) {
            // raise handler
            handler(this, e);
        }
    }
    // the common pattern for event handler are arguments "sender" and "args".
    // the public members should start with upper case
    // the common pattern for events "something happend" is suffix "-ed"
    public event EventHandler EntrySelected;
}

public class MenuEntry {
    public void Draw(MenuScreen screen, Vector2 position) {
        // ... some code ...
        screen.OnEntrySelected( EventArgs.Empty );
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消