Click here to Skip to main content
15,896,409 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
I have error called cant implicitly convert type string to at field owner of Cat And Owner of Dog For Following code.

C#
class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
class Pet
    {
        public string Name { get; set; }
        public Person Owner { get; set; }
    }
class Cat:Pet
    {
    }
class Dog:Pet
    {
    }
 class Program
    {
        static void Main(string[] args)
        {
            List<person> persons = new List<person>
            {
                new Person {FirstName="ganesh",LastName="swami"},
                new Person {FirstName="ranjit",LastName="swami"},
                new Person {FirstName="mahesh",LastName="biradar"},
                new Person {FirstName="vijay",LastName="shahane"},
                new Person{FirstName="pramod",LastName="pande"},


            };
            List<cat> cats = new List<cat>{
                new Cat {Name="mao",Owner="mahesh"},
                new Cat {Name="mao",Owner="ranjit"},
                new Cat {Name="mao",Owner="vijay"},
                new Cat {Name="mao",Owner="ganesh"},
            };

            List<dog> dogs = new List<dog>{
                new Dog{Name="mao",Owner="mahesh"},
                new Dog{Name="mao",Owner="vijay"},
                new Dog{Name="mao",Owner="pramod"},
                new Dog{Name="mao",Owner="ranjit"},
        };
        }
Posted
Updated 9-Oct-13 22:11pm
v2

Look at this line:

new Cat {Name="mao",Owner="mahesh"},


Property Owner is of type Person but yet you're assigning a string to it?

Try this instead:

C#
new Cat {Name="mao",Owner=persons.First(m => m.FirstName == "ganesh")},


My example is getting a Person object from the persons collection.
 
Share this answer
 
v2
You are getting this error because you are trying to assign "Owner" a string value instead of a Person value when generating your Cats and Dogs lists.

Instead you need to do something like this:

C#
new Dog{Name="mao", Owner=new Person{FirstName="manhesh", LastName-"birdadar"}}


Or you could do

C#
new Dog{Name="mao", Owner= persons.First(p => p.FirstName=="mahesh")}


The second approach uses Linq to find the appropraite person from the persons list you previously created based on their firstname.
 
Share this answer
 
As already pointed out in other solutions, you should pass a Person object as Owner, for instance
Quote:
new Cat {Name="mao",Owner="mahesh"}
could be
C#
new Cat {Name="mao",Owner=persons[2]}


Please note.
Quote:
List<person> persons = new List<person>
Should actually be
C#
List<<b>Person> persons = new List<<b>Person>

and so on for cats and dogs.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900