Hello World
Hello World

Reputation: 1437

Can't convert to char

This is just a simple problem that I'm a little stumped on...

As far as I can see I am not actually using a char anywhere as model.children is a IList<> and model.Name is a string however my foreach loop is giving the error that it can't convert char to catalogue.department, if you need any more information plese feel free to ask.

        private void DisplayOnWebsiteChecked(Object sender, EventArgs e)
    {
        var departments = model.Name;
        var departmentChildren = model.Children;

        if (departmentChildren != null)
        {
            int zeroChildren = 0;

            if (departmentChildren.Count.Equals(zeroChildren));
            {
                foreach (Department Children in departments)
                {
                }
            }
        }
    }

EDIT: This issue has actually be fixed, I will post the fixed code down below as it turns out I actually messed up the code by putting the list in the wrong place.

    private void DisplayOnWebsiteChecked(Object sender, EventArgs e)
    {
        var departments = model.Name;
        var departmentChildren = model.Children;

        if (departmentChildren != null)
        {
            int zeroChildren = 0;

            if (departmentChildren.Count.Equals(zeroChildren));
            {
                foreach (Department Child in departmentChildren)
                {
                }
            }
        }
    }

Upvotes: 0

Views: 131

Answers (5)

Vishal Suthar
Vishal Suthar

Reputation: 17193

departments is a string and you are trying to treat the string as an IEnumerable<char>

As string implements IEnumerable<char> and that is why the items in the foreach loop is of type char.

AND

int zeroChildren = 0;

if (departmentChildren.Count.Equals(zeroChildren));
{

}

So here, what is the meaning of foreach loop if departmentChildren collection is empty...?

Upvotes: 0

Jared Russell
Jared Russell

Reputation: 11332

The issue is with this line:

foreach (Department Children in departments)

departments is a string and you're trying to iterate over it, which is treating the string as an IEnumerable<char>

Upvotes: 1

cuongle
cuongle

Reputation: 75306

departments is a string from assignment:

 var departments = model.Name;

THat is why you got Children as char

Should be:

 foreach (Department Children in departmentChildren)
 {
 }

Upvotes: 1

Rafal
Rafal

Reputation: 12619

In your code:

var departments = model.Name;

I suppose that Name is a string then you are enumerating over collection of chars.

Upvotes: 0

Habib
Habib

Reputation: 223237

model.Name is a string

You have assigned:

var departments = model.Name;

here department is of type string and string implements IEnumerable<char>, that is why when you enumerate over it using foreach, the item in the foreach loop is of type char.

and you are using

foreach (Department Children in departments)
    {
    }

Here Children is of type char since department is a string, whereas you have specified it to be of type Department that is why you are getting this error.

Upvotes: 3

Related Questions