471,319 Members | 1,990 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

Problem with a private variable

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.
-

Feb 1 '06 #1
10 4752
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]
- mv*@spam.guard.caspershouse.com

"Gandalf" <Ga*****@discussions.microsoft.com> wrote in message
news:37**********************************@microsof t.com...
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.
-

Feb 1 '06 #2
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.

Feb 1 '06 #3

"Gandalf" <Ga*****@discussions.microsoft.com> wrote in message
news:37**********************************@microsof t.com...
| 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.

Feb 1 '06 #4
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


"Gandalf" wrote:
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.
-

Feb 1 '06 #5
>
"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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Feb 1 '06 #6
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:ex**************@TK2MSFTNGP10.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Gandalf" <Ga*****@discussions.microsoft.com> wrote in message
news:37**********************************@microsof t.com...
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.
-


Feb 2 '06 #7
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

Feb 2 '06 #8

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
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


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"
Feb 2 '06 #9

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
| 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.


Feb 2 '06 #10
"Nick Hounsome" <nh***@nickhounsome.me.uk> wrote in message
news:%3*********************@fe2.news.blueyonder.c o.uk...
As I said - Assuming....


And as the OP said, "I'm working on VS .NET 2003"

--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
Feb 3 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Nick Whitelegg | last post: by
1 post views Thread by Andrew | last post: by
8 posts views Thread by Nacho | last post: by
5 posts views Thread by tshad | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.