无法创建对象来填充项目。 [英] Unable to create Object to populate the items.

查看:48
本文介绍了无法创建对象来填充项目。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我创建了一个名为Car的类,然后创建了另一个名为CarSourceItem的类。通过Console程序,我无法通过创建CarSourceItem的对象来通过Foreach循环打印Car类型对象。



Car.cs

 class Car 
{
public string Brand {get;组; }
public int Wheels {get;组; }
public string Paint {get;组; }
公共字符串GearBox {get;组; }
}





CarSourceItem.cs

  class  CarItemSource 
{
public List< Car>汽车{获取; set ; }

public CarItemSource()
{
new Car {Brand = Audi,GearBox = 自动,Paint = Yello ,Wheels = 4 };
new Car {Brand = Maruti 800 ,GearBox = Manual,Paint = 白色,Wheels = 4 };
new Car {Brand = Vaux ,GearBox = Manual,Paint = 深黑色,轮子= 4 };
new Car {Brand = TATA ,GearBox = Manual,Paint = Lime Green,Wheels = 4 };
}

}







请帮帮我。

解决方案

Gopi Kishan Mariyala让你走在正确的轨道上:这里的基本问题是汽车收藏没有被初始化。



你可以保存一些类似的输入:

  public   class  Car 
{
public string 品牌{获得; set ; }
public string GearBox { get ; set ; }
public string Paint { get ; set ; }
public int 轮子{ get ; set ; }

public Car( string 品牌, string gearbox, string paint, int wheels)
{
品牌=品牌;
GearBox =变速箱;
油漆=油漆;
车轮=车轮;
}
}

public class CarItemSource
{
public 列表< Car>汽车{获取; set ; }

public CarItemSource()
{
cars = new 列表< Car>
{
new Car( Audi 自动 Yello 4 ),
Car( Maruti 800 Manual 白色 4 ),
new Car( Vaux 手动 深黑色 4 ),
new Car( TATA,< span class =code-string>
Manual Lime Green 4
};
}
}



传递一堆参数并将它们分配给Class'变量是很繁琐的:但是,一个好的养成这样做的习惯是下一个版本的C#可以为你自动生成属性变量反向存储:[ ^ ]。



但请注意在上面,以及对该线程的所有响应中,您可以创建任意数量的CarItemSource类的新实例,并且每个实例将重复具有相同的内部列表< Car>。 imho,这不是一件好事。



所以,我建议你在这里重新考虑你的设计,目标是只创建一个独特的List< Car>。 />


您可能认为可以使用此类类定义来解决此问题:

  public   class  CarItemSource:List< Car> 
{
public CarItemSource()
{
if this .Count == 0
{
.AddRange( new 列表< Car>
{
new Car( Audi 自动 Yello 4 ),
new Car( Maruti 800 手动 白色 4 ),
new Car( Vaux Manual 深黑色 4 ),
new Car( TATA Manual Lime Green 4
});
}
}
}

但是,这有同样的问题:你可以创建CarItemSource的多个实例,以及List< Car>重新创建(我有一个以上的学生认为他们用这种聪明的策略解决了这类问题!)。



显而易见的选择是制作CarItemSource是一个静态类:

  public   static  公开  CarItemSource 
{
public static 列表< Car> CarList = new 列表< Car>
{
new Car( Audi 自动 Yello 4 ),
Car( Maruti 800 Manual 白色 4 ),
new Car( Vaux 手动 深黑色 4 ),
new Car( TATA Manual Lime Green 4
};
}

测试,在某个方法中,如下所示:

 Car aCar = CarItemSource.CarList [0]; 

string carName = aCar.Brand;

从技术上讲,具有一个且唯一一个可能实例的类称为单身人士。为了深入考虑C#中的Singletons,Jon Skeet像往常一样,有明确的分析:[ ^ ]。


CarItemSource应修改如下。未创建列表对象。创建列表对象后,我们必须添加汽车项目。



公共列表< ;  汽车 >  cars {get;组; } 

public CarItemSource()
{
cars = new List < 汽车 > ();
cars.Add(新车{Brand =Audi,GearBox =Automatic,Paint =Yello,Wheels = 4});
cars.Add(新车{Brand =Maruti 800,GearBox =手动,油漆=白色,车轮= 4});
cars.Add(new Car {Brand =Vaux,GearBox =Manual,Paint =Dark Black,Wheels = 4});
cars.Add(new Car {Brand =TATA,GearBox =Manual,Paint =Lime Green,Wheels = 4});
}


试试这个





< pre lang =cs> public CarItemSource()
{
cars.Add( new Car {Brand = Audi,GearBox = 自动,Paint = Yello,Wheels = 4 });
cars.Add( new Car {Brand = Maruti 800,GearBox = 手动,Paint = White,Wheels = 4 });
cars.Add( new Car {Brand = Vaux,GearBox = 手动,Paint = Dark Black,Wheels = 4 });
cars.Add( new Car {Brand = TATA,GearBox = 手动,Paint = Lime Green,Wheels = 4 });
}


Hi,

I have created a Class called "Car" and then I created another Class called "CarSourceItem". Through the Console program I am unable to print the Car type objects via Foreach loop by creating object of the CarSourceItem.

Car.cs

class Car
    {
        public string Brand { get; set; }
        public int Wheels { get; set; }
        public string Paint { get; set; }
        public string GearBox { get; set; }
    }



CarSourceItem.cs

class CarItemSource
    {
        public List<Car> cars { get; set; }

        public CarItemSource()
        {
            new Car { Brand = "Audi", GearBox = "Automatic", Paint = "Yello", Wheels = 4 };
            new Car { Brand = "Maruti 800", GearBox = "Manual", Paint = "White", Wheels = 4 };
            new Car { Brand = "Vaux", GearBox = "Manual", Paint = "Dark Black", Wheels = 4 };
            new Car { Brand = "TATA", GearBox = "Manual", Paint = "Lime Green", Wheels = 4 };
         }

    }




Help me please.

解决方案

Gopi Kishan Mariyala put you on the right track here: the essential problem here is that the Cars collection is not initialized.

You can save some typing like this:

public class Car
{
    public string Brand { get; set; }
    public string GearBox { get; set; }
    public string Paint { get; set; }
    public int Wheels { get; set; }

    public Car(string brand, string gearbox, string paint, int wheels)
    {
        Brand = brand;
        GearBox = gearbox;
        Paint = paint;
        Wheels = wheels;
    }
}

public class CarItemSource
{
    public List<Car> cars { get; set; }

    public CarItemSource()
    {
        cars = new List<Car> 
        {
            new Car ("Audi","Automatic","Yello", 4 ),
            new Car ("Maruti 800", "Manual", "White", 4 ),
            new Car ("Vaux", "Manual", "Dark Black", 4 ),
            new Car ("TATA", "Manual", "Lime Green", 4 )
        };
    }
}


It is tedious to pass in a bunch of parameters and have to assign them to the Class' variables: but, a good reason to get in the habit of doing this is that the next version of C# may automatically generate the Property variable back-stores for you: [^].

However, note that in the above, and in all the responses to this thread, that you could create any number of new instances of the CarItemSource Class, and each one will repeat having the identical internal List<Car>. imho, that is not a good thing.

So, I suggest you reconsider your design here, with the goal of creating only one unique List<Car>.

You might think you could solve this using this type of Class definition:

public class CarItemSource: List<Car>
{
    public CarItemSource()
    {
        if(this.Count == 0)
        {
            this.AddRange( new List<Car>
            {
                new Car ("Audi","Automatic","Yello", 4 ),
                new Car ("Maruti 800", "Manual", "White", 4 ),
                new Car ("Vaux", "Manual", "Dark Black", 4 ),
                new Car ("TATA", "Manual", "Lime Green", 4 )
            });
        }
    }
}

But, that has the same problem: you can create multiple instances of CarItemSource, and the List<Car> is re-created (I have had more than one student who thought they solved this type of problem with this type of clever strategy !).

The obvious alternative is making CarItemSource a static Class:

public static public class CarItemSource
{
    public static List<Car> CarList = new List<Car>
    {
        new Car ("Audi","Automatic","Yello", 4 ),
        new Car ("Maruti 800", "Manual", "White", 4 ),
        new Car ("Vaux", "Manual", "Dark Black", 4 ),
        new Car ("TATA", "Manual", "Lime Green", 4 )
    };
}

Test, somewhere in a method, like this:

Car aCar = CarItemSource.CarList[0];

string carName = aCar.Brand;

Classes that have one-and-only-one possible instance are, technically, referred to as "Singletons." For an in-depth consideration of Singletons in C#, Jon Skeet, as usual, has the definitive analysis: [^].


The CarItemSource should be modified as below. The list object was not created. And after creating list object, we have to add car items.

public List<Car> cars { get; set; }

        public CarItemSource()
        {
            cars = new List<Car>();
            cars.Add(new Car { Brand = "Audi", GearBox = "Automatic", Paint = "Yello", Wheels = 4 });
            cars.Add(new Car { Brand = "Maruti 800", GearBox = "Manual", Paint = "White", Wheels = 4 });
            cars.Add(new Car { Brand = "Vaux", GearBox = "Manual", Paint = "Dark Black", Wheels = 4 });
            cars.Add(new Car { Brand = "TATA", GearBox = "Manual", Paint = "Lime Green", Wheels = 4 });
         }


Try this


public CarItemSource()
    {
        cars.Add(new Car { Brand = "Audi", GearBox = "Automatic", Paint = "Yello", Wheels = 4 });
        cars.Add(new Car { Brand = "Maruti 800", GearBox = "Manual", Paint = "White", Wheels = 4 });
        cars.Add(new Car { Brand = "Vaux", GearBox = "Manual", Paint = "Dark Black", Wheels = 4 });
        cars.Add(new Car { Brand = "TATA", GearBox = "Manual", Paint = "Lime Green", Wheels = 4 });
    }


这篇关于无法创建对象来填充项目。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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