Expecto
Expecto

Reputation: 521

Cannot implicitly convert type string

I keep getting this error on the second prepopulated... Cannot implicity convert type string to System.Collections.Generic.IEnumerable and I am unsure of what I am doing wrong.

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

        }
    }

Upvotes: 2

Views: 6688

Answers (6)

Matija Grcic
Matija Grcic

Reputation: 13371

You can't add to the IEnumerable but you can do something of the following.

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

Regards

Upvotes: 0

Furqan Safdar
Furqan Safdar

Reputation: 16698

Try passing the parameter prepopulated as of type IEnumerable<String>

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

OR

Assuming your string is like ABC, DEF, GHI, then you can do something like this:

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

You get the result:

ABC
DEF
GHI

Upvotes: 0

Zbigniew
Zbigniew

Reputation: 27584

You are trying to assign string to IEnumerable<string> here:

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

You should refactor contructor to something like this:

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

Upvotes: 3

Dharun
Dharun

Reputation: 613

This line

IEnumerable<String> lines = prepopulated;

prepopulated is a string and you are trying to assign it to a List of strings. Maybe you want to Split() it first? Or maybe your Form's constructor should take a List of strings to begin with.

Upvotes: 4

Reed Copsey
Reed Copsey

Reputation: 564333

You have your constructor taking a single string as a parameter:

public Form1(string prepopulated)

You then are trying to set this to an IEnumerable<string>:

IEnumerable<String> lines = prepopulated;

You need to, instead, pass in an IEnumerable<string>:

public Form1(IEnumerable<string> prepopulated)

If your prepopulated string is a string which can be parsed into multiple strings, you could do that instead. For example, if it's "prepopulated" with a string with newline separations, you could instead do something like:

IEnumerable<String> lines = prepopulated.Split(new[] {'\n'}, StringSplitOptions.RemoveEmptyEntries); // Split into separate lines

Upvotes: 2

SLaks
SLaks

Reputation: 887215

As the error message clearly states, you cannot assign a string to an IEnumerable<String>.

You may want to call .Split().

Upvotes: 1

Related Questions