0

I have two string/text files : "1.dll" and "1a.dll" - 1.dll contains "Order ID" and "cartID"(separated with enter '/n') - 1a.dll is database witdh "id" and "name" (separated with enter '/n')

I am splitting strings to string array. Then I'm separating each array string in two strings. One with even number position and other with odd number position. After splitting both files, I have 4 array strings which I'm displaying to 4 ListBoxes. - 2 arrays from 1.dll are displying as they should - 2 arrays from 1a.dll are missing some values. Here is the screenshot with problem

//Load and split "1.dll" > create 2 array strings. orderID=odd # position and cartID=even # position
        string a = File.ReadAllText(@"order/1.dll");
        string[] aa = a.Split('\n');
        aa = aa.Select(s => (s ?? "").Trim()).ToArray();
        string[] orderID = new string[aa.Length];
        string[] cartID = new string[aa.Length];

        int Dial1 = 0;
        int Dial2 = 0;
        for (int i = 0; i < aa.Length; i++)
        {
            if (i % 2 == 0)
            {
                orderID[Dial1] = aa[i];
                Dial1++;
            }
            else
            {
                cartID[Dial2] = aa[i];
                Dial2++;
            }
        }
        for (int j = 0; j < aa.Length / 2; j++)
        {
            AddToCartList.Items.Add(cartID[j]);
            OrderIDList.Items.Add(orderID[j]);
        }
//Load and split "1a.dll" > create 2 array strings. id=odd # position and game=even # position

        string b = File.ReadAllText(@"order/1a.dll");
        string[] bb = b.Split('\n');
        bb = bb.Select(s => (s ?? "").Trim()).ToArray();
        string[] id = new string[bb.Length / 2];
        id = id.Select(s => (s ?? "").Trim()).ToArray();
        string[] name = new string[bb.Length / 2];
        name = name.Select(s => (s ?? "").Trim()).ToArray();
        string combindedString = string.Join("\n", bb.ToArray());
        MessageBox.Show(combindedString);

        int Dial3 = 0;
        int Dial4 = 0;
        for (int i = 0; i < bb.Length / 2; i++)
        {
            if (i % 2 == 0)
            {
                id[Dial3] = bb[i];
                Dial3++;
            }
            else
            {
                name[Dial4] = bb[i];
                Dial4++;
            }
        }
        for (int j = 0; j < bb.Length / 2; j++)
        {
            IDlist.Items.Add(id[j]);
            nameList.Items.Add(name[j]);
        }




        for (int i = 0; i < id.Length; i++)
        {
            if (orderID[0] == id[i])
            {
                textBox1.Text = name[0];
            }
            if (orderID[2] == id[i])
            {
                textBox2.Text = name[1];
            }
            if (orderID[2] == id[i])
            {
                textBox3.Text = name[1];
            }
        }
1
  • One thing I see is that you have 2 if (orderID[2] == id[i]) lines, I assume you want one to say if (orderID[1] == id[i]) instead, also one of your name[1] you probably want as name[2] in the same code block (the last for loop in the posted code). Commented Jul 15, 2016 at 17:58

2 Answers 2

1

In the second loop you run the loop for half of the content of the bb array

for (int i = 0; i < bb.Length / 2; i++)

this should be

for (int i = 0; i < bb.Length; i++)

But apart from that this code could be changed a lot using the generic List<T> instead of creating so many temporary arrays,

For example the first loop could be written as

// ReadAllLines already returns your text file splitted at newlines
string[] aa = File.ReadAllLines(@"order/1.dll");

// With lists you don't need to create a fixed size array in advance...
List<string> orders = new List<string>();
List<string> carts = new List<string>();

// Your array could be iterated two items at times 
// Of course here a check for even number of items should be
// added here....
for (int i = 0; i < aa.Length; i += 2)
{
     orders.Add(aa[i]);
     carts.Add(aa[i+1]);
}
// The collections have the possibility to add a range of items
// without you writing a loop 
AddToCartList.Items.AddRange(carts.ToArray());
OrderIDList.Items.AddRange(orders.ToArray());
Sign up to request clarification or add additional context in comments.

2 Comments

Works perfect! I will do the same with 1a.dll. In that situation do you have a suggestion maybe how to do this with lists : ' for (int i = 0; i < id.Length; i++) // { // if (orderID[0] == id[i]) // { // textBox1.Text = name[i];'
That part is not really clear to me. You have three textboxes but I don't understand the rules to set the Text property. However the Lists could be used (iterated over with for or foreach) as they were arrays so that code don't need to be changed just because you have lists instead of arrays.
0

Mistake was here :

    int Dial3 = 0;
int Dial4 = 0;
for (int i = 0; i < bb.Length / 2; i++)

Length was supposed to be bb.Length without / 2;

1 Comment

I would just delete the question. This is not something that would be of value to other in the future. I am not a moderator. That is just one opinion.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.