簡體   English   中英

在C#中使用變量時List.Add方法出現問題

[英]Issues with List.Add method when using a variable in C#

我目前無法使用作為字符串添加到列表中的項目的變量。 我稍后再拉列表時,它只是返回null:

public class JobStatus
{

    public static string _JobURI;
    public static string currentStatus = "no job";

    public static void checkStatus()
    {

        ...
        //define job URI
        List<string> jobURIs = new List<string>();
        jobURIs.Add(_JobURI);

但是,當我插入如下所示的字符串值而不是變量時,它將正確地添加到列表中:

//define job URI
List<string> jobURIs = new List<string>();
jobURIs.Add("new item name");

我不確定我缺少什么。

根據您發布的代碼, _JobsURI為空的_JobsURI是您在此處聲明了它:

public static string _JobURI;

但是您永遠不會為其分配值。 根據文檔 :“已聲明但尚未分配值的字符串為null 。”

嘗試為_JobURI分配一個值,然后將其添加到List<string>

public static string _JobURI = "Some string here.";

我想通了,開始出現程序員錯誤。 我在同一類中使用了Get Set方法,並聲明了上面所有建議的變量:

public static string currentStatus = "no job";

        private static string joburi = "";
        public static string JobURI
        {
            get { return joburi; }
            set { joburi = value; }
        }

謝謝您的幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM