如何为公共const字符串变量赋值?

时间:2015-11-05 00:25:37

标签: c# .net winforms

我有这堂课:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Web.Script.Serialization;
using System.Json;
using System.Runtime.Serialization.Json;
using System.Web.Helpers;

namespace Automatic_Record
{
    class Youtube_Video_Information
    {
        public const string ytKey = "";


        public int Width { get; set; }
        public int Height { get; set; }
        public int Duration { get; set; }
        public string Title { get; set; }
        public string ThumbUrl { get; set; }
        public string BigThumbUrl { get; set; }
        public string Description { get; set; }
        public string VideoDuration { get; set; }
        public string Url { get; set; }
        public DateTime UploadDate { get; set; }

        public bool YouTubeImport(string VideoID)
        {
            try
            {
                WebClient myDownloader = new WebClient();
                myDownloader.Encoding = System.Text.Encoding.UTF8;

                string jsonResponse = myDownloader.DownloadString("https://www.googleapis.com/youtube/v3/videos?id=" + VideoID + "&key=" + ytKey + "&part=snippet");
                JavaScriptSerializer jss = new JavaScriptSerializer();
                var dynamicObject = Json.Decode(jsonResponse);
                var item = dynamicObject.items[0].snippet;

                Title = item.title;
                ThumbUrl = item.thumbnails.@default.url;
                BigThumbUrl = item.thumbnails.high.url;
                Description = item.description;
                UploadDate = Convert.ToDateTime(item.publishedAt);

                jsonResponse = myDownloader.DownloadString("https://www.googleapis.com/youtube/v3/videos?id=" + VideoID + "&key=" + ytKey + "&part=contentDetails");
                dynamicObject = Json.Decode(jsonResponse);
                string tmp = dynamicObject.items[0].contentDetails.duration;
                Duration = Convert.ToInt32(System.Xml.XmlConvert.ToTimeSpan(tmp).TotalSeconds);

                Url = "http://www.youtube.com/watch?v=" + VideoID;

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool VimeoImport(string VideoID)
        {
            try
            {
                WebClient myDownloader = new WebClient();
                myDownloader.Encoding = System.Text.Encoding.UTF8;

                string jsonResponse = myDownloader.DownloadString("http://vimeo.com/api/v2/video/" + VideoID + ".json");
                JavaScriptSerializer jss = new JavaScriptSerializer();
                var dynamicObject = Json.Decode(jsonResponse);
                var item = dynamicObject[0];

                Title = item.title;
                Description = item.description;
                Url = item.url;
                ThumbUrl = item.thumbnail_small;
                BigThumbUrl = item.thumbnail_large;
                UploadDate = Convert.ToDateTime(item.upload_date);
                Width = Convert.ToInt32(item.width);
                Height = Convert.ToInt32(item.height);
                Duration = Convert.ToInt32(item.duration);

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}

在form1构造函数中,我做了:

Youtube_Video_Information.ytKey = "myapikey";
Youtube_Video_Information yvi = new Youtube_Video_Information();
yvi.YouTubeImport("ee-myvideoid");

问题是我收到了错误:

Youtube_Video_Information.ytKey

错误3作业的左侧必须是变量,属性或索引器

我该如何解决错误?

1 个答案:

答案 0 :(得分:3)

  

如何为公共const字符串变量赋值?

你做不到。 Const值不能在运行时分配值。如果您需要能够在运行时分配值,请将值传递给构造函数调用并使成员readonly

class Youtube_Video_Information
{
    public readonly string ytKey;

    public Youtube_Video_Information(string ytKey)
    {
        this.ytKey = ytKey;
    }
}
相关问题