I have to maintain an old project in my company which is coded using C# and old .NET Framework (maybe 2.0?) many years ago. I loaded old source files to Visual Studio and it gives me an error: "CS1674 'IEnumerator': type used in a using statement must be implicitly convertible to 'System.IDisposable'" when I try to build it.
Here's a complete function which causes this error, most importantly this part: "using (IEnumerator enumerator = dataTable.Rows.GetEnumerator())", any advice how to get rid of it?
I tried to read other similar questions but I could not solve this with them. Is there maybe alternative way to code this, or could my programming environment be a cause for this?
public void CompileDynamicCode()
{
base.Session.trace_enter();
try
{
try
{
DataTable dataTable = this.ChimneyTable;
if (dataTable.Rows.Count > 0)
{
this.oChimneys = new Chimney[dataTable.Rows.Count];
int num = 0;
using (IEnumerator enumerator = dataTable.Rows.GetEnumerator())
{
while (enumerator.MoveNext())
{
DataRow dataRow = (DataRow)enumerator.Current;
bool entryXRight = false;
bool posXRight = false;
bool calcSteps = true;
bool calcCatwalk = true;
bool calcPlatform = true;
bool calcFlashing = true;
int connectCatwalkTo = -1;
if ((int)dataRow["entryXFromRight"] == 1)
{
entryXRight = true;
}
if ((int)dataRow["posXFromRight"] == 1)
{
posXRight = true;
}
if (dataTable.Columns.Contains("calcSteps") && (int)dataRow["calcSteps"] == 0)
{
calcSteps = false;
}
if (dataTable.Columns.Contains("calcCatwalk") && (int)dataRow["calcCatwalk"] == 0)
{
calcCatwalk = false;
}
if (dataTable.Columns.Contains("calcPlatform") && (int)dataRow["calcPlatform"] == 0)
{
calcPlatform = false;
}
if (dataTable.Columns.Contains("calcFlashing") && (int)dataRow["calcFlashing"] == 0)
{
calcFlashing = false;
}
if (dataTable.Columns.Contains("catwalkConnection"))
{
connectCatwalkTo = (int)dataRow["catwalkConnection"];
}
this.oChimneys[num] = new Chimney((int)dataRow["index"], (int)dataRow["shape_index"], (int)dataRow["entry_shape_index"], (int)dataRow["entryX"], entryXRight, (int)dataRow["posX"], posXRight, (int)dataRow["posY"], (int)dataRow["sizeX"], (int)dataRow["sizeY"], (int)dataRow["sizeZ"], (ChimneyAccessTypeEnum)dataRow["access_type"], calcSteps, calcCatwalk, calcPlatform, calcFlashing, connectCatwalkTo, this);
num++;
}
goto IL_247;
}
}
this.oChimneys = new Chimney[0];
IL_247:
dataTable = this.ShapeTable;
if (dataTable.Rows.Count > 0)
{
this.oShapes = new BasicShape[dataTable.Rows.Count];
int num2 = 0;
foreach (DataRow dataRow2 in dataTable.Rows)
{
this.oShapes[num2] = new BasicShape((TopRowModeEnum)dataRow2["TopRowMode"], (EaveRowModeEnum)dataRow2["EaveRowMode"], (ShapeBehaviorEnum)dataRow2["Behavior"], num2, this);
num2++;
}
}
dataTable = this.ConnectorTable;
if (dataTable.Rows.Count > 0)
{
this.oConnectors = new Connector[dataTable.Rows.Count];
int num3 = 0;
foreach (DataRow dataRow3 in dataTable.Rows)
{
BasicShape selectShape = null;
BasicShape selectShape2 = null;
try
{
selectShape = this.oShapes[(int)dataRow3["Shape1Index"]];
}
catch
{
}
try
{
selectShape2 = this.oShapes[(int)dataRow3["Shape2Index"]];
}
catch
{
}
this.oConnectors[num3] = new Connector((ConnectorBehaviorEnum)dataRow3["ConnectorBehavior"], selectShape, selectShape2, (ShapeConnectingLineEnum)dataRow3["Shape1Line"], (ShapeConnectingLineEnum)dataRow3["Shape2Line"], this, num3, (ConnectorStartEndMode)dataRow3["StartMode"], (ConnectorStartEndMode)dataRow3["StopMode"]);
num3++;
}
}
}
catch (Exception ex)
{
base.Session.HandleException(ex);
throw ex;
}
this.DynamicObject = this.CreateFromScript();
}
catch (Exception ex2)
{
base.Session.HandleException(ex2);
throw ex2;
}
finally
{
base.Session.trace_exit();
}
}
答案 0 :(得分:3)
Replace the call to GetEnumerator()
:
foreach (DataRow dataRow in dataTable.Rows)
{
bool entryXRight = false;
// ...
}
As a side note, that code needs a lot of love, like changing those throw ex;
to throw;
and getting rid of the goto
.
答案 1 :(得分:3)
The error message is raised because IEnumerator
itself does not implement IDisposable
. The latter is necessary if you want to use the object in a using
statement since using
is translated into a try/finally
block, and in this finally
block the IDisposable.Dispose
method of the object should be called.
The code looks weird all together, there is even a goto
! But what that code actually does is
Chimney
array if there are no rowsforeach
over the rows (executing what's inside the while (enumerator.MoveNext){}
loop)So it can be converted to this:
DataTable dataTable = this.ChimneyTable;
// changes start here
this.oChimneys = new Chimney[dataTable.Rows.Count];
int num = 0;
foreach(DataRow dataRow in dataTable.Rows)
{
// the part in the loop while (enumerator.MoveNext())
num++; // was the last line... for orientation
}
// end of changes
IL_247: // superflous, just for your orientation
答案 2 :(得分:0)
IEnumerator
does not implement IDisposable
. IEnumerator<T>
does but that is a different type.
Remove the using
part of that line and the accompanying block.