VB6的For Each和C#.NET/VB.NET里面的foreach实现方法完全不同。
VB6里面,如果要一个集合支持For Each,它需要:
1. 实现一个ICollection接口。
Implements ICollectionPrivate Property Get ICollection_Count() As Long
End PropertyPrivate Property Get ICollection_Item(ByVal Index As Long) As Object
End PropertyPrivate Property Get ICollection_IsMemberInvalid(ByVal Object As Object) As Boolean
End Property
2. 实现一个NewEnum函数:
Public Function NewEnum() As IEnumVARIANT
End Function在.NET里,则是实现IEnumerable接口:
public interface IEnumerable {
IEnumerator GetEnumerator();
}并且在GetEnumerator里面返回一个IEnumerator接口,用于iteration.
public interface IEnumerator {
object Current {get;}
bool MoveNext();
void Reset();
}具体实现MSDN里面有例子。
}
VB6里面,如果要一个集合支持For Each,它需要:
1. 实现一个ICollection接口。
Implements ICollectionPrivate Property Get ICollection_Count() As Long
End PropertyPrivate Property Get ICollection_Item(ByVal Index As Long) As Object
End PropertyPrivate Property Get ICollection_IsMemberInvalid(ByVal Object As Object) As Boolean
End Property
2. 实现一个NewEnum函数:
Public Function NewEnum() As IEnumVARIANT
End Function在.NET里,则是实现IEnumerable接口:
public interface IEnumerable {
IEnumerator GetEnumerator();
}并且在GetEnumerator里面返回一个IEnumerator接口,用于iteration.
public interface IEnumerator {
object Current {get;}
bool MoveNext();
void Reset();
}具体实现MSDN里面有例子。
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货