Multiple instances of webpart with custom EditorPart properties

I have recently stumbled upon an interesting problem in a sharepoint webpart.

I wanted to build a webpart that would dynamically create a contact form based on properties that the user set in the webpart property section. Because i wanted to give the user a dropdown from which he would select a sharepoint list from his site, and this dropdown has to populate on load based on his site, i had to use editorpart .

I won`t write about how this is done, because there are many other great posts in other blogs that explain how to do this : link1 , link2 .

After i have implemented the webpart, i have tested it and observed a problem. If i had only one webpart in the site collection, all went according to plan : the user set the properties based upon his needs and the webpart worked well.

The problem appeared when i added another instance of the webpart on another page from that site.  When i set its properties it changed the properties of first webpart as well. This problem persisted in a subsite as well.

After i googled a little i found 2 threads with the same problem, and no answers (in the meantime, i found the solution and answered).

Link1 

Link2

At first i had the properties of the webpart defined like this (this is an example of one):

public static string _emailReceiver;
[WebBrowsable(false), Personalizable(PersonalizationScope.Shared)]
public string EmailReceiver
{

get { return _emailReceiver; }
set { _emailReceiver = value; }
}
In the public class ContactFormWebPartEditorPart : EditorPart i had a textbox defined :
private TextBox Subject;

The apply changes in the editorpart :

public override bool ApplyChanges()
{

EnsureChildControls();

ContactFormWebPart webPart = WebPartToEdit as ContactFormWebPart;

if (webPart != null)

{

webPart.ContactFormList = _dropDownLists.SelectedValue.ToString();

webPart.EmailReceiver = Email.Text;

webPart.EmailSubject = Subject.Text;

webPart.RedirectTo = redirect.Text;

}

return true;

}

All you have to do is to change the EmailReceiver property from the webpart to:

[WebBrowsable(false), Personalizable(PersonalizationScope.Shared)]

public string EmailReceiver

{

get;

set;

}

I have tested it with 3 different webparts, configured each with different properties. It works! Two of them were on the same page!

Maybe it works because the setter and the getter generate random field names on compile time.

Hope that helps!