C# compiler gives wrong "variable undeclared or unasigned' error

N

~~~ .NET Ed ~~~

It is not the first time I see this happen. I am using VS.NET 2003 with .NET
Framework 1.1. In this particular situation I have a custom user control in
a windows form. There is a member variable declared with the fully qualified
class name (some.namespace.mycontrolclass). Then in the
InitializeComponent() it is instantiated and there is a block of code lines
that set the various properties of the control class, then further down the
usual this.Controls.Add(this.mycontrol).

Well, when I compile I get an error indicating "The variable
'mycontrolclass' is either undeclared or was never assigned." That seems
crazy considering it is:
a) declared in the same class with the full name
b) instantiated at the beginning of the InitializeComponent().
c) then further on it is initialized in the same method, and
d) even further down the same method (InitializeComponent) it is used in the
Add method (where the 'error' is pointed).

This has been declared *and* initialized. Could it be that the C# compiler
is somehow losing a bolt or two because the referenced assembly does not
have a strong name? anyway, I don't see why it should just throw such
meaningless and unjust error.
 
J

Jon Skeet [C# MVP]

~~~ .NET Ed ~~~ said:
It is not the first time I see this happen. I am using VS.NET 2003 with .NET
Framework 1.1. In this particular situation I have a custom user control in
a windows form. There is a member variable declared with the fully qualified
class name (some.namespace.mycontrolclass). Then in the
InitializeComponent() it is instantiated and there is a block of code lines
that set the various properties of the control class, then further down the
usual this.Controls.Add(this.mycontrol).

Well, when I compile I get an error indicating "The variable
'mycontrolclass' is either undeclared or was never assigned." That seems
crazy considering it is:
a) declared in the same class with the full name
b) instantiated at the beginning of the InitializeComponent().
c) then further on it is initialized in the same method, and
d) even further down the same method (InitializeComponent) it is used in the
Add method (where the 'error' is pointed).

This has been declared *and* initialized. Could it be that the C# compiler
is somehow losing a bolt or two because the referenced assembly does not
have a strong name? anyway, I don't see why it should just throw such
meaningless and unjust error.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
N

Nicholas Paldino [.NET/C# MVP]

Ed,

Can you post the code for the control? While it certainly is possible
that the compiler is doing something wrong, I tend not to think that
initially (I've been wrong when going head to head with the compiler too
often).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top