473,554 Members | 3,232 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.ConnectF orm.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 4896
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.co m

"Gandalf" <Ga*****@discus sions.microsoft .com> wrote in message
news:37******** *************** ***********@mic rosoft.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.ConnectF orm.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*****@discus sions.microsoft .com> wrote in message
news:37******** *************** ***********@mic rosoft.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.ConnectF orm.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.ConnectF orm.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.ConnectF orm.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.c om> wrote in
message news:ex******** ******@TK2MSFTN GP10.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.co m

"Gandalf" <Ga*****@discus sions.microsoft .com> wrote in message
news:37******** *************** ***********@mic rosoft.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.ConnectF orm.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.co m> wrote in message
news:11******** **************@ g47g2000cwa.goo glegroups.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.co m> wrote in message
news:11******** **************@ g47g2000cwa.goo glegroups.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
6262
by: Nick Whitelegg | last post by:
Hello, I'm having an odd problem with combining an authentication session variable with header() redirection. Basically I have an authentication script which checks a username/password. If the login is correct a session variable containing the username is set and the user is redirected to the (restricted access) main menu page. The...
2
4024
by: Vinay Aggarwal | last post by:
I have been thinking about the lazy initialization and double checked locking problem. This problem is explain in detail here http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html I am not fully convinced that this problem cannot be solved. I am going to propose a solution here. For the sake of discussion I will post my...
15
3338
by: Alfonso Morra | last post by:
Hi, I have some code from an example, that I want to retrofit into my project. The code from the example has the following line: SharedAppenderPtr myAppender( new RollingFileAppender("MyAppender")) I want to move this line of code to a constructor in a wrapper class, so that I can determine the appropriate Appender name during the...
1
4011
by: Rhy Mednick | last post by:
I'm creating a custom control (inherited from UserControl) that is displayed by other controls on the form. I would like for the control to disappear when the user clicks outside my control the same way a menu does. To do this my control needs to get notified when the user tried to click off of it. The Leave and LostFocus events of the...
1
4735
by: Andrew | last post by:
Hey all, Working on revamping our Intranet here and making use of the LDPA, Active Directory, Directory Services, etc. that .Net provides. I am still fairly new on this subject, so the problem I have run into I am not sure how to fix, and really not sure what is causing it. Here's what is going on (test server - Windows 2003 Server): I...
8
1788
by: Nacho | last post by:
Hello people I have the following problem I have a private area in my site. The user enters the username and password, then clicks "enter" and the session is created and also a session variable is created and set to true. Every page of the private area, checks whether this session variable is set to true and then if so, it gives you...
3
4188
by: christine0207 | last post by:
hi Can anyone help to debug this problem? the error msg is run-time error '91': object variable or with block variable not set. here is my source code: Option Explicit
7
4706
by: garyusenet | last post by:
This is the first time i've worked with openfile dialog. I'm getting a couple of errors with my very basic code. Can someone point out the errors in what i've done please. ========================================== using System; using System.Collections.Generic; using System.ComponentModel; using System.Data;
5
1512
by: tshad | last post by:
In VS 2003, I am setting up an abstract class that is setting up classes for each datatype of VB.Net (as well as C#). I am trying to set it up so that most of the work is done in the Abstract Class. It seems to work pretty well. I have the actual data memory variables stored as an object as each class will use it as a different type of...
2
2850
by: slapsh0t11 | last post by:
So, I've been working on this Game of Life (http://www.bitstorm.org/gameoflife/) project, and all the code has been written. However, it will not run. First, I will post the error message and the fragment of code I think to be the cause of the problem. Then, I will post the code to all 3 of my java files in their entirety should you wish to...
0
7583
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7507
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7783
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7542
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6127
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5424
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5143
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.