2012-10-19 37 views
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); 

     } 
    } 
+7

你阅读的信息? – SLaks

+0

它是编译错误还是运行时错误? – Ikaso

+0

什么是“预填充”填充?你期望在'行'中有什么? –

回答

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 
1

由于错误信息中明确指出,你不能分配stringIEnumerable<String>。您可能需要拨打.Split()

4

此行

IEnumerable<String> lines = prepopulated; 

预填充是一个字符串,你要转让它到一个字符串列表。也许你想斯普利特()它第一?或者,也许你的表单的构造函数应该以一个字符串列表开头。

3

您试图分配stringIEnumerable<string>这里:

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

你应该重构构造器的东西是这样的:

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

我可以知道为什么这个答案是downvoted? – Zbigniew

+0

啊,我怀疑有人在玩票,让他的答案“更高”...... – Zbigniew

+0

是的 - 我怀疑也是这样 - 大部分投票都发生过。 –

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); 
    } 
} 

问候

+0

“IEnumerable ”中没有“add”方法,也不是可以通过'new'创建的对象,因为它是*接口*。 –

+0

对不起,这是对CP的快速直接反应,现在编辑。 –

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 
相关问题