Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7781465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:14:07+00:00 2026-06-01T19:14:07+00:00

I have a class which has a property of generic type as given below.

  • 0

I have a class which has a property of generic type as given below. Type T can be any number (short, int, float …)

    public class EqualFilter <T> : Filter {
        private T _value;
        public override T Value {
        get {
            return _value;
        }
        set {
            if (!EqualityComparer<T>.Default.Equals(_value, value)) {
                _value = value;
                RaiseFilteringChanged();
            }
        }
    }
    .....
}

Now I have a client class which has a “String” that needs to be passed to the above setter. Now at the time of setting the value in the above setter, the type T is already determined at runtime and I can get hold of that type as Type T in my client. Is it possible to convert the string to the appropriate type ,as identified by the EqualFilter, in client program? Something like this that is not working

Type T = filter.getFilterType();
filter.Value = (T) myTextBox.Text;

The below code works but it will involve if-else for all the types that can be numeric.

Type T = filter.getFilterType();
if (T == typeof(int)) {
    filter.Value = Int32.Parse(myTextBox.Text);
} else if() {
....
}

In short I am not sure how to approach the issue in a better way.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T19:14:09+00:00Added an answer on June 1, 2026 at 7:14 pm

    It sounds like you’re doing something similar to data binding. If all you’re interested in is working with strings, you might consider constraining the generic type to IConvertible and calling the Convert.ChangeType method from a special setter method like so:

    public class EqualFilter<T> : Filter where T : IConvertible {
    
        // ... omitted Value property code ...
    
        public void SetValue(string value)
        {
            Value = (T)Convert.ChangeType(value, typeof(T));
        }
    
        // ...
    }
    

    The generic type constraint permits only those types that Convert.ChangeType can operate upon. Besides the integral and floating point types, this also permits use of the types string, decimal and DateTime. Just be aware that string types may not always be exact when working with the double type as there can be round-off and floating point representation errors. Also there may be other exceptions that you may want to catch to make this more robust (FormatException and OverflowException, for instance).

    void Main()
    {
        var a = new EqualFilter<int> { Value = 10 };
        var b = new EqualFilter<double> { Value = 20 };
        b.Value = Math.PI; // RaiseFilteringChanged called - no surprise
        b.SetValue(Math.PI.ToString()); // RaiseFilteringChanged called - surprised?
        Console.WriteLine(b.Value);
        b.SetValue("25");
        Console.WriteLine(b.Value);
        var c = new EqualFilter<DateTime> { Value = DateTime.Today };
        Console.WriteLine(c.Value);
        c.SetValue("12/23/2011");
        Console.WriteLine(c.Value);
    
        // compiler error object isn't an IConvertible:
        // var illegal = new EqualFilter<object>();
    }
    

    The big switch statement, in this case, is handled by the framework in the ChangeType method.

    Edit:
    Adding the Console.WriteLine output from my version of the code above (that has overrides and base-class usage commented-out):

    3.14159265358979

    25

    4/10/2012 12:00:00 AM

    12/23/2011 12:00:00 AM

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have some class which has a property actor_ of type Actor .
I have a base class ReportElement which has type property: public abstract class ReportElement
I have an class which has a enum property and a boolean property, based
I have an IPAddress class which has one property named ip and in its
Say I have a class Customer which has a property FirstName . Then I
I have a class which represents a shape. The Shape class has a property
I have a base class with a property called Name, which has an XmlText
I have a component which has a List<T> property. The class in the list
I have a Question class which has a property OptionList , which is nothing
I have a class Exporter which has a generic method which accepts an IEnumerable<T>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.