Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Another EnumConverter problem

Another EnumConverter problem

Scheduled Pinned Locked Moved ASP.NET
csharphelp
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    e laj
    wrote on last edited by
    #1

    I have the following enum:

    \[TypeConverter(typeof(E1C))\]
    public enum E1
    {
    	a = 1,
    	b = 2,
    	c = 3
    }
    

    And the following converter:

    public class E1C : EnumConverter
    {
    	public E1C(): base(typeof(E1))
    	{
    	}
    	public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
    	{
    		if(value != null)
    		{
    			if(destinationType == typeof(string))
    			{
    				if((E1)value == E1.a)
    				{
    					return "a a a";
    				}
    				else if((E1)value == E1.b)
    				{
    					return "b b b";
    				}
    				else if((E1)value == E1.c)
    				{
    					return "c c c";
    				}
    			}
    			if(destinationType == typeof(InstanceDescriptor))
    			{
    				FieldInfo fi = EnumType.GetField(value.ToString());
    				if(fi != null)
    				{
    					return new InstanceDescriptor(fi, null);
    				}
    			}
    		}
    		return base.ConvertTo (context, culture, value, destinationType);
    	}
    
    	public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
    	{
    		if(value != null && value is string)
    		{
    			if((string) value == "a a a")
    			{
    				return E1.a;
    			}
    			else if((string) value == "b b b")
    			{
    				return E1.b;
    			}
    			else if((string) value == "c c c")
    			{
    				return E1.c;
    			}
    		}
    		return base.ConvertFrom (context, culture, value);
    	}
    }
    

    Now, on the property page every thing works fine, as well as in ASP .net page, but within Windows Form, the designer generates the folowing code:

    	#region Windows Form Designer generated code
    	...
    	...		
    	this.userControl11.E1 = WindowsApplication2.E1.b b b;
    	...
    	...
    	#endregion
    

    As you can see, instead of generating: **this.userControl11.E1 = WindowsApplication2.E1.b;** It generated: **this.userControl11.E1 = WindowsApplication2.E1.b b b;** If anyone know this problem and its solution, it would be nice! Thanks in advance! Ilan

    M R 2 Replies Last reply
    0
    • E e laj

      I have the following enum:

      \[TypeConverter(typeof(E1C))\]
      public enum E1
      {
      	a = 1,
      	b = 2,
      	c = 3
      }
      

      And the following converter:

      public class E1C : EnumConverter
      {
      	public E1C(): base(typeof(E1))
      	{
      	}
      	public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
      	{
      		if(value != null)
      		{
      			if(destinationType == typeof(string))
      			{
      				if((E1)value == E1.a)
      				{
      					return "a a a";
      				}
      				else if((E1)value == E1.b)
      				{
      					return "b b b";
      				}
      				else if((E1)value == E1.c)
      				{
      					return "c c c";
      				}
      			}
      			if(destinationType == typeof(InstanceDescriptor))
      			{
      				FieldInfo fi = EnumType.GetField(value.ToString());
      				if(fi != null)
      				{
      					return new InstanceDescriptor(fi, null);
      				}
      			}
      		}
      		return base.ConvertTo (context, culture, value, destinationType);
      	}
      
      	public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
      	{
      		if(value != null && value is string)
      		{
      			if((string) value == "a a a")
      			{
      				return E1.a;
      			}
      			else if((string) value == "b b b")
      			{
      				return E1.b;
      			}
      			else if((string) value == "c c c")
      			{
      				return E1.c;
      			}
      		}
      		return base.ConvertFrom (context, culture, value);
      	}
      }
      

      Now, on the property page every thing works fine, as well as in ASP .net page, but within Windows Form, the designer generates the folowing code:

      	#region Windows Form Designer generated code
      	...
      	...		
      	this.userControl11.E1 = WindowsApplication2.E1.b b b;
      	...
      	...
      	#endregion
      

      As you can see, instead of generating: **this.userControl11.E1 = WindowsApplication2.E1.b;** It generated: **this.userControl11.E1 = WindowsApplication2.E1.b b b;** If anyone know this problem and its solution, it would be nice! Thanks in advance! Ilan

      M Offline
      M Offline
      minhpc_bk
      wrote on last edited by
      #2

      Hi there, You should apply the TypeConverter attribute to the E1 property of the UserControl1 class instead of the enum E1 definition:

      public class UserControl1 : System.Windows.Forms.UserControl
      {
      ...

      \[TypeConverter(typeof(E1C))\]	
      public E1 E1
      {
      	...
      }
                    
      ...
      

      }

      However, IMO this question should belong to the C# forum.

      E 1 Reply Last reply
      0
      • M minhpc_bk

        Hi there, You should apply the TypeConverter attribute to the E1 property of the UserControl1 class instead of the enum E1 definition:

        public class UserControl1 : System.Windows.Forms.UserControl
        {
        ...

        \[TypeConverter(typeof(E1C))\]	
        public E1 E1
        {
        	...
        }
                      
        ...
        

        }

        However, IMO this question should belong to the C# forum.

        E Offline
        E Offline
        e laj
        wrote on last edited by
        #3

        Thanks! Yes i have noticed that this this question fits C#, but only after i pressed "Post" (and then i re-posted it in the C# forum). Seems that i start to like ASP.net:-) And again thank you very much!

        1 Reply Last reply
        0
        • E e laj

          I have the following enum:

          \[TypeConverter(typeof(E1C))\]
          public enum E1
          {
          	a = 1,
          	b = 2,
          	c = 3
          }
          

          And the following converter:

          public class E1C : EnumConverter
          {
          	public E1C(): base(typeof(E1))
          	{
          	}
          	public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
          	{
          		if(value != null)
          		{
          			if(destinationType == typeof(string))
          			{
          				if((E1)value == E1.a)
          				{
          					return "a a a";
          				}
          				else if((E1)value == E1.b)
          				{
          					return "b b b";
          				}
          				else if((E1)value == E1.c)
          				{
          					return "c c c";
          				}
          			}
          			if(destinationType == typeof(InstanceDescriptor))
          			{
          				FieldInfo fi = EnumType.GetField(value.ToString());
          				if(fi != null)
          				{
          					return new InstanceDescriptor(fi, null);
          				}
          			}
          		}
          		return base.ConvertTo (context, culture, value, destinationType);
          	}
          
          	public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
          	{
          		if(value != null && value is string)
          		{
          			if((string) value == "a a a")
          			{
          				return E1.a;
          			}
          			else if((string) value == "b b b")
          			{
          				return E1.b;
          			}
          			else if((string) value == "c c c")
          			{
          				return E1.c;
          			}
          		}
          		return base.ConvertFrom (context, culture, value);
          	}
          }
          

          Now, on the property page every thing works fine, as well as in ASP .net page, but within Windows Form, the designer generates the folowing code:

          	#region Windows Form Designer generated code
          	...
          	...		
          	this.userControl11.E1 = WindowsApplication2.E1.b b b;
          	...
          	...
          	#endregion
          

          As you can see, instead of generating: **this.userControl11.E1 = WindowsApplication2.E1.b;** It generated: **this.userControl11.E1 = WindowsApplication2.E1.b b b;** If anyone know this problem and its solution, it would be nice! Thanks in advance! Ilan

          R Offline
          R Offline
          Ravi C Khambhati
          wrote on last edited by
          #4

          This code is working fine with windows application but it is not working with web applicatioin. I am working with .net 3.5 with vs 2010. Can you please help me out here? You can also send me some sample code to ravick4u@yahoo.co.in Thanks

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups