String array out of index C#
I am currently writing a little program that takes a string of numbers, splits them into lines and then writes them to a file. The problem is however all of my string arrays work just fine except this one and I cannot figure out why. The code is bellow and like I said I don't get why sysid is failing since sysidpart works just fine.
string[] sysid;
string[] sysidpart;
sysid = new string[10];
sysidpart = new string[sysintcount];
while (H < sysintcount)
{
sysidpart[H] = syscode.Substring((H * 4), 4);
if (I == 0)
{
sysid[J] = sysidpart[H];
H++;
I++;
}
else if (I <= 3)
{
sysid[J] = sysid[J] + "-" + sysidpart[H];
H++;
I++;
}
else if (I == 4)
{
J++;
I = 0;
}
}
I'm assu$$anonymous$$g that "J" is declared at a value of 0 somewhere...
Aside from that, does "sysintcount" ever reach a value greater than ~50? If so, your hard-coded string size of 10 for sysid would be exceeded.
J is declared earlier as 0 it is a counter for each one. As for sysintcount it can reach over 50 but unlikely I believe with the current settings it stops at 42.
What I am trying to do is take sysidpart[] and put them into lines of 4 (so sysidpart[1,2,3,4] are a line) and then store that line in sysid[]. This is mainly to clear it up in the text file so it looks more organized.
if you are starting your array from 0 (zero) then in less than condition dont take equals-to. may be it may give solution.
That is why I store the array counter on a seperate int like I, I do it to run these and like I mentioned I have similar things in this same script that all work fine but this one is giving my issues for some reason.
Answer by Rin · Mar 03, 2016 at 10:16 PM
I figured out the issue, I put some print commands in the loop to see how it would turn out and it seemed that it was not having the error on the first loop but on a later one so I added in an extra zero to sysid and it works perfectly. Thank you for the help guys seems the error was on my end.