如何为变量分配更多内存

时间:2014-03-03 18:26:20

标签: c# memory console-application windows-ce

我创建了一个c#程序,它接受扫描输入,然后从输入中获取信息。我注意到的是,对于较大的字符串,我的程序由于某种原因将字符串分成两部分(不是一半),这搞砸了我获取信息的方式。我的字符串中也包含十六进制值。

例如,当我将代码扫描到我的控制台时,它会读取字符串

[)>065JUN1234567892300167Q205GT21L123 ABC06P123456787Q100PL7Q10PKQ1006P98356877Q100PL7Q5PKQ2006P235265437Q200PL7Q40PKQ5

但它将该字符串拆分为:

[)>065JUN1234567892300167Q205GT21L123 ABC06P123456787Q100PL7Q10PKQ1006P98356877Q100"

PL7Q5PKQ2006P235265437Q200PL7Q40PKQ5

知道如何解决这个问题,或者为读取输入扫描控制台的变量分配更多内存?

这是我的代码,它有点长。

static void Main(string[] args)
    {
        Console.WriteLine("Please enter the date and lane number");
        Console.WriteLine("like so: ddmmyylanenumber.");
        string lanenum = Console.ReadLine();
        Console.WriteLine("When done scanning, please type");
        Console.WriteLine("\"finish\" into the console.");
        string scanInput;

        do
        {
            Console.Write("Scan now:");
            scanInput = Console.ReadLine();

                //The number before "JUN" identifies the type of label it is.
                int posOfJUN = scanInput.IndexOf("JUN");
                //Finding the type of label
                string typeOfLabel = scanInput.Substring(posOfJUN - 1, 1);
                string label;
                if (typeOfLabel == "5")
                {
                    label = "mixed";
                }
                else if (typeOfLabel == "1")
                {
                    label = "individual";
                }
                else
                {
                    label = null;
                }

                switch (label)
                {
                    case "individual":
                        partNumber = scanInput.Substring(8, 8);

                        int posOfQ1 = scanInput.IndexOf("Q");
                        int posOf1JUN = scanInput.IndexOf("1JUN");
                        //Quantity of the pack
                        total = scanInput.Substring(posOfQ1 + 1, posOf1JUN - posOfQ1 - 1 - 1);
                        //number of packs is 1 because individual
                        numOfPacks = "1";
                        dunsNumber = scanInput.Substring(posOf1JUN + 4, 9);
                        //used to find the duns and serial number
                        posOf20L = scanInput.IndexOf("20L");
                        posOf21L = scanInput.IndexOf("21L");

                        //Setting the serial number
                        if (posOf21L == -1 || posOf20L < posOf21L)
                        {
                            serialNumber = scanInput.Substring(posOf1JUN + 4, posOf20L - posOf1JUN - 4 - 1);
                        }
                        else if (posOf20L == -1 || posOf21L < posOf20L)
                        {
                            serialNumber = scanInput.Substring(posOf1JUN + 4, posOf21L - posOf1JUN - 4 - 2);
                        }
                        else
                        {
                            serialNumber = null; //else clause if serial number can't be created
                            Console.WriteLine(new ArgumentException("Error obtaining Serial Number"));
                        }

                        partObject part2 = new partObject(partNumber, total, numOfPacks);

                        newPacks = int.Parse(numOfPacks);
                        total = total.ToString();
                        newtotal = int.Parse(total);
                        part2.callSQL(partNumber, newtotal, newPacks, dunsNumber, serialNumber, lanenum);
                        break;

                    case "mixed":
                        posOfJUN = scanInput.IndexOf("JUN");
                        dunsNumber = scanInput.Substring(posOfJUN + 3, 9);
                        int posOf7Q = scanInput.IndexOf("7Q");
                        posOf20L = scanInput.IndexOf("20L");
                        posOf21L = scanInput.IndexOf("21L");

                        //Finding serial number
                        serialNumber = scanInput.Substring(posOfJUN + 3, posOf7Q - posOfJUN - 3);

                        //The following lines are to find how many different parts are in the mixed load. 
                        posOfPK = scanInput.IndexOf("PK");
                        int PKTemp;
                        int parts = 1;
                        //Each time a "PK" is seen, it means there is another part so the count increments.
                        while (scanInput.IndexOf("PK", posOfPK + 1) != -1)
                        {
                            PKTemp = scanInput.IndexOf("PK", posOfPK + 1);
                            posOfPK = PKTemp;
                            parts++;
                        }
                        //Creating an array of size "parts"
                        int posOf06 = scanInput.IndexOf("06");
                        int temp06 = scanInput.IndexOf("06", posOf06 + 2);
                        posOf06 = temp06;
                        int indexOfP = scanInput.IndexOf("P", posOf06 + 1);
                        partNumber = scanInput.Substring(indexOfP + 1, 8);

                        posOfPK = scanInput.IndexOf("PK", indexOfP);
                        posOfPL = scanInput.IndexOf("PL", indexOfP);
                        posOf7Q1 = scanInput.IndexOf("7Q", indexOfP);
                        partObject[] arrayOfParts = new partObject[parts];

                        for (int i = 0; i < parts; i++)
                        {

                            //Finds the different values, creates an object and puts it into the array of parts
                            partNumber = scanInput.Substring(indexOfP + 1, 8);
                            total = scanInput.Substring(posOf7Q1 + 2, posOfPL - posOf7Q1 - 2);
                            numOfPacks = scanInput.Substring(posOfPL + 5, posOfPK - posOfPL - 5);
                            arrayOfParts[i] = new partObject(partNumber, total, numOfPacks);

                            //resetting the variables for the next iteration, so a new object can be created with the next part
                            posOf06 = scanInput.IndexOf("06", temp06 + 1);
                            indexOfP = scanInput.IndexOf("P", posOf06 + 1);
                            temp06 = posOf06;
                            posOfPK = scanInput.IndexOf("PK", indexOfP);
                            posOfPL = scanInput.IndexOf("PL", indexOfP);
                            posOf7Q1 = scanInput.IndexOf("7Q", indexOfP);

                            //putting each object into SQL
                            newtotal = int.Parse(total);
                            newPacks = int.Parse(numOfPacks);
                            serialNumber = serialNumber + "P" + (i + 1);
                            arrayOfParts[i].callSQL(partNumber, newtotal, newPacks, dunsNumber, serialNumber, lanenum);

                        }


                        break;
                    default:
                        break;
                }

            }
        } while (scanInput != "finish");
    }

从代码的开头,完整的字符串永远不会存在。

1 个答案:

答案 0 :(得分:0)

如果您调试程序,我认为您会发现string就在那里。 我不确定为什么您认为stringsplit ... System.String只能容纳一个string。在某些时候你得到string[]

在此行scanInput = Console.ReadLine();之后放置一个断点,并在Visual Studio中将鼠标悬停在scanInput上,它会显示整个字符串....或者只是打印出字符串以显示它是在那里。

编辑:如果您使用的是十六进制,请尝试查找导致ascii table异常的十六进制值。也许十六进制值导致回车或换行。