Reputation: 33
i have start to code C# and i have a little problem with my application, it's show me the error code:
CS1674 WPF C# 'IEnumerator': type used in a using statement must be implicitly convertible to 'System.IDisposable' or implement a suitable 'Dispose' method.
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 !!
Upvotes: 1
Views: 575
Reputation: 36
As some have pointed out, IEnumerable doesn't implement IDisposable, and it would probably be a good idea to use foreach instead.
Information on foreach: https://learn.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.
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
}
}
Upvotes: 0
Reputation: 126
Yeah this whole thing should be re-written entirely and I would certainly recommend using a foreach
loop here as well.
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 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
.
You may even want to surround the last statement in a try-catch
loop similar to this:
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.
Upvotes: 0
Reputation: 399
IEnumerator doesn't inherit IDispoable so it cannot be used with using statement.
Upvotes: 0
Reputation: 46909
Is because the non generic IEnumerator
does not implement IDisposable
. Just remove the using
.
var enumerator = this.listViewBoxes.Items.GetEnumerator();
Upvotes: 2