示例#1
0
        public static bool TryParse(string figurePartString, [NotNullWhen(true)] out FigurePart?figurePart)
        {
            figurePart = null;

            string[] split = figurePartString.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
            if (split.Length < 2)
            {
                return(false);
            }

            if (!H.TryGetFigurePartType(split[0], out FigurePartType partType))
            {
                return(false);
            }
            if (!int.TryParse(split[1], out int partId))
            {
                return(false);
            }

            var figurePartTemp = new FigurePart(partType, partId);

            for (int i = 2; i < split.Length; i++)
            {
                if (!int.TryParse(split[i], out int color))
                {
                    return(false);
                }
                figurePartTemp.Colors.Add(color);
            }

            figurePart = figurePartTemp;
            return(true);
        }
示例#2
0
        public static FigurePart Parse(string figurePartString)
        {
            string[] split = figurePartString.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
            if (split.Length < 1)
            {
                throw new FormatException("Empty figure part");
            }
            if (split.Length < 2)
            {
                throw new FormatException($"Figure part '{split[0]}' has no ID");
            }

            var partType = H.GetFigurePartType(split[0]);

            if (!int.TryParse(split[1], out int partId))
            {
                throw new FormatException($"Couldn't parse ID '{split[1]}' for figure part '{split[0]}'");
            }

            var figurePart = new FigurePart(partType, partId);

            for (int i = 2; i < split.Length; i++)
            {
                if (!int.TryParse(split[i], out int color))
                {
                    throw new FormatException($"Couldn't parse color '{split[i]}' for figure part '{split[0]}'");
                }
                figurePart.Colors.Add(color);
            }

            return(figurePart);
        }