ForEach In Loop抛出异常424:对象必需 [英] ForEach In Loop throwing exception 424:Object Required
问题描述
我正在使用C#中的集合在VB6中执行ForEach In循环。代码如下,
public class DTGFDTCollection:IFdtChannelCollection
{
ChannelEnum channlEnum = new ChannelEnum();
///< summary>
/// Construcotr,它初始化收藏清单
///< / summary>
public DTGFDTCollection()
{
}
public void AddChannel(IFdtChannel objChannel,string channelName)
{
channlEnum.AddChannelItems(objChannel,channelName);
}&NBSP;&NBSP;&NBSP;&NBSP;
#region IFdtChannelCollection会员
int IFdtChannelCollection.Count
{
get {return this.channlEnum.objDicChannel.Count; }
}
IFdtChannel IFdtChannelCollection.get_Item(ref object pvarIndex)
{
试试
{
如果(pvarIndex.ToString()包含(QUOT; _"))
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP; {
的foreach(在this.channlEnum.objDicChannel.Keys串strkey)
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP; {
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;如果(strkey.Equals(pvarIndex.ToString()))
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; //指数= this.objDicChannel.alVariant.IndexOf(变体);
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;返回(IFdtChannel)this.channlEnum.objDicChannel [strkey];
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; }
}
}
返回null;
}
catch(例外)
{
返回null;
}
}
#endregion
#region IEnumerable成员
IEnumerator IEnumerable.GetEnumerator()
{
return(IEnumerator)channlEnum;
}
[DispId(-4)]
$
public IEnumerator GetEnumerator()
{
return(IEnumerator)channlEnum;
}
#endregion
我已经为ChannelEnum类实现了IEnumerator,如下所示
public class DTGFDTCollection : IFdtChannelCollection
{
ChannelEnum channlEnum = new ChannelEnum();
/// <summary>
/// Construcotr, it initializes the collection list
/// </summary>
public DTGFDTCollection()
{
}
public void AddChannel(IFdtChannel objChannel, string channelName)
{
channlEnum.AddChannelItems(objChannel, channelName);
}
#region IFdtChannelCollection Members
int IFdtChannelCollection.Count
{
get { return this.channlEnum.objDicChannel.Count; }
}
IFdtChannel IFdtChannelCollection.get_Item(ref object pvarIndex)
{
try
{
if (pvarIndex.ToString().Contains("_"))
{
foreach (string strkey in this.channlEnum.objDicChannel.Keys)
{
if (strkey.Equals(pvarIndex.ToString()))
{
//index = this.objDicChannel.alVariant.IndexOf(Variant);
return (IFdtChannel)this.channlEnum.objDicChannel[strkey];
}
}
}
return null;
}
catch (Exception )
{
return null;
}
}
#endregion
#region IEnumerable Members
IEnumerator IEnumerable.GetEnumerator()
{
return (IEnumerator)channlEnum;
}
[DispId(-4)]
public IEnumerator GetEnumerator()
{
return (IEnumerator)channlEnum;
}
#endregion
I have implemented the IEnumerator for the ChannelEnum class as below
公共类ChannelEnum:IEnumerator
{
internal Dictionary< string,IFdtChannel> objDicChannel =新词典< string,IFdtChannel>(3);
$
string [] strNames = new string [3];
int i = 0;
public void AddChannelItems(IFdtChannel objChannel,string channelName)
{
objDicChannel.Add(channelName,objChannel);
strNames [i ++] = channelName;
}
#region IEnumerator会员
int position = -1;
字符串键;
object IEnumerator.Current
{
得到
{
试试
{
return objDicChannel [key];
}
catch(例外)
{
抛出新的InvalidOperationException();
}
}
}
bool IEnumerator.MoveNext()
{
职位++;
key = strNames [position];
return(position< objDicChannel.Count);
}
void IEnumerator.Reset()
{
position = -1;
}
#endregion
}
public class ChannelEnum:IEnumerator
{
internal Dictionary<string, IFdtChannel> objDicChannel = new Dictionary<string, IFdtChannel>(3);
string[] strNames = new string[3];
int i = 0;
public void AddChannelItems(IFdtChannel objChannel, string channelName)
{
objDicChannel.Add(channelName, objChannel);
strNames[i++] = channelName;
}
#region IEnumerator Members
int position = -1;
string key;
object IEnumerator.Current
{
get
{
try
{
return objDicChannel[key] ;
}
catch (Exception)
{
throw new InvalidOperationException();
}
}
}
bool IEnumerator.MoveNext()
{
position++;
key = strNames[position];
return (position < objDicChannel.Count);
}
void IEnumerator.Reset()
{
position = -1;
}
#endregion
}
此处AddChannels()由其他类方法调用,并返回IFdtChannelCollection类型的值。我需要执行
For Each In VB6中此集合的循环。
Here the AddChannels() is called by other class method and returns the value of type IFdtChannelCollection.I need to do the For Each In loop for this collection in VB6.
如果我尝试使用C#和VB.net为此集合执行 foreach ,它可以正常工作,但如果我尝试在VB6中执行
For Each In 循环,在调用 Current 属性后抛出异常
424:需要对象。我不知道这里出了什么问题。
当前属性返回一个有效的对象,但仍然会抛出错误。所以有人可以帮我吗?。
If i try to do the foreach for this collection with C# and VB.net, it works fine,but If i try to do the
For Each In loop in VB6, after the Current property is called it throws exception
424:Object required. I am clueless what is going wrong here.The
Current property returns a valid object ,but still it throws the error.So can anyone help me in this?.
推荐答案
Visual Basic 6.0是这个论坛不支持。请参阅以下链接获取VB 6.0问题。
Visual Basic 6.0 is not supported in this forum. See the below link for VB 6.0 questions.
http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/6a0719fe-14af-47f7-9f51-a8ea2b9c8d6b
这篇关于ForEach In Loop抛出异常424:对象必需的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!