“使用可能未分配的字段”编译错误 [英] "Use of possibly unassigned field" compile error

查看:74
本文介绍了“使用可能未分配的字段”编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们公司已经开发了一个C#程序已有一段时间了,我们

没有任何问题,但就在昨晚出现了一些问题

,让我和其他人都难过。


我有一个包含几种不同类型数据的结构。这个结构

在程序中使用。现在,当我编译时,我得到6个错误,全部是

他们使用可能未分配的字段''awayTime''"或使用可能

未分配字段''intlTime''。这是正确的,这些

字段可能没有分配,但我不在乎。它在一个结构体内,并且

无论如何,结构内部的所有内容都被分配了它的默认值。


这很难解释,但如果其他人都遇到过这个问题并且确定了这个问题,我很想知道怎么做。谢谢。


Chris LaJoie

Our company has been developing a program in C# for some time now, and we
haven''t had any problems with it, but just last night something cropped up
that has me, and everyone else, stumped.

I have a struct that contains several different types of data. This struct
is used throuout the program. Now, when I compile, I get 6 errors, all of
them "Use of possibly unassigned field ''awayTime''" or "Use of possibly
unassigned field ''intlTime''". It is correct, it is possible that these
fields aren''t assigned, but I don''t care. It''s inside of a struct, and
everything inside of a struct gets assigned its default value anyway.

This is difficult to explain, but if anyone else has had this problem and
fixed it, i''d love to know how. Thanks.

Chris LaJoie

推荐答案

Chris LaJoie< ch *** @ etriptrader.com>写道:
Chris LaJoie <ch***@etriptrader.com> wrote:
我们公司已经开发了一个C#程序已有一段时间了,我们没有遇到任何问题,但就在昨晚出现了一些问题让我和其他所有人都难过。

我有一个包含几种不同类型数据的结构。这个结构
在程序中使用。现在,当我编译时,我得到6个错误,所有这些都是使用可能未分配的字段'awayTime''"或使用可能
未分配的字段''intlTime''。这是正确的,这些
字段可能没有分配,但我不在乎。它在一个结构体内部,并且结构中的所有内容都被赋予其默认值。


根据C#规范,本地变量*不具有默认值。

这就是你遇到的问题。

这很难解释,但如果有其他人遇到这个问题并且已经修好了,我很想知道怎么做。谢谢。
Our company has been developing a program in C# for some time now, and we
haven''t had any problems with it, but just last night something cropped up
that has me, and everyone else, stumped.

I have a struct that contains several different types of data. This struct
is used throuout the program. Now, when I compile, I get 6 errors, all of
them "Use of possibly unassigned field ''awayTime''" or "Use of possibly
unassigned field ''intlTime''". It is correct, it is possible that these
fields aren''t assigned, but I don''t care. It''s inside of a struct, and
everything inside of a struct gets assigned its default value anyway.
Local variables *don''t* have default values, according to the C# spec.
That''s what you''re running into.
This is difficult to explain, but if anyone else has had this problem and
fixed it, i''d love to know how. Thanks.




解决问题的最简单方法是更改​​:


MyStruct foo;

to


MyStruct foo = new MyStruct();


代码。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复该群组,请不要给我发邮件



The easiest way to fix it is to change:

MyStruct foo;

to

MyStruct foo = new MyStruct();

in your code.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


struct var正在foreach循环中使用。只是为了踢(一个

预感)我将foreach改为for,循环遍历数组。这个

解决了所有问题。


简单地使用bla = new StructName()在这种情况下不起作用

(因为价值被读取,而不是分配给。)


Chris LaJoie


" Jon Skeet" < SK *** @ pobox.com>在消息中写道

新闻:MP ************************ @ news.microsoft.com ...
The struct var was being used inside of a foreach loop. Just for kicks (a
hunch) I changed the foreach to a for, looping through the array. This
solved everything.

Simply using bla = new StructName() would not have worked in this case
(because the values were being read from, not assigned to).

Chris LaJoie

"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
Chris LaJoie< ch *** @ etriptrader.com>写道:
Chris LaJoie <ch***@etriptrader.com> wrote:
我们公司已经开发了一个C#程序已经有一段时间了,
我们没有遇到任何问题,但就在昨晚,有些东西被裁掉了
这让我和其他所有人都难过了。

我有一个包含几种不同类型数据的结构。这个
结构在程序中使用。现在,当我编译时,我得到6个错误,所有
他们使用可能未分配的字段'awayTime''"或使用可能
未分配的字段''intlTime''。这是正确的,这些
字段可能没有分配,但我不在乎。它在一个结构体内部,并且结构中的所有内容都被赋予其默认值。
Our company has been developing a program in C# for some time now, and we haven''t had any problems with it, but just last night something cropped up that has me, and everyone else, stumped.

I have a struct that contains several different types of data. This struct is used throuout the program. Now, when I compile, I get 6 errors, all of them "Use of possibly unassigned field ''awayTime''" or "Use of possibly
unassigned field ''intlTime''". It is correct, it is possible that these
fields aren''t assigned, but I don''t care. It''s inside of a struct, and
everything inside of a struct gets assigned its default value anyway.



局部变量*不具有默认值,根据C#规范。
这就是你遇到的问题。



Local variables *don''t* have default values, according to the C# spec.
That''s what you''re running into.

这很难解释,但如果有其他人有这个问题
并修好了,我很想知道如何。谢谢。
This is difficult to explain, but if anyone else has had this problem and fixed it, i''d love to know how. Thanks.



解决问题的最简单方法是改变:

MyStruct foo;



你的代码中有MyStruct foo = new MyStruct();



- Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet <如果回复小组,请不要给我发邮件



The easiest way to fix it is to change:

MyStruct foo;

to

MyStruct foo = new MyStruct();

in your code.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too



会在我今天晚些时候回家后发布一些示例代码。敬请期待!


Chris LaJoie


" Jon Skeet" < SK *** @ pobox.com>在消息中写道

新闻:MP ************************ @ news.microsoft.com ...
will post some sample code when I get home later today. Stay tuned!

Chris LaJoie

"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
Chris LaJoie< ch *** @ etriptrader.com>写道:
Chris LaJoie <ch***@etriptrader.com> wrote:
struct var正在foreach循环中使用。只是为了踢b
(预感)我将foreach改为for,循环遍历数组。这解决了一切。
The struct var was being used inside of a foreach loop. Just for kicks (a hunch) I changed the foreach to a for, looping through the array. This
solved everything.



有意思 - 你能发布一些示例代码吗? (刚刚显示
发生了什么。)



Interesting - could you post some sample code? (Just enough to show
what''s going on.)

简单地使用bla = new StructName()在这种情况下不会起作用
(因为这些价值是从中读取的,而不是分配给。)
Simply using bla = new StructName() would not have worked in this case
(because the values were being read from, not assigned to).



我不确定我是否跟着你,说实话......如果你假设的话/>声明给你默认值,然后在声明点给它指定默认的
值应该给你*完全*
相同的行为,但没有编译错误。

-
Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet
如果回复小组,请不要给我发邮件



I''m not sure I follow you, to be honest... if you''re assuming that the
declaration gave you the default values, then assigning the default
value to it at the point of declaration should give you *exactly* the
same behaviour, but without the compile error.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too



这篇关于“使用可能未分配的字段”编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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