使用未分配的局部变量错误
我收到错误:
“使用未分配的局部变量'PostData'”
在方法中编译以下语句时 。我的目的是获取包含 XML SOAP 标头的“字符串”值,并将其转换为 XMLDictionaryWriter
对象。请参阅下面的代码:
Stream PostData;
byte[] buffer = Encoding.ASCII.GetBytes(x509.CreateX509SoapEnvelope());
PostData.Write(buffer, 0, buffer.Length); // error here
XmlDictionaryWriter xmlwriter = XmlDictionaryWriter.CreateTextWriter(PostData, Encoding.ASCII);
request.Headers.WriteHeaderContents(0,xmlwriter);
仅供参考,x509.CreateX509SoapEnvelope()
的输出是一个字符串,我测试了该部分并且它有效。我标记了上面的代码以显示错误发生的位置。需要有关错误的帮助以及如何修复它?
I am receiving an error:
"Use of unassigned local variable 'PostData'"
when compiling the following statements below within a method. My intent is take a "string" value containing an XML SOAP header and convert it to a XMLDictionaryWriter
object. See my code below:
Stream PostData;
byte[] buffer = Encoding.ASCII.GetBytes(x509.CreateX509SoapEnvelope());
PostData.Write(buffer, 0, buffer.Length); // error here
XmlDictionaryWriter xmlwriter = XmlDictionaryWriter.CreateTextWriter(PostData, Encoding.ASCII);
request.Headers.WriteHeaderContents(0,xmlwriter);
FYI, the output of x509.CreateX509SoapEnvelope()
is a string and I tested that part and it works. I marked the code above to show where the error occurs.Need assistance with the error and how to fix it ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您从未给
PostData
赋值。因此,它的默认值为null
并且编译器足够聪明,可以告诉您这是一件坏事(如果它允许您按原样编写代码,您将得到一个运行时NullReferenceException)。您需要实例化一个
Stream
类的实例(Stream
是抽象的,因此您需要一个具体实例)并将其分配给PostData
>。You never assigned a value to
PostData
. Thus, its default value isnull
and the compiler is smart enough to tell you this is a bad thing to do (if it permitted your code as is, you will get a runtimeNullReferenceException
). You need to instantiate an instance of a class that is aStream
(Stream
is abstract and so you need a concrete instance) and assign it toPostData
.您已声明
PostData
,但未初始化它。你需要:
至少。有关各种初始化程序的详细信息,请参阅 MSDN 文档 。
You have declared
PostData
, but not initialised it.You need to have:
at the bare minimum. See the MSDN documentation for more information on the various initialisers.
我想我和你追求的东西是一样的。我使用了 MemoryStream,它不需要文件来实例化。请参阅下面的参考:
MemoryStream
I think I was after the same thing you are. I used a MemoryStream which didn't need a file in order to instantiate. See reference below:
MemoryStream