繁体   English   中英

将项目添加到列表 <string> 从C#中的另一个类

[英]add item into List<string> from another class in c#

我有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; }

    }
}

我想从placetovisit类将项目添加到Gallery。 为此,该怎么办?

实际上,我想为每个对象添加一个照相馆。 但是我在OOP方面不是很好。 此时此刻我可以接受这个概念,还是需要更改这个概念。 如果我可以使用该类,那么如何从placestovisit类将项目添加到Gallery中?

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");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM