Tim Abell
Tim Abell

Reputation: 11901

Are there any pitfalls for making an event handling method static?

I have the following code (from https://github.com/timabell/PageStructureBuilder ) and the ever knowledgeable ReSharper is suggesting that I make the DataFactoryCreatingPage() method static.

Is this safe, or would I be introducing a potential fault when used as an event handler?

public void Initialize(InitializationEngine context)
{
    DataFactory.Instance.CreatingPage += DataFactoryCreatingPage;
    DataFactory.Instance.MovedPage += DataFactoryMovedPage;
}

void DataFactoryCreatingPage(object sender, PageEventArgs e)
{
    var parentLink = e.Page.ParentLink;
    var page = e.Page;
    parentLink = GetNewParent(parentLink, page);

    e.Page.ParentLink = parentLink;
}

I can't think of any issues, but I'm wondering if I have a gap in my knowledge.

Thanks!

Upvotes: 2

Views: 189

Answers (2)

Aliostad
Aliostad

Reputation: 81680

Resharper would do that if your method only uses the parameters passed in and does not access any member variables.

You dont have to do it.

Upvotes: 1

scibuff
scibuff

Reputation: 13755

there's no reason why that handler should be static; the reason ReSharper suggests to make it static is probably because you're not using any instance variables within its body, so there's no harm, but event handlers shouldn't be static as they should be able to modify/use internal fields of the class that provides the handler's implementation

Upvotes: 2

Related Questions