C# 无法隐式转换类型字符串

C# 无法隐式转换类型字符串,c#,C#,我在第二次预填充时一直遇到这个错误。。。无法将类型字符串隐式转换为System.Collections.Generic.IEnumerable,我不确定我做错了什么 namespace HomeInventory2 { public partial class Form1 : Form { public Form1(string prepopulated) { InitializeComponent();

我在第二次预填充时一直遇到这个错误。。。无法将类型字符串隐式转换为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);

        }
    }
命名空间HomeInventory2
{
公共部分类Form1:Form
{
公共表单1(字符串预填充)
{
初始化组件();
IEnumerable行=预填充;
textBoxAmount.Text=lines.ElementAtOrderFault(0);
textBoxCategories.Text=lines.ElementAtOrderFault(1);
textBoxProperties.Text=lines.elementatorderfault(2);
textBoxValue.Text=lines.elementatorderfault(3);
}
私有无效标签1_单击(对象发送方,事件参数e)
{
}
私有无效提交按钮单击(对象发送者,事件参数e)
{
CreateInventory创建=新建CreateInventory();
create.ItemAmount=textBoxAmount.Text;
create.itemcegory=textBoxCategories.Text;
create.ItemProperties=textBoxValue.Text;
create.ItemValue=textBoxValue.Text;
InventoryMngr invtryMngr=新的InventoryMngr();
invtryMngr.Create(创建);
}
}

正如错误消息明确指出的,您不能将
字符串
分配给
IEnumerable


您可能需要调用
.Split()

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

public Form1(string prepopulated)
然后尝试将其设置为
IEnumerable

这条线

IEnumerable<String> lines = prepopulated;
IEnumerable行=预填充;

prepopulated是一个字符串,您正试图将其分配给一个字符串列表。可能您想先拆分()它?或者表单的构造函数应该从字符串列表开始。

您正试图将
字符串
分配给
IEnumerable
,此处:

public Form1(string prepopulated)
{
    // ...
    IEnumerable<string> lines = prepopulated;
    // ...
}
public Form1(字符串预填充)
{
// ...
IEnumerable行=预填充;
// ...
}
您应该将constructor重构为如下内容:

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

您不能添加到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);
    }
}
IEnumerable lines=new List();
//或
//IEnumerable lines=Enumerable.Empty();
//IEnumerable lines=Enumerable.Repeat(“我的字符串”,1);//仅单个值
//IEnumerable lines=新字符串[]{“我的字符串”、“我的其他字符串”}
//或
变量行=新列表();
行。添加(“我的字符串”);
行。添加(“我的其他字符串”);
IEnumerable ienumLines=行;
返回ienumLines;
//或者添加一个扩展方法
公共静态void AddRange(此ICollection集合,IEnumerable items)
{
foreach(项目中的T项目)
{
集合。添加(项目);
}
}

作为类型
IEnumerable

你得到的结果是:

ABC
DEF
GHI

你读过这条消息吗?是编译错误还是运行时错误?预填充的是什么?你希望在
行中填充什么?
?是的-我也怀疑是这样-大多数投票都发生了这种情况。你不会是认真的吧?如果你看这家伙的个人资料,他有6个问题反复询问相同的代码呃,再一次,从所有答案中一块一块地,你最终会得到这个代码。我可以理解你想要帮助,但是拉着这个按钮,有人否决了一张合法的答题卡是愚蠢的。没有人应该回答这个问题,应该标记这个问题,他的个人资料也是如此。@Rikkos,你真的认为我在跟踪每个发布问题的人吗问题?我看到了他的问题,我已经回答了,这对我来说似乎很正常。真的,我不明白你的意思,我(和其他人)应该被否决,因为他发布了错误的(至少对你来说)问题?;)在
IEnumerable
中没有“add”方法,这也不是一个你可以通过
new
创建的对象,因为它是一个接口。
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);
    }
}
public Form1(IEnumerable<String> prepopulated)
{
    InitializeComponent();
    IEnumerable<String> lines = prepopulated;
    // ....
}
// where String Split is based on comma (,)
IEnumerable<String> lines = prepopulated.Split(new[] { ',' }, 
    StringSplitOptions.RemoveEmptyEntries).Select(s => s.Trim());
ABC
DEF
GHI