错误:无法转换为' lab3.MultiSet'到'字符串[]'

时间:2015-03-29 14:43:10

标签: c#

MultiSet.cs和Form.cs

  

'string.Join(string,string [])'的最佳重载方法匹配有一些无效的参数

     

参数'2':无法从'lab3.MultiSet'转换为'string []'

namespace lab3
{
    internal class MultiSet : IEnumerable<int>
    {
        public MultiSet()
        {
        }

        public MultiSet(IEnumerable<int> elements)
        {
            if (elements == null)
                throw new ArgumentNullException("elements");

            foreach (int element in elements)
                Add(element);
        }

        public MultiSet(params int[] elements)
            : this((IEnumerable<int>)elements)
        {
        }

        public IEnumerator<int> GetEnumerator()
        {
            return new Enumerator(this);
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        public bool Add(int element)
        {
            if (_cardinal == _elements.Length)
            {
                int[] newElementsArray = new int[_elements.Length * 2];
                _elements.CopyTo(newElementsArray, 0);
                _elements = newElementsArray;
            }
            _elements[_cardinal] = element;
            _cardinal++;
            _lastModificationTime = DateTime.Now;

            return true;
        }

        public int Count
        {
            get
            {
                return _cardinal;
            }
        }

        public override string ToString()
        {
            return string.Format("{{ {0} }}", string.Join(", ", this));
        }

        private int _cardinal = 0;
        private int[] _elements = new int[8];
        private DateTime _lastModificationTime = DateTime.Now;

        private sealed class Enumerator: IEnumerator<int>
        {       
            public Enumerator(MultiSet set)
            {
                if (set == null)
                    throw new ArgumentNullException("set");

                _set = set;
                _setLastModificationTime = _set._lastModificationTime;
                Reset();
            }

            public int Current
            {
                get
                {
                    if (_index < 0 || _set._cardinal <= _index || _setLastModificationTime != _set._lastModificationTime)
                        throw new InvalidOperationException();

                    return _set._elements[_index];
                }
            }

            void IDisposable.Dispose()
            {
            }

            object IEnumerator.Current
            {
                get
                {
                    return Current;
                }
            }

            public bool MoveNext()
            {
                if (_setLastModificationTime != _set._lastModificationTime)
                    throw new InvalidOperationException();

                _index++;
                return (_index < _set.Count);
            }

            public void Reset()
            {
                _index = -1;
            }

            private int _index;
            private readonly MultiSet _set;
            private readonly DateTime _setLastModificationTime;
        }
    }
}

namespace lab3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            _setElementNumericUpDown.Minimum = int.MinValue;
            _setElementNumericUpDown.Maximum = int.MaxValue;

            _currentSetTextBox.Text = _currentSet.ToString();
        }

        private void _addSetButton_Click(object sender, EventArgs e)
        {
            _setsListBox1.Items.Add(_currentSet);
            _setsListBox2.Items.Add(_currentSet);
            _currentSet = new MultiSet();
           _currentSetTextBox.Text = _currentSet.ToString();
        }

        private void _addElementToSetButton_Click(object sender, EventArgs e)
        {
            _currentSet.Add((int)_setElementNumericUpDown.Value);
            _currentSetTextBox.Text = _currentSet.ToString();           
        }

        private MultiSet _currentSet = new MultiSet();
    }
}

1 个答案:

答案 0 :(得分:0)

因为你的类是IEnumerable&lt; int&gt;,您是否尝试过创建一个返回列表中项目的字符串[]的方法?

private string[] ToStringArray()
{
    if (this.Count == 0)
        return null;

    int i = 0;
    string[] items = new string[this.Count];
    IEnumerator<int> enumerator = this.GetEnumerator();
    while(enumerator.MoveNext())
    {
        items[i] = enumerator.Current.ToString();
        i++;
    }
    // Reset your enumerator index if needed
    enumerator.Reset();

    return items;
}

然后你的ToString()看起来像这样:

public override string ToString()
{       
    return string.Format("{{ {0} }}", string.Join(", ", ToStringArray()));
}