Extension Method

2 posts, 1 answers
  1. Dale
    Dale avatar
    5 posts
    Member since:
    Oct 2016

    Posted 12 Oct Link to this post

    Ive written some extension methods for various UI components for example here is one for the TextBox

     

            public static TextBoxBuilder<T> Width<T>(this TextBoxBuilder<T> builder, int width)
            {
                return builder.HtmlAttributes(new { @style = "width:" + width.ToString() + "px" });
            }

     

    However I cant get the syntax correct for the NumericTextBoxBuilder

     

            public static NumericTextBoxBuilder<T> Width<T>(this NumericTextBoxBuilder<T> builder, int width)
            {
                return builder.HtmlAttributes(new { @style = "width:" + width.ToString() + "px" });
            }

     

    I get a message The type 'T' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'NumericTextBoxBuilder<T>'

     

    I could find an example working on google, can anyone assist



  2. Answer
    Daniel
    Admin
    Daniel avatar
    4945 posts

    Posted 14 Oct Link to this post

    Hello Dale,

    You need to add constraint in this case
    public static NumericTextBoxBuilder<T> Width<T>(this NumericTextBoxBuilder<T> builder, int width) where T:struct
    {
        return builder.HtmlAttributes(new { @style = "width:" + width.ToString() + "px" });
    }

    Constraints on Type Parameters

    I hope this helps.

    Regards,
    Daniel
    Telerik by Progress
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. UI for ASP.NET MVC is VS 2017 Ready
Back to Top