Why isn't my UserControl keeping its initialization?


I have a UserControl for which I <em>think</em> I'm initializing some of the members:

// MyUserControl.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; namespace MyNamespace { public partial class MyUserControl : UserControl { private string m_myString; private int m_myInt; public string MyString { get { return m_myString; } set { m_myString = value; } } public int MyInt { get { return m_myInt; } set { m_myInt = value; } } public MyUserControl() { InitializeComponent(); MyString = ""; // was null, now I think it's "" MyInt = 5; // was 0, now I think it's 5 } // ......... } }

When I insert this control into my main form, though, and call a function that checks values within MyUserControl, things don't look like they're initialized:

// MainForm.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace MyProgram { public partial class MainForm : Form { public MainForm() { InitializeComponent(); } private void MyButton_Click(object sender, EventArgs e) { // this prints 0 rather than 5 MessageBox.Show(this.theUserControl.MyInt.ToString()); } } }

I'm guessing this is a <em>really</em> simple error but I don't know where. I've tried prepending this. to things, but this probably isn't the way to go about fixing code. :)

Thanks as always!

<strong>EDIT:</strong> Stepping into the designer code as Pete suggested showed me where the write-over was happening. First I called the constructor of the user control, and then later, the values got overwritten with default values. I hadn't specified any default values (Sanjeevakumar Hiremath's suggestion) so the default values were those of the primitive types (for int this was 0).


What you're likely seeing here is an artifact of the designer. If you ever opened up MainForm in a designer after you added MyUserControl it likely recorded the default values of MyUserControl in the generated InitializeComponent method of MainForm. These recorded values are re-assigned after the constructor of MyUserControl runs hence they're overwriting the values you set.

You can control this behavior via the use of the DesignerSerializationVisibilityAttribute

<ul><li><a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.designerserializationvisibilityattribute.designerserializationvisibilityattribute.aspx#Y375" rel="nofollow">http://msdn.microsoft.com/en-us/library/system.componentmodel.designerserializationvisibilityattribute.designerserializationvisibilityattribute.aspx#Y375</a></li> </ul>


Use <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.defaultvalueattribute%28v=VS.100%29.aspx" rel="nofollow">[DefaultValue]</a> Attribute. It lets you specify default value for a property of a control when the value is not specified in the designer.


  • serialize list of generic type
  • A “dynamic bitfield” in C
  • Write int to binary buffer (Android) and read with C++
  • Convert NSData to signed integer
  • How to count amount of elements in a row of a matrix in C
  • XSLT foreach repeating nodes to flat
  • Does Apple allow the usage of sysctl.h within iOS applications?
  • How to add git credentials to the build so it would be able to be used within a shell code?
  • QLineEdit password safety
  • Nant, Vault & Windows Integrated Authentication
  • PHP - How to update data to MySQL when click a radio button
  • Bug in WPF DataGrid
  • output of program is not same as passed argument
  • Join two tables and save into third-sql
  • How to handle AllServersUnavailable Exception
  • How to make Safari send if-modified-since header?
  • Adding custom controls to a full screen movie
  • jQuery tmpl and DataLink beta
  • How can I estimate amount of memory left with calling System.gc()?
  • Akka Routing: Reply's send to router ends up as dead letters
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • SQL merge duplicate rows and join values that are different
  • Proper way to use connect-multiparty with express.js?
  • How to set the response of a form post action to a iframe source?
  • Change div Background jquery
  • How does Linux kernel interrupt the application?
  • Qt: Run a script BEFORE make
  • unknown Exception android
  • Checking variable from a different class in C#
  • Django query for large number of relationships
  • Busy indicator not showing up in wpf window [duplicate]
  • reshape alternating columns in less time and using less memory
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • How can I use `wmic` in a Windows PE script?
  • Why do underscore prefixed variables exist?
  • Net Present Value in Excel for Grouped Recurring CF
  • How to push additional view controllers onto NavigationController but keep the TabBar?
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?