2

我在第二个预填充时不断收到此错误...无法将类型字符串隐式转换为 System.Collections.Generic.IEnumerable,我不确定自己做错了什么。

namespace HomeInventory2
{
    public partial class Form1 : Form
    {
        public Form1(string prepopulated)
        {
            InitializeComponent();
            IEnumerable<String> lines = prepopulated;
            textBoxAmount.Text = lines.ElementAtOrDefault(0);
            textBoxCategories.Text = lines.ElementAtOrDefault(1);
            textBoxProperties.Text = lines.ElementAtOrDefault(2);
            textBoxValue.Text = lines.ElementAtOrDefault(3);
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void submitButton_Click(object sender, EventArgs e)
        {
            CreateInventory create = new CreateInventory();
            create.ItemAmount = textBoxAmount.Text;
            create.ItemCategory = textBoxCategories.Text;
            create.ItemProperties = textBoxValue.Text;
            create.ItemValue = textBoxValue.Text;

            InventoryMngr invtryMngr = new InventoryMngr();
            invtryMngr.Create(create);

        }
    }
4

6 回答 6

4

这条线

IEnumerable<String> lines = prepopulated;

prepopulated是一个字符串,您正在尝试将其分配给字符串列表。也许你想先拆分()它?或者,也许您的表单的构造函数应该以一个字符串列表开头。

于 2012-10-19T16:43:19.190 回答
3

您正在尝试分配stringIEnumerable<string>这里:

public Form1(string prepopulated)
{
    // ...
    IEnumerable<string> lines = prepopulated;
    // ...
}

您应该将构造函数重构为如下所示:

public Form1(IEnumerable<string> prepopulated)
{
    // ...
    IEnumerable<string> lines = prepopulated;
    // ...
}
于 2012-10-19T16:43:31.250 回答
2

您的构造函数将单个字符串作为参数:

public Form1(string prepopulated)

然后,您尝试将其设置为IEnumerable<string>

IEnumerable<String> lines = prepopulated;

相反,您需要传入一个IEnumerable<string>

public Form1(IEnumerable<string> prepopulated)

如果您的prepopulated字符串是可以解析为多个字符串的字符串,则可以改为这样做。例如,如果它使用带有换行符的字符串“预填充”,您可以改为执行以下操作:

IEnumerable<String> lines = prepopulated.Split(new[] {'\n'}, StringSplitOptions.RemoveEmptyEntries); // Split into separate lines
于 2012-10-19T16:43:04.697 回答
1

正如错误消息明确指出的那样,您不能将 a 分配给stringa IEnumerable<String>

你可能想打电话.Split()

于 2012-10-19T16:42:58.847 回答
0

您不能添加到 IEnumerable,但可以执行以下操作。

IEnumerable<String> lines = new List<string>();
//or
//IEnumerable<String> lines = Enumerable.Empty<string>();
//IEnumerable<String> lines = Enumerable.Repeat("My string",1); //only single value
//IEnumerable<String> lines = new string[] {"My string","My other string"}

//or
var lines = new List<string>();
lines.Add("My string");
lines.Add("My other string");   

IEnumerable<String> ienumLines = lines;
return ienumLines;

//or add an extension method

public static void AddRange<T>(this ICollection<T> collection, IEnumerable<T> items) 
{
    foreach(T item in items) 
    {
       collection.Add(item);
    }
}

问候

于 2012-10-19T16:46:35.120 回答
0

尝试将参数prepopulated作为类型传递IEnumerable<String>

public Form1(IEnumerable<String> prepopulated)
{
    InitializeComponent();
    IEnumerable<String> lines = prepopulated;
    // ....
}

或者

假设您的字符串是 like ABC, DEF, GHI,那么您可以执行以下操作:

// where String Split is based on comma (,)
IEnumerable<String> lines = prepopulated.Split(new[] { ',' }, 
    StringSplitOptions.RemoveEmptyEntries).Select(s => s.Trim());

你得到结果:

ABC
DEF
GHI
于 2012-10-19T16:52:59.940 回答