gpt4 book ai didi

Default values for out parameters(输出参数的默认值)

转载 作者:bug小助手 更新时间:2023-10-28 11:28:46 26 4
gpt4 key购买 nike



Take this function:

使用此函数:


public void GetOutlineDateAndMediaClassification(string talksdatabase, int talknumber, out DateTime date, out bool hasMedia)
{
date = new DateTime(1900, 1, 1);
hasMedia = false;

try
{
XDocument doc = XDocument.Load(talksdatabase);
var ptInfo = doc
.Descendants("PublicTalk")
.Where(p => p.Attribute("Number").Value == talknumber.ToString()).First();

if (ptInfo != null)
{
date = DateTime.Parse(ptInfo.Attribute("IssueDate").Value).Date;
hasMedia = Convert.ToBoolean(ptInfo.Attribute("Media").Value);
}
}
catch (Exception ex)
{
SimpleLog.Log(ex);
}
}

It has two out parameters. I wanted the default values to be assigned inside the try block. But the system flags they are not initialized.

它有两个输出参数。我希望在try块内分配缺省值。但系统会将它们标记为未初始化。


enter image description here


So I moved them to the top of the function. But the DateTime constructor might through an exception, which is why I wanted to declare it in the try block.

所以我把它们移到了函数的顶部。但是DateTime构造函数可能会通过异常,这就是我想在try块中声明它的原因。


What's the right way to deal with this conundrum?

解决这个难题的正确方法是什么?


更多回答

"But the system flags they are not initialized." - which system? third party code analyzer?

“但系统标记它们未初始化。”-哪个系统?第三方代码分析器?

Just assign default and then again later

只需指定默认设置,然后再指定一次

As I've already said, date = new DateTime(1900, 1, 1); is never going to throw an exception. There is nothing to catch.

正如我已经说过的,date=new date time(1900,1,1);永远不会抛出异常。没什么好抓的。

If XDocument.Load(talksdatabase); throws exception then out parameters will be unassigned, that's why the compiler complains.

如果XDocument.Load(Talks Database);抛出异常,则输出参数将被取消赋值,这就是编译器抱怨的原因。

@AndrewTruckle Which value to date and hasMedia should be assigned when any exception inside the try block is thrown before your default values for date and hasMedia is assigned (like when XDocument.Load() is throwing an exception)?

@AndrewTruckle当try块内的任何异常在您的date和hasMedia的缺省值被分配之前抛出时(比如当XDocument.Load()抛出异常时),应该分配哪个to date和hasMedia的值?

优秀答案推荐

If the out parameters are set inside the try-block, you could have a situation where your code throws before you set the out parameters. In that case the code will continue executing in the catch block. This would be fine if you set them there as well, or throw an Exception. But since you don't, you will return from the function without having set the out parameters. That is not allowed.
Please see the comments I have added to your code for more clarity:

如果out参数是在try-块内部设置的,则可能会出现代码在设置out参数之前抛出的情况。在这种情况下,代码将继续在CATCH块中执行。如果您将它们也设置在那里,或者抛出一个异常,这将是很好的。但是由于您没有这样做,所以您将在没有设置输出参数的情况下从函数返回。这是不允许的。请参阅我添加到您的代码中的注释,以便更清楚地了解:


public void GetOutlineDateAndMediaClassification(string talksdatabase, int talknumber, out DateTime date, out bool hasMedia)
{
try
{
XDocument doc = XDocument.Load(talksdatabase); // (1) Assume Document.Load(talksdatabase) throws an exception. Note that you have not set date or hasMedia yet.
var ptInfo = doc
.Descendants("PublicTalk")
.Where(p => p.Attribute("Number").Value == talknumber.ToString()).First();

date = new DateTime(1900, 1, 1);
hasMedia = false;

if (ptInfo != null)
{
date = DateTime.Parse(ptInfo.Attribute("IssueDate").Value).Date;
hasMedia = Convert.ToBoolean(ptInfo.Attribute("Media").Value);
}
}
catch (Exception ex)
{
// (2) Your code will continue here after (1) without executing the rest of the try-block. date and hasMedia still have not been set.
SimpleLog.Log(ex);
}

// (3) There is no more code to execute but date and hasMedia are not set. This is invalid.
}

更多回答

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com