This is a migrated thread and some comments may be shown as answers.

Extension Method

1 Answer 118 Views
NumericTextBox
This is a migrated thread and some comments may be shown as answers.
Dale
Top achievements
Rank 1
Iron
Dale asked on 12 Oct 2016, 08:04 AM

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



1 Answer, 1 is accepted

Sort by
0
Accepted
Daniel
Telerik team
answered on 14 Oct 2016, 05:55 AM
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
Tags
NumericTextBox
Asked by
Dale
Top achievements
Rank 1
Iron
Answers by
Daniel
Telerik team
Share this question
or