Why some variable in a new form i'm getting warning that they are never assigned?
这是新表单的顶部:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | 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是一个新表单,通过变量upload,我可以访问upload设计器中的控件。
上传表单中没有代码,只有我将其设置为public的控件。
我的问题是,为什么我看到上传下面的绿线说它没有被分配,将是空的?你可以在我正在做的构造函数中看到,例如:
1 | upload.comboBox1.Items.Add(item); |
我不会出错的。没有例外。我只是想知道我收到这个警告是不是件坏事?我对upload var所做的就是访问upload表单的控件。
下面是一个简单的例子,您可以做什么,也不能做什么:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | 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; } |