locked
Recurring designer errors RRS feed

  • Question

  • I'm not sure what exactly I am doing wrong, but I seem to run into one particular issue over and over. Hopefully someone can point me in the right direction so I can stop doing this.

    In my winform development, I keep running into errors on the designer where I will open a user control and it will display some error. Usually it's something like

    Object of type 'System.Collections.Generic.List`1[XXXX.Foundation.Model.Demographics.Address]' cannot be converted to type 'System.Collections.Generic.IList`1[XXXX.Foundation.Model.Demographics.Address]'.

    at System.RuntimeType.CheckValue(Object value, Binder binder, CultureInfo culture, BindingFlags invokeAttr)
    at System.Reflection.RtFieldInfo.InternalSetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, CultureInfo culture, Boolean doVisibilityCheck, Boolean doCheckConsistency)
    at System.Reflection.RtFieldInfo.InternalSetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, CultureInfo culture, Boolean doVisibilityCheck)
    at System.Runtime.Serialization.SerializationFieldInfo.InternalSetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, CultureInfo culture, Boolean requiresAccessCheck, Boolean isBinderDefault)
    at System.Runtime.Serialization.FormatterServices.SerializationSetValue(MemberInfo fi, Object target, Object value)
    at System.Runtime.Serialization.ObjectManager.CompleteObject(ObjectHolder holder, Boolean bObjectFullyComplete)
    at System.Runtime.Serialization.ObjectManager.DoNewlyRegisteredObjectFixups(ObjectHolder holder)
    at System.Runtime.Serialization.ObjectManager.RegisterObject(Object obj, Int64 objectID, SerializationInfo info, Int64 idOfContainingObj, MemberInfo member, Int32[] arrayIndex)
    at System.Runtime.Serialization.Formatters.Binary.ObjectReader.RegisterObject(Object obj, ParseRecord pr, ParseRecord objectPr, Boolean bIsString)
    at System.Runtime.Serialization.Formatters.Binary.ObjectReader.ParseObjectEnd(ParseRecord pr)
    at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Parse(ParseRecord pr)
    at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run()
    at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
    at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
    at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream)
    at System.Resources.ResXDataNode.GenerateObjectFromDataNodeInfo(DataNodeInfo dataNodeInfo, ITypeResolutionService typeResolver)
    at System.Resources.ResXDataNode.GetValue(ITypeResolutionService typeResolver)
    at System.Resources.ResXResourceReader.ParseDataNode(XmlTextReader reader, Boolean isMetaData)
    at System.Resources.ResXResourceReader.ParseXml(XmlTextReader reader)

    When I get this, I can no longer edit the form.


    In my typical applications, I will created some user controls to handle the editing of certain types of objects. So for an Address object, I will have an EditAddress user control, for a Person object I will have an EditPerson user control. That EditPerson control will contain an EditAddress control as well as other controls. I may even have an EditCustomer user control which will contain an EditPerson user control. And so on.

    Each control will contain a public property for it's corresponding model object. So EditAddress will contain an Address property. The getter will fill the Address object with the values from the various text,comboboxes and return the address object. The setter will set the text,combobox values. This is the same for all the user controls.

    So in my application if I need to edit a customer, I simply create an EditCustomer control, set the Customer property to my customer object. And when I'm done, I simply access the Customer property in the EditCustomer object and that triggers all the getters and in the end I have my Customer object filled with all the values from the various edit user controls.

    Besides the that error I listed, I sometimes get errors related to some of the data methods being called. For example, the EditAddress control when loaded will call a LoadCountries method, which will populate the Country combo box with a list of countries from the database. I have gotten around these by putting statements like  if (DesignMode) return; to prevent this. But I find my self putting these all over the place. Is there a better way to handle this situation? Something cleaner?

    I think one alternative I tried was basically getting rid of the object as properties and just making them public variables. But then I had to create methods to read the data from the objects and write the data to the objects.

    Saturday, November 8, 2008 7:28 PM

Answers

  • This is a generic answer and does not attempt to address your specific collection issue:

    Your Forms/Controls run in the designer just as they do at runtime. Often times I forget this fact and my Forms fail to load into the designer because the code relies on something external that either is not there, or just hasnt loaded yet. These things get loaded in a 'real' run of the system.

    The solution to this is to use the DesignMode property of the Form/Control to test if we are loaded into a designer and to skip processing of external sources if we are.


    As for AlexBB, I don't think English is his or her primary language and so translation/cultural bumps can lead to mis-interpretation of intent <wink>.
    Sunday, November 9, 2008 7:02 PM

All replies

  •   I sometimes get errors related to some of the data methods being called. For example, the EditAddress control when loaded will call a LoadCountries method, which will populate the Country combo box with a list of countries from the database.

    Do you mean that you get this behavior irregularly, once in a while?

    Anyhow, the first error is obviously your fault. You should not complain. You should do proper typing. IList is not List. IList is an interface that serves many other classes besides List. You can show the code to get help on that.

    Any time you have irregular errors you should first try to clean the Solution. If that does not help, reinstall VS2008.

    AlexB
    Saturday, November 8, 2008 7:47 PM
  • Ah typical AlexBB response...


    Do you mean that you get this behavior irregularly, once in a while?

    Yup, but its probably do to some change I've made. Meaning, when I start a new application everything is fine. But as I continue adding/changing things, eventually I run into these.


    Anyhow, the first error is obviously your fault. You should not complain. You should do proper typing. IList is not List. IList is an interface that serves many other classes besides List. You can show the code to get help on that.



    First I'm not complaining. And I never stated that it's not my fault. Try reading my message again, maybe slowly. You can clearly see from the first couple of lines that I have already assumed I'm doing something wrong and I'm asking for help to see what it could be.


    As for the types I'm using. All my collections use the following

    System.Collections.ObjectModel.Collection<>

    Unless I'm binding it to a data grid that allows editing directly within the grid view. In which case I'll use System.ComponentModel.BindingList<>

    If this is wrong, let me know.


    Any time you have irregular errors you should first try to clean the Solution. If that does not help, reinstall VS2008.

    Yup, clean, restart VS, restart computer, tried them all. I suspect it's something related to using properties with getters/settings in the user controls. This causes the designer to add the property initialize code in the designer file, then triggering these errors from either some issue it has creating the object or triggering code in the setter.



    Saturday, November 8, 2008 8:30 PM
  •  You should post the code that causes the trouble. It is in the StackTrack. There is a line there pointing to the  line of code that causes the immediate trouble however there is a remote change that the problem was seeded some time before that, upstream. Anyway. Post your code.

    As far as the DataBinding goes I believe you are on the right track. I actually had it in mind to mention that but thought it would be a wild speculation based on your code-free OP.

    What do you mean by a typical post of mine. What do you not like about it:) ?

    AlexB
    Saturday, November 8, 2008 9:58 PM
  • This is a generic answer and does not attempt to address your specific collection issue:

    Your Forms/Controls run in the designer just as they do at runtime. Often times I forget this fact and my Forms fail to load into the designer because the code relies on something external that either is not there, or just hasnt loaded yet. These things get loaded in a 'real' run of the system.

    The solution to this is to use the DesignMode property of the Form/Control to test if we are loaded into a designer and to skip processing of external sources if we are.


    As for AlexBB, I don't think English is his or her primary language and so translation/cultural bumps can lead to mis-interpretation of intent <wink>.
    Sunday, November 9, 2008 7:02 PM