使用未分配的局部变量错误

发布于 2024-11-30 20:11:20 字数 611 浏览 0 评论 0原文

我收到错误:

“使用未分配的局部变量'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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

背叛残局 2024-12-07 20:11:20

您从未给 PostData 赋值。因此,它的默认值为 null 并且编译器足够聪明,可以告诉您这是一件坏事(如果它允许您按原样编写代码,您将得到一个运行时 NullReferenceException)。您需要实例化一个 Stream 类的实例(Stream 是抽象的,因此您需要一个具体实例)并将其分配给 PostData >。

You never assigned a value to PostData. Thus, its default value is null 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 runtime NullReferenceException). You need to instantiate an instance of a class that is a Stream (Stream is abstract and so you need a concrete instance) and assign it to PostData.

桃酥萝莉 2024-12-07 20:11:20

您已声明 PostData,但未初始化它。

你需要:

Stream PostData = new StreamWriter(filename);

至少。有关各种初始化程序的详细信息,请参阅 MSDN 文档

You have declared PostData, but not initialised it.

You need to have:

Stream PostData = new StreamWriter(filename);

at the bare minimum. See the MSDN documentation for more information on the various initialisers.

旧故 2024-12-07 20:11:20

我想我和你追求的东西是一样的。我使用了 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文