如何从类中返回包含对象的集合

问题描述 投票:0回答:1

我有一个类变量和另一个类变量。变量类用于维护和生成Variable类型的对象列表。

一切正常,我很高兴。但有一件事我不知道如何完成它。

我想在这样的foreach循环中使用变量..

Variables oVariables;
foreach(Variable element in oVariables)

但是我不能通过在这个oVariable示例中使用类Variables的对象来做到这一点。我必须编写一些将返回Collection的函数。那么有什么办法可以避免编写额外的函数并从中获取Collection。

任何帮助表示赞赏。谢谢这是变量类的代码。

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualBasic;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace APVariable
{
    public class Variables
    {
        private Collection mCol;
        public Variable Add(string Name, object Value, string skey = "")
        {
            Variable objNewMember = new Variable();
            objNewMember.Name = Name;
            objNewMember.Value = Value;

            if (skey.Length == 0)
            {
                mCol.Add(objNewMember);
            }
            else
            {
                try
                {
                    Information.Err().Clear();
                    mCol.Add(objNewMember, skey);
                    if (Information.Err().Number != 0)
                    {
                        Information.Err().Clear();
                        mCol.Add(objNewMember);
                    }
                }
                catch { Information.Err(); }
                {

                }
            }
            return objNewMember;
            objNewMember = null;
        }
        public int count
        {
            get
            {
                return mCol.Count;
            }
        }
        //public void Remove(int vntIndexKey)
        //{
        //    //this can be the int or the string.
        //    //passes the index or the key of the collection to be removed.

        //    mCol.Remove(vntIndexKey);
        //}
        public void Remove(dynamic vntIndexKey)
        {
            //this can be the int or the string.
            //passes the index or the key of the collection to be removed.

            mCol.Remove(vntIndexKey);
        }
        public dynamic newEnum
        {
            get
            {
                return mCol.GetEnumerator();
            }

        }

        public Variable this[object vIndex]
        {
            get
            {
                Variable result = null;
                try
                {
                    result = (Variable)mCol[vIndex];

                }
                catch
                {
                }
                return result;
            }
        }
        //public IEnumerator GetEnumerator()
        //{
        //    //this property allows you to enumerate
        //    //this collection with the For...Each syntax
        //    return mCol.GetEnumerator();
        //}
        public Variables()
        {
            mCol = new Collection();
        }
         ~Variables()
        {
            mCol = null;

        }

    }
}
c# .net vb.net
1个回答
1
投票

您需要实现IEnumerable接口。这样就可以在课堂上使用foreach了。你也应该使用Colection

看起来你已经开始,你几乎就在那里。

namespace APVariable
{
    public class Variables : IEnumerable, IEnumerator
    {
        private Collection<Variable> mCol;
        public Variable Add(string Name, object Value, string skey = "")
        {
            Variable objNewMember = new Variable();
            objNewMember.Name = Name;
            objNewMember.Value = Value;

            if (skey.Length == 0)
            {
                mCol.Add(objNewMember);
            }
            else
            {
                try
                {
                    Information.Err().Clear();
                    mCol.Add(objNewMember, skey);
                    if (Information.Err().Number != 0)
                    {
                        Information.Err().Clear();
                        mCol.Add(objNewMember);
                    }
                }
                catch { Information.Err(); }
                {

                }
            }
            return objNewMember;
            objNewMember = null;
        }
        public int count
        {
            get
            {
                return mCol.Count;
            }
        }
        //public void Remove(int vntIndexKey)
        //{
        //    //this can be the int or the string.
        //    //passes the index or the key of the collection to be removed.

        //    mCol.Remove(vntIndexKey);
        //}
        public void Remove(dynamic vntIndexKey)
        {
            //this can be the int or the string.
            //passes the index or the key of the collection to be removed.

            mCol.Remove(vntIndexKey);
        }
        public dynamic newEnum
        {
            get
            {
                return mCol.GetEnumerator();
            }

        }

        public Variable this[object vIndex]
        {
            get
            {
                Variable result = null;
                try
                {
                    result = (Variable)mCol[vIndex];

                }
                catch
                {
                }
                return result;
            }
        }
        public IEnumerator GetEnumerator()
        {
            //this property allows you to enumerate
            //this collection with the For...Each syntax
            return mCol.GetEnumerator();
        }
        public Variables()
        {
            mCol = new Collection<Variable>();
        }
         ~Variables()
        {
            mCol = null;

        }

    }
}
</code>
</pre>

我建议继承Collection或List或实现IList或ICollection

© www.soinside.com 2019 - 2024. All rights reserved.