C# 为什么有些变量采用新的形式i';我得到警告说他们从未被分配?
这是新表单的顶部:C# 为什么有些变量采用新的形式i';我得到警告说他们从未被分配?,c#,.net,winforms,C#,.net,Winforms,这是新表单的顶部: namespace test { public partial class Youtube_Uploader : Form { public class ComboboxItem { public string Text { get; set; } public object Value { get; set; } public override stri
namespace test
{
public partial class Youtube_Uploader : Form
{
public class ComboboxItem
{
public string Text { get; set; }
public object Value { get; set; }
public override string ToString()
{
return Text;
}
}
YouTubeService service;
string devKey = "";
string apiKey = "";";
string userName = "";
string Password = "";
string feedUrl = "";
string FileNameToUpload = "";
string[] stringProgressReport = new string[5];
long totalBytes = 0;
DateTime dt;
Upload upload;
public Youtube_Uploader()
{
InitializeComponent();
service = AuthenticateOauth(apiKey);
var videoCatagories = service.VideoCategories.List("snippet");
videoCatagories.RegionCode = "IL";
var result = videoCatagories.Execute();
for (int i = 0; i < result.Items.Count; i++)
{
ComboboxItem item = new ComboboxItem();
item.Text = result.Items[i].Snippet.Title;
item.Value = result.Items[i].Id;
upload.comboBox1.Items.Add(item);
}
upload.comboBox1.SelectedIndex = 0;
MakeRequest();
}
我没有犯错误。没有例外。
我只是想知道我得到这个警告是不是一件坏事?
我对upload var所做的一切就是访问upload表单的控件。以下是一个简单示例中可以做和不能做的事情:
void Main()
{
Foo f1 = new Foo("good",6);
Foo f2;
string temp_string;
// You can do this
temp_string = f1.S;
// Bot not this
// temp_string = f2.S; --> Use of unassigned local variable 'f2'
// You can also do this:
int temp_int = Foo.I;
// But not
// temp_int = f1.I;
// You can do
bool b = TestFoo(f1);
// But not
// b = TestFoo(f2); --> Use of unassigned local variable 'f2'
}
public class Foo {
// static class property, can be accessed with "Foo.I"
public static int I {get; set;}
// instance property, can be accessed with "f1.S"
public string S { get; set; }
public Foo(string s, int i = 0) {
S = s;
I = i;
}
}
public bool TestFoo(Foo foo) {
return foo != null;
}
您从未将
upload
设置为任何内容。@TonyLee实际上,您会得到一个编译错误,说Member'upload.comboBox1'不能通过实例引用访问;改用类型名称限定它
。
void Main()
{
Foo f1 = new Foo("good",6);
Foo f2;
string temp_string;
// You can do this
temp_string = f1.S;
// Bot not this
// temp_string = f2.S; --> Use of unassigned local variable 'f2'
// You can also do this:
int temp_int = Foo.I;
// But not
// temp_int = f1.I;
// You can do
bool b = TestFoo(f1);
// But not
// b = TestFoo(f2); --> Use of unassigned local variable 'f2'
}
public class Foo {
// static class property, can be accessed with "Foo.I"
public static int I {get; set;}
// instance property, can be accessed with "f1.S"
public string S { get; set; }
public Foo(string s, int i = 0) {
S = s;
I = i;
}
}
public bool TestFoo(Foo foo) {
return foo != null;
}