24

After several days of happily hacking away on this C# app using Visual Studio 2008, I get struck by a barrage of error dialogs showing:

Code generation for property 'valueMember' failed.
Error was: 'Object reference not set to an instance of an object.'

This happens now often when I make a tiny change in the designer, e.g. shift a control a few pixels, and then try to save. Several such error dialogs appear each second, keeping me busy cancelling all those by hammering the Enter key while trying to get alt-F4 to get VS to close.

Eventually I do get VS to close and to save the changes I made. After restarting VS, I do "clean" on the entire project, then "build" and everything works fine, the app runs fine, no problems.

Until I make another slight change in the form designer.

I don't know about any property valueMember in my app.

This makes me crazy, it is a real showstopper for my project. Any help is appreciated.

Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
Roland
  • 4,619
  • 7
  • 49
  • 81
  • 3
    Sounds like the designer has a problem with the *.designer.cs files that sit behind each of your forms. That would be where to start looking. – Polyfun Nov 22 '12 at 17:12

9 Answers9

22

Try to Close and reopen the Visual Studio. maybe it seem silly, but it works!!

Chani Poz
  • 1,413
  • 2
  • 21
  • 46
  • 2
    A minor time-saver, but closing and opening the solution within Visual Studio worked for me. I had to discard the changes to the particular Form causing the error dialog to show before I could close it though. – Matt Arnold Nov 10 '20 at 12:21
16

You can debug the designer using another visual studio and attach to process. If you got exception it should be easy to find it that way. In general when openning the designer the constructor and of course initializeComponent is running.

shanif
  • 464
  • 1
  • 4
  • 12
13

As this is happening at design time, it is likely that you have a custom control which requires a parameter or other value which does not have a default.

When in design view in Visual Studio; a control instance is created to render it on the visual editor, but if the control requires a property to be set before it can be rendered, it will result in an error.

Can you check that all custom controls have default values, and anything referenced in the constructor that cannot have a default is wrapped by DesignMode property - see http://msdn.microsoft.com/en-us/library/system.componentmodel.component.designmode.aspx.

Kami
  • 19,134
  • 4
  • 51
  • 63
  • Thanks for the hints. I don't think I have custom controls in this project I inherited to maintain, but a global search does find 'valueMember' properties in some X.designer.cs files. When the errors appear again, I will study that code. This leaves the problem why the error message appears in infinite numbers instead of only once. – Roland Nov 23 '12 at 08:52
  • THis is a huge pile of inherited code, and I have added default values to lots of variables. This is a good design principle anyway. This resulted in much fewer null-ref exceptions in general, and the VS problem also doesn't return anymore. – Roland Sep 05 '14 at 12:37
2

Similiar to @Chanipoz's answer (close/re-open) my component-rich/user-controls-everywhere forms app started to compile happily after I closed down the main form designer window.

I've had this code stack for years and have never seen the error until today. Not sure where it's coming from. But, something today about having the form open in the designer made everything unhappy. Simply closing it off of the screen made it all go smooth.

bkwdesign
  • 1,953
  • 2
  • 28
  • 50
  • Did you also suffer from a barrage of error dialogs that came in faster than you could close them? – Roland Jan 28 '15 at 10:43
  • only seemed like a handful.. but.. it was very dedicated to issuing those error boxes as long as that particular form was open in the designer – bkwdesign Jan 28 '15 at 19:28
1

Use another instance of Visual Studio to attach to the first instance of visual studio.

Go to Debug-> Attach To Process and look for the devenv.exe process. Since you'll have two devenv.exe processes running you'll probably want to pick the one with the lower ID, that's usually the first instance of visual studio that was run.

Garrett Banuk
  • 111
  • 1
  • 3
  • Thanks for your answer. As I have upgraded from VS2008 to VS2017 in several steps over the past 7 years and am working on much different code, much more structured, I have not seen this problem anymore. But in case someone might experience it today, your answer may be helpful. – Roland Sep 24 '19 at 13:25
1

I had to face this problem. As I have found the solution below I am facing this issue in my customized control.

we need to implement like this

[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public MyCustomclass _Prperty { get; set; }
Peter Csala
  • 17,736
  • 16
  • 35
  • 75
Ponsingh A
  • 11
  • 1
  • My problem happened 10 years ago with a VS version 14 years old. Did you see the same problem recently with VS2022? – Roland Jan 28 '22 at 15:49
0

I had to face this problem. As I have not found the solution (much inheritance), I can tell: .SuspendLayout() and .ResumeLayout() may be missing in code or one of them. The same is with .BeginInit() and .EndInit(). It is expected between them, that there will be = new ... and some settings for properties. Maybe someone facing this problem would find the solution with this information.

pbies
  • 666
  • 11
  • 28
  • 1
    Also, while there is inheritance, important are modifiers: public, private, protected - for controls. – pbies Mar 18 '14 at 23:40
0

The problem is missing initialization code for a public property on the control. This will be added for you when you add the control to the designer, but if you replace a control with a derived control, or update the component, then the designer does not know how to deal with this.

If you have a control (wincontrol) with a public property PropertyA, and you add it to a form (myForm), then the designer will add all the necessary initialization for properties into myForm.Designer.cs. Something like;

Wincontrol1.PropertyA = new List<widget>();

It is not uncommon to need to modify a control slightly, lets say we have a new control MyWinControl

public partial class MyWinControl : WinControl
{
    public List<wodget> PropertyDer1;
    protected List<wodget> PropertyDer2;
}

If you sub this new control for the old control in myForm.Designer.cs, then you may well encounter this issue. The reason is that PropertyDer1 has no initialization in the winforms designer. PropertyDer2 won't cause any issues because it is protected. Similarly if you had a custom component and you add a new public property after the component has been added to a form.

If however, you deleted the instance of WinControl on the form, and dragged an instance of the MyWinControl onto the form instead, the proper initialization would occur and you would not see the error. The designer will have created the new control like this

Wincontrol1.PropertyA = new List<widget>();
Wincontrol1.PropertyDer1= new List<wodget>();

There are two easy solutions that do not require hiding the property from the designer. 1. If the property doesn't need to be public, give it the right modifier 2. If the property does need to be public, then just edit the code in the myForm.Designer.cs as in the code above to add the missing initializer

statler
  • 1,322
  • 2
  • 15
  • 24
0

If could be of help I just detected a case that brings that same error message, impossible to take away : I am developing an application in French, and I had to create a ToolStripMenuItem with an accented word in it like "annulées". The system generated a menu item like "annuléesToolStripMenuItem" and the accent is the culprit. Enough to delete the item, create it again in English and the just change the Text property of the menu item. Hope it will be of some help.

  • Did the unicode method name just cause a build error or run exception, or did this generate many many many error dialogs, faster than you can close them? – Roland Jul 13 '21 at 08:15
  • No, it just generate a quite long error message and stopped the publishing. Correcting the accented letter did solve the problem. – Claudio-it Jul 15 '21 at 14:59