[英]Converting usage of VB6 err object to C#
下面是我的vb6代码,我如何将其转换为c#,实际上我在if条件下对err的处理中感到困惑。
Function FilterDuplicates(Arr As Variant) As Long
Dim col As Collection, Index As Long, dups As Long
Set col = New Collection
On Error Resume Next
For Index = LBound(Arr) To UBound(Arr)
col.Add 0, CStr(Arr(Index))
If err Then
Arr(Index) = Empty
dups = dups + 1
err.Clear
ElseIf dups Then
Arr(Index - dups) = Arr(Index)
Arr(Index) = Empty
End If
Next
FilterDuplicates = dups End Function
下面是我尝试过的C#代码,实际上我无法处理此If条件部分。
private long FilterDuplicates(string[] Arr)
{
Collection col = new Collection();
long Index=0;
long dups =0;
try
{
for (Index = Arr.GetLowerBound(0); Index <= Arr.GetUpperBound(0); Index++)
{
col.Add(0, Conversion.Str(Arr[Index]));
if (Information.Err)
{
Arr[Index] = null;
dups += 1;
err.Clear;
}
else if (dups != 0)
{
Arr[Index - dups] = Arr[Index];
Arr[Index] = null;
}
}
return dups;
}
catch
{
}
}
通常, 实现例程要比转换例程容易。 看来,你要
null
如果是您的情况,可以尝试
private static long FilterDuplicates(string[] Arr) {
HashSet<string> appeared = new HashSet<string>();
long result = 0;
for (int i = 0; i < Arr.Length; ++i)
if (!appeared.Add(Arr[i])) {
result += 1;
Arr[i] = null;
}
return result;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.