我在C#中有错误 [英] i have an error in C#

查看:50
本文介绍了我在C#中有错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为cant的错误隐式将类型字符串转换为Cat的所有者和Dog的所有者以下代码。



I have error called cant implicitly convert type string to at field owner of Cat And Owner of Dog For Following code.

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"},
        };
        }

推荐答案

看看这一行:



Look at this line:

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





Property 所有者的类型为 Person 但是你要为它分配一个字符串



试试这个:





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

Try this instead:

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





我的例子是获得来自人员集合的对象。


您收到此错误是因为您尝试为所有者分配字符串值而不是生成您的猫和狗名单时的人值。



相反,你需要做这样的事情:



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:

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





或者你可以做





Or you could do

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





第二种方法使用Linq从您之前根据其名字创建的人员列表中找到合适的人。



The second approach uses Linq to find the appropraite person from the persons list you previously created based on their firstname.


正如其他解决方案中已经指出的那样,你应该将 Person 对象作为所有者传递,例如

As already pointed out in other solutions, you should pass a Person object as Owner, for instance
Quote:

new Cat {Name =mao,Owner =mahesh}

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

可能是

could be

new Cat {Name="mao",Owner=persons[2]}





请注意。



Please note.

Quote:

List< person> person = new List< person>

List<person> persons = new List<person>

实际应该是

Should actually be

List<<b>Person> persons = new List<<b>Person>



依此类推


这篇关于我在C#中有错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆