2015-11-05 33 views
-1

我有这个类:如何为公共const字符串变量赋值?

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 = [email protected]; 
       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左手边的任务必须是一个变量,财产或索引器

我怎么可以错误?

+2

如果你想为它指定一个值,你为什么要把这个变量作为一个const?你打算这是静态的吗? – LarsTech

+0

'... public const string variable'如果它是const,它不是一个变量,反之亦然。错误消息中的关键字是*变量* – Plutonix

+0

使它成为'readonly'而不是'const'。 –

回答

3

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

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

class Youtube_Video_Information 
{ 
    public readonly string ytKey; 

    public Youtube_Video_Information(string ytKey) 
    { 
     this.ytKey = ytKey; 
    } 
}