为什么新形式的某个变量会收到警告他们从未被分配?

时间:2015-08-10 03:51:37

标签: 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 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.comboBox1.Items.Add(item);

我没有收到错误。也不例外。 我只是想知道我收到这个警告是不是一件坏事? 我所做的上传var就是访问上传表单的控件。

1 个答案:

答案 0 :(得分:1)

以下是您可以做的事情,可以做一个简单的例子:

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;
}