简体   繁体   English

如何解决 IEnumerator GetEnumerator C# 的问题

[英]How To Fix The Problem with IEnumerator GetEnumerator C#

i have start to code C# and i have a little problem with my application, it's show me the error code:我已经开始编写 C# 代码,但我的应用程序有一个小问题,它显示了错误代码:

CS1674 WPF C# 'IEnumerator': type used in a using statement must be implicitly convertible to 'System.IDisposable' or implement a suitable 'Dispose' method. CS1674 WPF C#“IEnumerator”:using 语句中使用的类型必须可隐式转换为“System.IDisposable”或实现合适的“Dispose”方法。

I don't know how to fix that.我不知道如何解决这个问题。

My code is:我的代码是:

    using (IEnumerator enumerator = this.listViewBoxes.Items.GetEnumerator())
                        {
                            while (enumerator.MoveNext())
                            {
                                object obj = enumerator.Current;
                                ListViewItem listViewItem = (ListViewItem)obj;
                                int num5 = 0;
                                int num6 = 0;
                                try
                                {
                                    if (listViewItem.Tag.ToString() == "HEAD")
                                    {
                                        num5 = this.displayBox.Width / 2;
                                        num6 = num + int.Parse(this.offsetHead.Text) * 5;
                                    }
                                    else if (listViewItem.Tag.ToString() == "BODY")
                                    {
                                        num5 = this.displayBox.Width / 2;
                                        num6 = num + int.Parse(this.offsetBody.Text) * 5;
                                    }
                                    else if (listViewItem.Tag.ToString() == "ARM0")
                                    {
                                        num5 = this.displayBox.Width / 2 - 25;
                                        num6 = num2 + int.Parse(this.offsetArms.Text) * 5;
                                    }
                                    else if (listViewItem.Tag.ToString() == "ARM1")
                                    {
                                        num5 = this.displayBox.Width / 2 + 25;
                                        num6 = num2 + int.Parse(this.offsetArms.Text) * 5;
                                    }
                                    else if (listViewItem.Tag.ToString() == "LEG0")
                                    {
                                        num5 = this.displayBox.Width / 2 - 10;
                                        num6 = num3 + int.Parse(this.offsetLegs.Text) * 5;
                                    }
                                    else if (listViewItem.Tag.ToString() == "LEG1")
                                    {
                                        num5 = this.displayBox.Width / 2 + 10;
                                        num6 = num3 + int.Parse(this.offsetLegs.Text) * 5;
                                    }

If you need the full code, i can give you on a pastebin or something else thanks !!如果你需要完整的代码,我可以给你一个粘贴箱或其他东西,谢谢!!

Is because the non generic IEnumerator does not implement IDisposable .是因为非通用IEnumerator没有实现IDisposable Just remove the using .只需删除using

var enumerator = this.listViewBoxes.Items.GetEnumerator();

IEnumerator 不继承 IDispoable,因此它不能与 using 语句一起使用。

Yeah this whole thing should be re-written entirely and I would certainly recommend using a foreach loop here as well.是的,这整个事情应该完全重写,我当然也建议在这里使用foreach循环。

Also I see nothing calling on the Dispose() method from IDisposable but if you need to add it from what I can see here at least I would say try creating a local variable and casting your enumerator object to IDisposable like so:此外,我看不到IDisposable调用Dispose()方法的任何内容,但是如果您需要从我在这里看到的内容中添加它,至少我会说尝试创建一个局部变量并将您的enumerator对象转换为IDisposable如下所示:

IDisposable disposable = (IDisposable)enumerator;

//example of calling Dispose()

disposable?.Dispose();

Note the ?.注意?. operator.操作员。 It is important to check for null here and in this example we're using null propagation since there is a chance that enumerator object instance does not implement IDisposable .在这里检查null很重要,在这个例子中我们使用 null 传播,因为枚举器对象实例有可能没有实现IDisposable

You may even want to surround the last statement in a try-catch loop similar to this:您甚至可能希望将最后一条语句包含在类似于以下内容的try-catch循环中:

try
{
    disposable?.Dispose();
}

catch (NullreferenceException ex)
{
   // insert logic to handle null references in here.
}

If that doesn't help I'll need a little more elaboration.如果这没有帮助,我将需要更多详细说明。

As some have pointed out, IEnumerable doesn't implement IDisposable, and it would probably be a good idea to use foreach instead.正如一些人指出的那样,IEnumerable 没有实现 IDisposable,而使用 foreach 可能是个好主意。

Information on foreach: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/foreach-in foreach 信息: https : //docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/foreach-in

I, personally would also use a switch-statement instead of your if-else chain.我个人也会使用 switch 语句而不是 if-else 链。

So with those changes the code would look something like this:因此,通过这些更改,代码将如下所示:

foreach (ListViewItem listViewItem in this.listViewBoxes.Items)
{
    int num5 = 0;
    int num6 = 0;
    try
    {
        switch (listViewItem.Tag.ToString())
        {
            case "HEAD":
                num5 = this.displayBox.Width / 2;
                num6 = num + int.Parse(this.offsetHead.Text) * 5;
                break;    
            case "BODY":
                num5 = this.displayBox.Width / 2;
                num6 = num + int.Parse(this.offsetBody.Text) * 5;
                break;    
            case "ARM0":
                num5 = this.displayBox.Width / 2 - 25;
                num6 = num2 + int.Parse(this.offsetArms.Text) * 5;
                break;    
            case "ARM1":
                num5 = this.displayBox.Width / 2 + 25;
                num6 = num2 + int.Parse(this.offsetArms.Text) * 5;
                break;    
            case "LEG0":
                num5 = this.displayBox.Width / 2 - 10;
                num6 = num3 + int.Parse(this.offsetLegs.Text) * 5;
                break;    
            case "LEG1":
                num5 = this.displayBox.Width / 2 + 10;
                num6 = num3 + int.Parse(this.offsetLegs.Text) * 5;
                break;
        }
    }
    catch (Exception e)
    {
        //Exception handling
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM