如何解决这个问题? C#错误索引超出范围异常

时间:2019-05-23 10:27:10

标签: c# 2d-games

Here is the Error

第99行:

uint ID = uint.Parse(split[0].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)[0]);

从中读取EshopV2File.cs的文件

ID=2888
MoneyType=1

138008 equip|new Hello~to~you~in~our~server~all~that~is~new~click~to~wear.

文件EshopV2File.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Kingdom.Database
{
    public static class EShopV2File
    {
        public static void Load()
        {
            string[] text = File.ReadAllLines(Constants.EShopsV2Path);
            ShopFile.Shop shop = new ShopFile.Shop();
            for (int x = 0; x < text.Length; x++)
            {
                string line = text[x].Replace("_", " ");

                bool bound = false;
                if (line.Contains("present"))
                    bound = true;
                string[] split = line.Split('=');

                if (split[0] == "ID")
                {
                    uint id = uint.Parse(split[1]);
                    if (EShopFile.Shops.ContainsKey(id))
                        shop = EShopFile.Shops[uint.Parse(split[1])];
                    else
                    {
                        shop = new ShopFile.Shop();
                        shop.Items = new List<uint>();
                        shop.UID = id;
                        EShopFile.Shops.Add(id, shop);
                    }
                }
                else if (split[0] == "MoneyType")
                {
                    shop.MoneyType = (ShopFile.MoneyType)byte.Parse(split[1]);
                }
                else if (split[0].Contains("Item"))
                {

                    if (split[0].StartsWith("Item"))
                    {

                        uint ID = uint.Parse(split[1].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)[0]);
                        if (bound)
                        {
                            if (!shop.BoundItems.Contains(ID))
                                shop.BoundItems.Add(ID);
                        }
                        else
                        {
                            if (!shop.Items.Contains(ID))
                                shop.Items.Add(ID);
                        }
                    }
                    else
                    {
                        uint ID = uint.Parse(split[0].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)[0]);
                        if (bound)
                        {
                            if (!shop.BoundItems.Contains(ID))
                                shop.BoundItems.Add(ID);
                        }
                        else
                        {
                            if (!shop.Items.Contains(ID))
                                shop.Items.Add(ID);
                        }
                    }
                }
                else if (split[0].StartsWith("item"))
                {
                    try
                    {
                        uint ID = uint.Parse(split[1].Split(' ')[0]);
                        if (bound)
                        {
                            if (!shop.BoundItems.Contains(ID))
                                shop.BoundItems.Add(ID);
                        }
                        else
                        {
                            if (!shop.Items.Contains(ID))
                                shop.Items.Add(ID);
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.ToString());
                    }
                }
                else if (split[0].Length != 0 && split[0] != "[recommend]" && split[0] != "Amount")
                {
                    try
                    {
                        uint ID = uint.Parse(split[0].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)[0]);
                        if (bound)
                        {
                            if (!shop.BoundItems.Contains(ID))
                                shop.BoundItems.Add(ID);
                        }
                        else
                        {
                            if (!shop.Items.Contains(ID))
                                shop.Items.Add(ID);
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.ToString());
                    }
                }
            }
        }
    }
}

0 个答案:

没有答案