【问题标题】:Adding an item to a list将项目添加到列表
【发布时间】:2014-01-10 10:39:34
【问题描述】:

在 Visual Studio 中,如何设置用户创建对象的属性?

这是我的代码:

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

这是我得到的错误:

Invalid token '=' in class, struct, or interface member declaration

我可以帮忙吗?

【问题讨论】:

  • 使用构造函数或collection initializer 语法。
  • 显示这三行的周边代码。
  • @CodeCaster:根据编译器错误,我假设它在类本身的主体中。
  • 应该清楚的是,不能直接在正文中访问实例。
  • @Tim 显示了MapLocation 类,我不相信 OP 想要在同一个类中实例化一个。正如您在我删除的答案中看到的那样,我确实相信这三行已粘贴在其他类中,因此是我的问题。 :-)

标签: c# list token


【解决方案1】:

您可以使用构造函数或collection/object initializer syntax,两种方式都可以:

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

【讨论】:

    【解决方案2】:

    您需要设置属性。执行以下操作:

    class MapLocation
        {
            public MapLocation()
            {
            }
    
            public LatLng Location {get; set;}
            public BitmapDescriptor icon {get; set;}
            public String Snippet {get; set;}
            public String Title {get; set;}
        }
    }
    

    【讨论】:

      【解决方案3】:

      您不能在类主体中访问(因此分配或检索)您的实例(称为MapLocationItem)。这只有在你在构造函数、函数、getter 或 setter 中时才有可能(可能我忘了一两个)。

      class MainClass
      {
          // Define the list of map locations in the body of the class
          // It is private thus only available from within the class itself
          // Added underscore '_' to it that is quite common for private mmebers
          private List<MapLocation> _mapLocationList = new List<MapLocation>();
      
          public MainClass()
          {
              // Create an instance of the MapLocation, only valid within the constructor scope
              MapLocation item = new MapLocation();
              // Set the property of the instance
              item.Title = "Title";
      
              // Adds the MapLocation instance 'item' to the list of MapLocations
              _mapLocationList.Add(item);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2020-06-23
        • 2019-11-29
        • 2017-09-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-14
        • 1970-01-01
        • 2020-11-11
        相关资源
        最近更新 更多