[英]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.