如何为公共常量字符串变量赋值
本文关键字:字符串 变量 赋值 常量 | 更新日期: 2023-09-27 18:20:32
我有这个类:
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赋值的左侧必须是变量、属性或索引器
我该如何解决这个错误?
如何为公共常量字符串变量赋值?
你不能。Const值不能在运行时分配值。如果您需要能够在运行时分配值,请将值传递给构造函数调用,并使成员成为readonly
。
class Youtube_Video_Information
{
public readonly string ytKey;
public Youtube_Video_Information(string ytKey)
{
this.ytKey = ytKey;
}
}