We help IT Professionals succeed at work.

Using TypeConverter for Parameterized Constructor Class

saktiranjan
saktiranjan asked
on
Hi,
Can somebody help to know how to use a class having parameterized constructor inherted from typeconverter in the property grid. That means how can I pass parameters to constructor while defining property. Below is the code for your reference.

I have a custom pipeline component and I am adding three custom properties to input columns. Different properties will have different static values. So I have developed a class inherting from Type Converter.

public class BooleanList : TypeConverter
{
    //public BooleanList(string displayValues)
    //{
    //    if (displayValues.ToLower() == "boolean")
    //    {
    //        m_list.Add("Y");
    //        m_list.Add("N");
    //    }
    //   else if (displayValues.ToLower() == "datatypes")
    //    {
    //        m_list.Add("String");
    //        m_list.Add("Numeric");
    //        m_list.Add("DateTime");
    //    }
    //}

    public BooleanList()
    {
        m_list.Add("Y");
        m_list.Add("N");
    }
    public List<string> m_list = new List<string>();

    public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
    {
        return true;
    }
    public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
    {
        return true;
    }
    public StandardValuesCollection GetValues()
    {
        return new StandardValuesCollection(m_list);
    }
    protected void SetList(List<string> list)
    {
        m_list = list;
    }
    public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
    {
        return GetValues();
    }
}

The defined property will use the above class and the property will have static values (Y & N) as coded below.
propCheckNull.TypeConverter = typeof(BooleanList);
Now look at the commented part in above class. I want to use this parameterized constructor and want to send parameters while assigning to property values so that I will get static values as per the parameter. Instead of typeof(BooleanList), want to write something else where I can pass parameters.

Let me know if something is unclear.

Thanks
Sakti
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
Huh?  I don't understand your requirement.

Bob

Author

Commented:
OK... let me explain little more. The full code which I have posted, is a class which creates a list of values. And if you use this class in a custom property like below, you will get property values in the property grid as dropdown list.
[TypeConverter(typeof(DefineProperty))]
        public string SourceConnection
        {
            get { return this.strConnection; }
            set { strConnection = value; }
        }

In my case I am using this [TypeConverter(typeof(DefineProperty))] in a input column of data flow pipeline component in ssis as
propCheckNull.TypeConverter = typeof(BooleanList); where propCheckNull is a custom property.
Now, Can I use the same class for loading different static values in the property? If I can pass parameter to the constructor, different values can be loaded as per the parameter. But how to write that, I dont know.

Let me know if you need something specific.

Sakti
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
Does that mean that you want to use a single TypeConverter to return different values depending on how it is used?  What value would this have versus having 2 different TypeConverter classes?

Bob

Author

Commented:
I am expecting more than 10 values to be returned on 10 different situation. For each thing, its not good idea to write those many TypeConverter classes.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008
Commented:
Why not?  I don't see any problem with a base class for each TypeConverter, for the common things, and then a deriving class for the return values.

Bob

Author

Commented:
I struggled a lot but did not find any other solution except this, though I am not totally convinced.