Problem with a private variable

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I created a C# application which is using many private variables.
I'm working on VS .NET 2003 and have the following error (whereas the
variable is used in the method of the class !) :

"warning CS0169: The private field 'Canal.ConnectForm.statusImg' is never
used !

I don't understand because I'm using the variable at 2 different places in
the class :
- In the constructor, I'm using the following code : "statusImg = false;"
- In a private method of the class, I'm using the following code :
"statusImg = true;"

If someone has an idea of the problem ?

Thanks in advance.
-
 
Gandalf,

Even though you might be using the name of the field, you might not be
accessing it. If you have a local variable or a parameter which has the
same name, then that takes precedence over the field, in which case you have
to say:

this.statusImg = false;

or

this.statusImg = true;

Hope this helps.
 
It's a terminology problem.

You're _not_ using the variable. You're only setting it. If you do
nothing but assign values to the variable, and never test its value or
do anything with the value, it can't affect the execution of your
program. If I write a method like this:

public void MyMethod()
{
int x = 6;
x = 7;
x = 100;
x = 1000;
}

I can remove all of those lines and it won't affect my method at all.
Since I never _do_ anything with x (I never "use" it), it doesn't
affect the execution of my method.
 
| Hi,
|
| I created a C# application which is using many private variables.
| I'm working on VS .NET 2003 and have the following error (whereas the
| variable is used in the method of the class !) :
|
| "warning CS0169: The private field 'Canal.ConnectForm.statusImg' is never
| used !
|
| I don't understand because I'm using the variable at 2 different places in
| the class :
| - In the constructor, I'm using the following code : "statusImg = false;"
| - In a private method of the class, I'm using the following code :
| "statusImg = true;"
|
| If someone has an idea of the problem ?
|
| Thanks in advance.
| -
|

You are assigning the value true and false to it, but you never use the
variable in your code, that means your assignments are bogus or you may have
forgotten something like...
if(statusImg)
//do something....

Willy.
 
"warning CS0169: The private field 'Canal.ConnectForm.statusImg' is never
used !

I don't understand because I'm using the variable at 2 different places in
the class :
- In the constructor, I'm using the following code : "statusImg = false;"
- In a private method of the class, I'm using the following code :
"statusImg = true;"

If someone has an idea of the problem ?

If those are the only two places the variable is used, you're never
reading its value and therefore it's pretty useless.


Mattias
 
Hi Gandalf,
although you are setting the value it is never being read anywhere, that
is what the warning really means, for example if you took out that variable
completely from your code it would not make any difference because you never
use it, you are not assigning its value to other variables or using it in a
conditional etc.

It is really just telling you that you don't need this variable.

Hope that helps
Mark Dawson
http://www.markdawson.org
 
Nick is right and everyone else is wrong (sort of)

Assuming that you are using VS2005 [or that VS.NET uses the same
diagnostics] then you can get two different diagnostics:

"... is never used" - This means that you must have hidden the field
somehow - whether by parameter, local variable or conditional compilation.

"... is assigned but its value is never used" - This is the diagnostic for
the error that the other guys describe.

If this hasn't cleared it up then post the code.

Nicholas Paldino said:
Gandalf,

Even though you might be using the name of the field, you might not be
accessing it. If you have a local variable or a parameter which has the
same name, then that takes precedence over the field, in which case you
have to say:

this.statusImg = false;

or

this.statusImg = true;

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Gandalf said:
Hi,

I created a C# application which is using many private variables.
I'm working on VS .NET 2003 and have the following error (whereas the
variable is used in the method of the class !) :

"warning CS0169: The private field 'Canal.ConnectForm.statusImg' is never
used !

I don't understand because I'm using the variable at 2 different places
in
the class :
- In the constructor, I'm using the following code : "statusImg = false;"
- In a private method of the class, I'm using the following code :
"statusImg = true;"

If someone has an idea of the problem ?

Thanks in advance.
-
 
Nick said:
Nick is right and everyone else is wrong (sort of)

Assuming that you are using VS2005 [or that VS.NET uses the same
diagnostics] then you can get two different diagnostics:

"... is never used" - This means that you must have hidden the field
somehow - whether by parameter, local variable or conditional compilation.

"... is assigned but its value is never used" - This is the diagnostic for
the error that the other guys describe.

Unfortunately, the warning has changed between 1.1 and 2.0. Try
compiling this:

public class Test
{
int i;

Test()
{
i = 10;
}

static void Main()
{
}
}

Under 1.1:
"warning CS0169: The private field 'Test.i' is never used"

Under 2.0:
"warning CS0414: The private field 'Test.i' is assigned but its value
is never used"

CS0169 is used under 1.1 for both types of "non-use"; it's only used in
2.0 if the variable is never accessed either way.

(There's a different warning if it's read but not written.)

Jon
 
Jon Skeet said:
Nick said:
Nick is right and everyone else is wrong (sort of)

Assuming that you are using VS2005 [or that VS.NET uses the same
diagnostics] then you can get two different diagnostics:

"... is never used" - This means that you must have hidden the field
somehow - whether by parameter, local variable or conditional
compilation.

"... is assigned but its value is never used" - This is the diagnostic
for
the error that the other guys describe.

Unfortunately, the warning has changed between 1.1 and 2.0. Try
compiling this:

public class Test
{
int i;

Test()
{
i = 10;
}

static void Main()
{
}
}

Under 1.1:
"warning CS0169: The private field 'Test.i' is never used"

Under 2.0:
"warning CS0414: The private field 'Test.i' is assigned but its value
is never used"

CS0169 is used under 1.1 for both types of "non-use"; it's only used in
2.0 if the variable is never accessed either way.

(There's a different warning if it's read but not written.)

Jon

As I said - Assuming....

It's nice to see that they've improved the diagnostics.

Now if only the default for "Treat warnings as errors" was "All"
 
| Nick Hounsome wrote:
| > Nick is right and everyone else is wrong (sort of)
| >
| > Assuming that you are using VS2005 [or that VS.NET uses the same
| > diagnostics] then you can get two different diagnostics:
| >
| > "... is never used" - This means that you must have hidden the field
| > somehow - whether by parameter, local variable or conditional
compilation.
| >
| > "... is assigned but its value is never used" - This is the diagnostic
for
| > the error that the other guys describe.
|
| Unfortunately, the warning has changed between 1.1 and 2.0. Try
| compiling this:
|
| public class Test
| {
| int i;
|
| Test()
| {
| i = 10;
| }
|
| static void Main()
| {
| }
| }
|
| Under 1.1:
| "warning CS0169: The private field 'Test.i' is never used"
|
| Under 2.0:
| "warning CS0414: The private field 'Test.i' is assigned but its value
| is never used"
|
| CS0169 is used under 1.1 for both types of "non-use"; it's only used in
| 2.0 if the variable is never accessed either way.
|
| (There's a different warning if it's read but not written.)
|
| Jon
|

Yep, and note that the rhs must be a const value expression.

i= 10-10; //CS0414

i = Foo(); //OK no warning
i = i+i; //OK no warning
i = i + 0; //OK no warning

However, this:
i = i;
will return CS1717

Willy.
 

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

Back
Top