Implicit Operator and Binding

Implicit Operator and Binding

Old forum URL: forums.lhotka.net/forums/t/2063.aspx


cultofluna posted on Tuesday, January 02, 2007

Hello everyone,

I have two BO's: Country and Street.

Country has a string value property that contains the ISO countrycode. It also uses an implicit operator to assign and read this value.

Street has a string value property that contains the streetname and a Country property that contains the country.

I want to use the Street in my form and edit it using binding. So I have two textboxes, one bound to the string property and one to the Country property. I also use an errorprovider to notify of any broken rules.

When I first access the BO the value of the Country is shown without any problems. So here the implicit operator seems to work. However when assigning the errorprovider shows an "Invalid cast System.String to Library.Country".

Can binding only be used with properties of the same type as the property of the control it's bound to?

It seems like it has something todo with formatting the value...

System.FormatException: Invalid cast from 'System.String' to 'H2O.Library.Type.Country'. ---> System.InvalidCastException: Invalid cast from 'System.String' to 'H2O.Library.Type.Country'.
   at System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)
   at System.String.System.IConvertible.ToType(Type type, IFormatProvider provider)
   at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
   at System.Windows.Forms.Formatter.ChangeType(Object value, Type type, IFormatProvider formatInfo)
   --- End of inner exception stack trace ---
   at System.Windows.Forms.Formatter.ChangeType(Object value, Type type, IFormatProvider formatInfo)
   at System.Windows.Forms.Formatter.ParseObjectInternal(Object value, Type targetType, Type sourceType, TypeConverter targetConverter, TypeConverter sourceConverter, IFormatProvider formatInfo, Object formattedNullValue)
   at System.Windows.Forms.Formatter.ParseObject(Object value, Type targetType, Type sourceType, TypeConverter targetConverter, TypeConverter sourceConverter, IFormatProvider formatInfo, Object formattedNullValue, Object dataSourceNullValue)
   at System.Windows.Forms.Binding.ParseObject(Object value)
   at System.Windows.Forms.Binding.PullData(Boolean reformat, Boolean force)

cultofluna replied on Tuesday, January 02, 2007

OK, I figured it out, it was the formattingEnabled parameter in the binding:

this.valueTextBox1.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.streetBindingSource, "Country", false));

Still not completely solved however, it's seems like because the implicit creates a new instance the binding is lost as well...

I'll be back with the answer to that..

cultofluna replied on Tuesday, January 02, 2007

OK, I think this is a cool feature, where you can use implicit operators to create BO datatypes that can be used in the same manner you use for example a string datatype!

The trick was adding an extra function called BindControls in the form. Here I define binding for the textboxes that bind to BO datatypes and specify the format and parse events for the binding.

Now I can use BO datatypes with implicit operators combined with formatting!


tiago replied on Tuesday, August 12, 2008

cultofluna:
OK, I think this is a cool feature, where you can use implicit operators to create BO datatypes that can be used in the same manner you use for example a string datatype!

The trick was adding an extra function called BindControls in the form. Here I define binding for the textboxes that bind to BO datatypes and specify the format and parse events for the binding.

Now I can use BO datatypes with implicit operators combined with formatting!


Can you explaina bit how you solved it?

TIA

cultofluna replied on Tuesday, August 12, 2008

hello tiago,

well actually I stopped using it... :)

It costed to many hours in maintaining all the code, so the deadline of my project won from this solution...

I'm sorry I don't have the code anymore, maybe I will try again in future.

Good luck!

CoL

Copyright (c) Marimer LLC