私有变量的问题 [英] Problem with a private variable

查看:71
本文介绍了私有变量的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我创建了一个使用许多私有变量的C#应用​​程序。

我正在使用VS .NET 2003并拥有跟随错误(而

变量用于类的方法!):


"警告CS0169:私有字段''Canal.ConnectForm .statusImg''永远不会使用



我不明白,因为我在

班级:

- 在构造函数中,我使用以下代码:" statusImg = false;"

- 私有我正在使用以下代码的方法:

" statusImg = true;"


如果有人知道这个问题?


提前致谢。

-

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,


即使你可能正在使用该字段的名称,你可能不会是

accessi它。如果你有一个局部变量或一个具有

同名的参数,那么它优先于该字段,在这种情况下你需要

来说:


this.statusImg = false;





this.statusImg = true;


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- < a href =mailto:mv*@spam.guard.caspershouse.com> mv*@spam.guard.caspershouse.com


" Gandalf" <嘎***** @ discussions.microsoft.com>在消息中写道

news:37 ********************************** @ microsof t.com ...
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...


我创建了一个使用许多私有变量的C#应用​​程序。
我正在研究VS .NET 2003并且有以下错误(而
变量用于类的方法!):

"警告CS0169:私有字段''Canal.ConnectForm.statusImg''是从来没有使用过!

我不明白,因为我在班级的两个不同的地方使用变量:
- 在构造函数中,我正在使用以下代码:statusImg = false;
- 在类的私有方法中,我使用以下代码:
statusImg = true; ;

如果有人知道这个问题?

提前致谢。
-
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.
-


这是术语问题。


你没有使用变量BLE。你只是设置它。如果你只是为变量分配值,而且从不测试它的值或

对值进行任何操作,那么它就不会影响你的执行

计划。如果我写这样的方法:


public void MyMethod()

{

int x = 6;

x = 7;

x = 100;

x = 1000;

}


我可以删除所有这些行,它根本不会影响我的方法。

因为我从来没有_do_任何x(我从不使用它),它不会

会影响我的方法的执行。

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.




Gandalf <嘎***** @ discussions.microsoft.com>在消息中写道

news:37 ********************************** @ microsof t.com ...

|

|

|我创建了一个使用许多私有变量的C#应用​​程序。

|我正在研究VS .NET 2003并且出现以下错误(而在类的方法中使用了

|变量!):

|

| 警告CS0169:私有字段''Canal.ConnectForm.statusImg''永远不会是

|二手!

|

|我不明白,因为我在两个不同的地方使用变量

|上课:

| - 在构造函数中,我使用以下代码:" statusImg = false;"

| - 在课堂的私人方法中,我使用以下代码:

| statusImg = true;"

|

|如果有人知道这个问题?

|

|在此先感谢。

| -

|


你给它赋值true和false,但你从不在代码中使用

变量,这意味着你的任务是假的,或者你可能已经忘记了类似的事情......

if(statusImg)

//做点什么。 ...


威利。


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


这篇关于私有变量的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆