The variable 'variable_name' is either undeclared or was never assigned
Solution 1
So, I have had the same problem in the past, for fix I did the following:
- Solution → Clean Solution;
- Build → Rebuild Solution;
- Close Visual Studio, re-open.
Thanks a lot to Marshall Belew!
Solution 2
In my case, I had an older Windows Forms project where InitializeComponents()
started like this:
private void InitializeComponent()
{
var componentResourceManager = new ComponentResourceManager(typeof(MyForm));
...
This resulted in an error message later on when accessing the componentResourceManager
inside InitializeComponent()
:
The variable 'componentResourceManager' is either undeclared or was never assigned.
When comparing with a newly created form, I saw that it was similar to my non-working form, except for one thing:
The variable was not named componentResourceManager
but simply resources
.
Solution
After doing a rename on my variable to also have the name resources
, everything works successfully:
private void InitializeComponent()
{
var resources = new ComponentResourceManager(typeof(MyForm));
...
The Windows Forms Designer in Visual Studio 2017 did open the form correctly.
Solution 3
I ran into this error because my project is x64 only. Apparently Visual Studio, being a 32bit application, cannot load any forms or controls compiled to 64bit in the designer. It makes total sense, but the error gives you no indication that is the problem.
See the answer to Visual studio designer in x64 doesn't work.
The workaround is to change your project to Any CPU when designing, then back when building.
Solution 4
Maybe the error occurs due to your constructor code. Place InitializeComponent();
at the beginning of the constructor like this:
public FormularioGeneral()
{
InitializeComponent();
ConfigurarUI();
AccionesConstructor();
PostInicializacionComponentes();
EstablecerIcono();
InicializarLocalizacionFormulario();
}
Explanation:
The variables are initialized in that method.
Solution 5
I had the same problem and cleaning and rebuilding did not work for me.
In my case the problem was caused by the Visual Studio designer loading referenced DLLs from the GAC instead of loading them from the <HintPath> directory specified in the .csproj file. The DLLs in the GAC did not have the same version as the locally stored DLLs.
When I updated the DLLs in the GAC to have the same version everything worked OK again.
Related videos on Youtube
Kitinz
Updated on July 09, 2022Comments
-
Kitinz almost 2 years
I have a question related to the error on the title. Im working with c# and Visual Studio 2010.
I have a form declared as "public class FormularioGeneral : Form", which is the base for the rest of the forms in my application. When i try to access the Designer View i get this error several times, as you can see in the image:
All the errors references lines inside the InitializeComponent method, where the value is assigned to a property like this one:
[...] this.PanelMargenIzquierdoCapaBase.BackColor = m_ColorCapaBase; [...]
But all the variables are declared in the same class as read-only properties and all of them are assigned inside a method which is called in the constructor.
Declaration of properties:
protected Color m_VariableName; public Color VariableName { get { return m_VariableName; } set { } }
Constructor code:
public FormularioGeneral() { ConfigurarUI(); AccionesConstructor(); InitializeComponent(); PostInicializacionComponentes(); EstablecerIcono(); InicializarLocalizacionFormulario(); }
ConfigurarUI method:
public virtual void ConfigurarUI() { [...] m_AltoBordeSuperiorCapaBase = 30; m_AltoBordeInferiorCapaBase = 7; m_AnchoBordesLateralesCapaBase = 7; m_ColorCapaBase = Color.FromArgb(50, 100, 150); m_ColorTextoCapaBase = Color.White; m_ColorTextoBotonAplicacion = Color.Black; m_FuenteTextoIzquierdoCapaBase = new System.Drawing.Font("Verdana", 11.0F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); m_FuenteTextoCentroCapaBase = new System.Drawing.Font("Verdana", 14.0F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); [...] }
So, as far as i know, all the variable which are giving the errors are correctly declared and have a value assigned before the InitilizeComponent function is called.
Im stuck at this point and dont know what to do to solve the problem. Hope some of you can help me with this issue.
-
Moonlight over 12 yearsbe sure you call your constructor before using your code. public Form1() { FormularioGeneral(); }
-
Kitinz over 12 yearsHello Bruno. The error is produced inside the InitializeComponent method, when i assign the m_* value to some property (for example, when i set the text color of a button --> this.Button1.ForeColor = m_TextButtonColor)
-
Christian about 10 yearsalso discussed here: stackoverflow.com/questions/1915183/…
-
Pedro77 almost 9 yearsAnd here also: stackoverflow.com/questions/12342760/…
-
Faisal Al-Harbi over 5 yearsI can't write an answer due to not being sure what's going on, and am trying to solve an probably different maybe unrelated problem, but experimenting with my main Solution and an small hackjob custom control project, I find that I must chose "Any CPU" not x86 or x64 to avoid getting these 'variable undeclared' errors.
-
-
Kitinz over 12 yearsI already tried it, but this doesnt solve the problem. Anyway, the variables are declared without value. A value is assigned inside the "ConfigurarUI" method (So this method should be executed before using the variable). Finally, in the InitializeComponent the variables are used.
-
Fischermaen over 12 yearsAs far as I understand you correctly, have you changed the code in the
InitializeComponent
to use your variables? -
Kitinz over 12 yearsI dont think the assignments were "hardcoded" in the InitializeComponent since it is auto-generated each time you compile. I assume that the values were assigned through the Designer, but cant assure it since other guy started this project and im the "sucessor" :P
-
Fischermaen over 12 yearsPoor "sucessor". The designer doesn't generate member variables with an "m_" at the beginning. So this was "handmade" and it will make trouble. There is a comment to the "InitializeComponents" method, stating that one should never edit this. Try to solve problem in a different way, maybe by setting the values after InitializeComponents.
-
Kitinz over 12 yearsThanks for the answer. I tried to do as you say, initializing the variables in the declaration, but doesnt solve the problem :P I also read about the virtual methods, and copied all the code in the virtual method inside the constructor, to not need to make the call, but even so, the problem persist :P
-
Kitinz over 12 yearsI know the m_* variables are created manually, but i think you are right. The errors point the assingments made inside the InitializeComponent like "this.Button1.ForeColor = m_TextButtonColor", which should be made through the designer and seems like it was hardcoded. Im going to try to change those hardcoded assingments to this one: "this.Button1.ForeColor = Color.Black" which is the value stored in the m_TextButtonColor :P
-
Pedro77 almost 9 yearsI ways do that, and it works, but I wonder why this error is happening and how can I avoid that. Or this is simple a VS bug?
-
Tuan Do almost 8 yearsThis is exactly my problem, I have created some UserControl, and for some reason, the process to generate Design of the Form always complains. After I moved anything seems suspicious out of InitializeComponent, it works fine. Thanks
-
Lara over 7 yearsIn case someone finds their way here, the problem might arise from the constructor of the offending control being declared anything other than
public
. -
ephraim about 6 yearsToo bad even in visual studio 2017 they still don't tell the real reason... 2 hours on the web, after trying manually change the GUI... all that until I came to here...
-
Sam over 5 yearsThanks, worked for me. Can you give any explanation why it worked this way?
-
Scott over 5 yearsAs a clarification, the designer doesn't execute your code, it reads your code, and specifically, it only reads the
InitializeComponent()
method. Any initialization code you have outside of that is beyond its understanding. -
tkha007 over 4 yearsAmazing! This also worked for me. Can someone please explain why changing to
resources
works?