C#将值附加到字符串[]不起作用

时间:2018-08-01 18:06:06

标签: c# list add

因此,我尝试将值追加到列表(在Json []中),该列表为空或其中包含项目。因此,我检查对象中的列表是否包含项目,如果该项目不存在,则创建一个新项目,如果存在,则重写其值。这是代码:

if (e.Key == Key.Enter)
{
    // When the user pressed enter, do action
    Team selected_team = teams.Where(t => t.team_number == Convert.ToInt32(inp_team_number.Text)).FirstOrDefault();

    if (selected_team != null)
    {
        // when the team number is given, go try and find the data of them
        Results team_results = results.Where(r => r.team_number == Convert.ToInt32(inp_team_number.Text)).FirstOrDefault();

        int index = (Convert.ToInt32(gtk_input.Name.Substring(gtk_input.Name.Length - 1)) - 1);

        // Check if the item in the list exists
        if (index < team_results.results[inp_tour_part.SelectedIndex].gtks.Length && team_results.results[inp_tour_part.SelectedIndex].gtks[index] != null)
        {
            if (regexColon.Match(gtk_input.Text).Success == true)
            {
                team_results.results[inp_tour_part.SelectedIndex].gtks[(Convert.ToInt32(gtk_input.Name.Substring(gtk_input.Name.Length - 1)) - 1)] = gtk_input.Text; // Give the new value
            }
            else
            {
                MessageBox.Show("Wrong value.", "An error occured", MessageBoxButton.OK, MessageBoxImage.Warning);
                team_results.results[inp_tour_part.SelectedIndex].gtks[(Convert.ToInt32(gtk_input.Name.Substring(gtk_input.Name.Length - 1)) - 1)] = "00:00"; // Give the default value
            }
        }
        else
        {
            if (regexColon.Match(gtk_input.Text).Success == true)
            {
                team_results.results[inp_tour_part.SelectedIndex].gtks.Append(gtk_input.Text); // Give the new value
            }
            else
            {
                MessageBox.Show("Wrong value.", "An error occured", MessageBoxButton.OK, MessageBoxImage.Warning);
                team_results.results[inp_tour_part.SelectedIndex].gtks.Append("00:00"); // Give the default value
            }
        }

        SaveResults(results);
        // Move to the next UI element
        MoveToNextUIElement(e);
    }
    else
    {
        MessageBox.Show("Something went somewhere wrong.", "An error occured", MessageBoxButton.OK, MessageBoxImage.Warning);
    }
}

现在,重写项目可以很好地工作,但是当列表为空(默认)或项目不存在时,并且需要添加/添加新值时,它就不会死掉并且不会不会抛出任何错误...而且它也不会将值添加到我的json中,现在当我为此初始化新对象时,它看起来像以下内容:

team_results = new Results()
{
    team_number = selected_team.team_number,
    results = new Result[2] { new Result{ }, new Result { } } // Fixed length of array for the results. TODO: Needs fix.
};

模型如下:

namespace RittensportRekenSoftware.Models
{
    public class Results
    {
        public int team_number { get; set; }
        public Result[] results { get; set; }
    }

    public class Result
    {
        public string given_start_time { get; set; }
        public string connection_to_start { get; set; }
        public string start_kp { get; set; }
        public string stop_kp { get; set; }
        public int missed_controls { get; set; }
        public float km { get; set; }
        public string[] gtks { get; set; }
    }
}

现在我只需要json中的字符串列表,但是我不知道如何实现此目的...

4 个答案:

答案 0 :(得分:2)

如果必须,则可以使用Array.Resize()方法来调整数组的大小。请在此处查看documentation

int[] array = new int[] { 1, 2, 3 };
Array.Resize(ref array, 5);
array[3] = 4;
array[4] = 5;

但是强烈建议使用List<T>而不是数组。毕竟List<T>在幕后使用了数组 ,因此您可以获得了数组的所有功能,同时也消除了大多数缺点。

答案 1 :(得分:0)

您可以改为使用列表。因此,实例化列表时无需知道数组大小。

答案 2 :(得分:0)

为什么不更改模型,使它们实现List而不是数组。在每个模型的构造函数中,只需初始化空列表(或其他操作,具体取决于您的情况)

namespace RittensportRekenSoftware.Models
{
    public class Results
    {
        public int team_number { get; set; }
        public List<Result> results { get; set; }

        public Results() {
            results = new List<Result>();
        }
    }

    public class Result
    {
        public string given_start_time { get; set; }
        public string connection_to_start { get; set; }
        public string start_kp { get; set; }
        public string stop_kp { get; set; }
        public int missed_controls { get; set; }
        public float km { get; set; }
        public List<string> gtks { get; set; }

        public Result() {
            gtks = new List<string>();
        }
    }
}

然后,当您拥有模型时,可以将其添加到每个列表中,如下所示:

Results r = new Results();
r.results.Add(new Result()); // or other `result` object here

Result r = new Result();
r.gtks.Add("My String"); // or other `string` here

答案 3 :(得分:0)

我认为您可以实现一种基于原始数组创建新数组的方法。然后,您将能够使用结果数组覆盖该原始数组(由该新方法返回)。

示例代码如下:

var test = new string[1] { "Test string 1" };

test = AddItemToArray(test, "Test string 2");


private static string[] AddItemToArray(string[] original, string item)
{
    var result = new string[original.Length + 1];

    for (int i = 0; i < original.Length; i++)
    {
        result[i] = original[i];
    }

    result[result.Length - 1] = item;

    return result;
}
相关问题