无法隐式转换类型字符串

时间:2012-10-19 16:41:17

标签: c#

我继续在第二个预先填充的内容中出现此错误...无法将类型字符串隐式转换为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);

        }
    }

6 个答案:

答案 0 :(得分:4)

这一行

IEnumerable<String> lines = prepopulated;

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

答案 1 :(得分:3)

您正在尝试将string分配给IEnumerable<string>

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

你应该将构造函数重构为这样的东西:

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

答案 2 :(得分: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

答案 3 :(得分:1)

如错误消息明确指出,您无法将string分配给IEnumerable<String>

您可能需要致电.Split()

答案 4 :(得分: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);
    }
}

此致

答案 5 :(得分:0)

尝试传递类型为prepopulated

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

OR

假设您的字符串类似于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