开发者

Custom control property as enum

I have a custom control which is a combination of textbox and button (a search control). One of its attribute defined is used to identify its position (whether in Master or Content page so that we make the search settings) that is PageName. Initially this was declared as string property as:

public string PageName
{
    set;
    get;
}

But since we had some issues with case sensitivity. We changed this as enum, so that users will not enter any random things avoiding the exceptions. So this is now changed to:

public enum PageNameProperty { MasterPage , SearchResultsPage, MLIPage };

public P开发者_如何学编程ageNameProperty PageName
{
   set;
   get;
}

But now we are getting the error as :

Method not found:'Void UI.SearchTextBox.set_PageName(System.String)'.

Inner Exception (Level 1)

Note: Now i cannot force users to change this to PageName.Master or anything else, since they cannot modify the code for current release but can only install our control, so the projects would refer the new DLLs.

I need some solution apart from replicating the property type back to string before the current release.

Please help.


You changed the signature (return type for instance) of a property. Calling code will fail as they won't find the signature they are expected.

You should keep the original property, mark it as obsolete, and create a new one like this :

[Obsolete("User PageName2")
public string PageName {
{
 get { return PageName2.ToString(); }
set { PageName2 = (PageNameProperty )Enum.Parse(typeof(PageNameProperty), value);
}

public PageNameProperty PageName2 { set; get; }

This will keep compatibility with former use, while incitating the use of the new one.


Could you do something like this?:

public enum PageNameProperty { MasterPage , SearchResultsPage, MLIPage };  
private PageNameProperty pageName;

public PageNameProperty PageName 
{ 
   get
   {
       return pageName ?? PageNameProperty.MLIPage;
   }
   set
   {
       pageName = value;
   }
} 


You could simply validate the values to ensure they are using a valid string

private string _pageName = null;

public string PageName
{
    set 
    { 
        if (value == null)
           throw new NullReferenceException();
        if (!(new string[] { "master", "content" }.Contains(value.ToLower()))
           throw new InvalidOperationException();

        _pageName = value; 
    }
    get
    {
       return _pageName;
    }
}

At design time, this would give the client an error stating that they entered an invalid value.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜