将项添加到c#</string>中另一个类的List <string>中

时间:2014-05-07 12:56:57

标签: c# list class

我有3节课。那些是places.cs,onePlace.cs和placestovisit.cs。 placestovisit.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Sunamganj.ViewModels
{
    public class placestovisit
    {
        public bool IsDataLoaded { get; set; }
        public onePlace sunamganj { get; set; }
        public static string basePlaces = "Assets/Places/";
        private string baseTanguar = basePlaces + "Tanguar/";
        private string baseBaruni = basePlaces + "Baruni/";

        public void LoadData()
        {
            sunamganj = createSunamganj();

            IsDataLoaded = true;
        }

        private onePlace createSunamganj()
        {
            onePlace data = new onePlace();

            data.Items.Add(new places()
            {
                ID = "0",
                Title = "Tanguar Haor",
                shortDescription="Tanguar Haor (Lowlaying marsh) is a complex landscape of over 46 marshes, 30 km Northwest of Sunamgonj District.",
                itemImage = baseTanguar + "1.jpg",

                FullDescription = "Tanguar Haor (Lowlaying marsh) is a complex landscape of over 46 marshes, 30 km Northwest of Sunamgonj District. The marshes are inter connected with one another through narrow Channels but merge into a single large water body during monsoon. The aquatic vegetation and less disturbance from the human are instrument to invite a large variety of waterfowl specially winter migrant ducks that congregates in thousands. Resident and local migrant, raptor, waders and passerine birds made the area as one of the Asia's most potential birding place. Tanguar Haor is listed as a Ramsar site under the Ramsar Convention in 2000."
            });
    }
}

onePlace.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sunamganj.ViewModels
{
    public class onePlace
    {
        public string Title { get; set; }
        public List<places> Items { get; set; }

        public onePlace()
        {
            Items = new List<places>();
        }
    }
}

places.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sunamganj.ViewModels
{
    public class places
    {
        public string ID { get; set; }
        public string Title { get; set; }
        public string shortDescription { get; set; }
        public string FullDescription { get; set; }
        public string itemImage { get; set; }
        public List<string>Gallery { get; set; }

    }
}

我想从placestovisit类添加到Gallery中。那该怎么办?

实际上我想为每个对象添加一个照片库。但我在OOP方面并不是那么好。在这个时刻,我可以采用这个概念还是需要改变这个概念。如果我可以使用这个,那么如何从placestovisit类添加项目到画廊?

1 个答案:

答案 0 :(得分:3)

Gallery只是places类的属性,您可以通过places类的实例访问该属性来添加项目。

您应该记住,默认情况下,引用类型的属性是 null ,因此您需要初始化它们。您可以在构造函数中执行此操作:

public class places
{
    public places()
    {
        Gallery  = new List<string>();
    }
}

然后您可以在列表中添加新项目,如:

var plc = new places() { /* set the properties */ }

plc.Gallery.Add("new gallery");