C# 将项目添加到列表中
在VisualStudio中,如何设置用户创建的对象的属性 这是我的密码:C# 将项目添加到列表中,c#,list,token,C#,List,Token,在VisualStudio中,如何设置用户创建的对象的属性 这是我的密码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Android.App; using Android.Content; using Android.OS; using Android.Runtime; using Android.Views; using Android.Widget;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Gms.Maps.Model;
namespace SimpleMapDemo
{
class MapLocation
{
public MapLocation()
{
}
public LatLng Location;
public BitmapDescriptor icon;
public String Snippet;
public String Title;
}
}
我想添加这些项目的列表,我已经完成了以下代码:
private List<MapLocation> MapLocationList = new List<MapLocation>();
MapLocation MapLocationItem = new MapLocation();
MapLocationItem.Title = "Title";
可以为我提供一些帮助吗?您无法访问(从而分配或检索)类主体中的实例(称为MapLocationItem
)。这只有在构造函数、函数、getter或setter(可能我忘记了一个或两个)中才可能实现
class类main类
{
//定义类主体中的地图位置列表
//它是私有的,因此只能从类本身中获得
//在其上添加了下划线“u”,这在私有MMeber中非常常见
私有列表_mapLocationList=新列表();
公共类()
{
//创建MapLocation的实例,仅在构造函数范围内有效
MapLocation项=新的MapLocation();
//设置实例的属性
item.Title=“Title”;
//将MapLocation实例“项”添加到MapLocation列表中
_mapLocationList.Add(项目);
}
}
您需要设置属性。执行以下操作:
class MapLocation
{
public MapLocation()
{
}
public LatLng Location {get; set;}
public BitmapDescriptor icon {get; set;}
public String Snippet {get; set;}
public String Title {get; set;}
}
}
您可以使用构造函数或,以下是两种方法:
class MapLocation
{
// constructor
public MapLocation()
{
MapLocationList = new List<MapLocation>();
MapLocation MapLocationItem = new MapLocation();
MapLocationItem.Title = "Title";
MapLocationList.Add(MapLocationItem);
}
// collection initializer
private List<MapLocation> MapLocationList = new List<MapLocation>()
{
// object initializer
new MapLocation
{
Title = "Title"
}
};
public string Title{get;set;}
}
类映射位置
{
//建造师
公共地图位置()
{
MapLocationList=新列表();
MapLocation MapLocationItem=新的MapLocation();
MapLocationItem.Title=“Title”;
MapLocationList.Add(MapLocationItem);
}
//集合初始值设定项
私有列表MapLocationList=新列表()
{
//对象初始值设定项
新地图位置
{
Title=“Title”
}
};
公共字符串标题{get;set;}
}
使用构造函数或语法。显示这三行的周围代码。@CodeCaster:根据编译器错误,我假设它在类本身的主体中。应该清楚的是,不能直接访问主体中的实例。@Tim显示类映射位置
,我不相信OP想要在同一个类中实例化一个。正如你在我删除的答案中所看到的,我确实相信这三行是粘贴在其他类中的,因此我的问题是::-)
class MapLocation
{
public MapLocation()
{
}
public LatLng Location {get; set;}
public BitmapDescriptor icon {get; set;}
public String Snippet {get; set;}
public String Title {get; set;}
}
}
class MapLocation
{
// constructor
public MapLocation()
{
MapLocationList = new List<MapLocation>();
MapLocation MapLocationItem = new MapLocation();
MapLocationItem.Title = "Title";
MapLocationList.Add(MapLocationItem);
}
// collection initializer
private List<MapLocation> MapLocationList = new List<MapLocation>()
{
// object initializer
new MapLocation
{
Title = "Title"
}
};
public string Title{get;set;}
}