Tuesday, April 1, 2014

Object Array Logical Usage to Store Multiple values at Client side:

Object Array Logical Usage to Store Multiple values at Client side:
I was on a way of R & D on the requirement I had, at last I could able to find the solution for it, let us have a look below.
In the first screen1 below I have an textbox asking user to enter no. of Parties
In the screen 2 below, I have some details which need to be taken for each parties from users, as for now you can see 10 entered as No. Of Parties(user can enter even 100,1000 or any number), so I should able to take each party details(might be any no.) from user on next screens.
Question is how..??
See the screen 2 below where I have provided one drop down or combo box has values binded from 1-10(depend upon the first screen Parties# entered).
From the drop down, user can able to select each party and enter the details of each.
When Party 1 details are completely entered and user selected 2 party and start entering details.
We need to store first Party details and empty the text boxes to enter other party details.
When user want to come back & edit first party details or want to see first party details he entered then we shall able to re produce his entered result back for each party.
In more details Let us say user entered 1st  Party details & then entered 10th  Party details and again he want to edit 1st party details by selecting value as 1 in the dropdown, when he does so the previous firs party details shall be reflected back and he could able to edit.
Remember the entire operation must be performed at client side no database must involve.


In another way I meant to say If the no of parties in the first screen entered as 100 then I could able to store each 100 party details at client side with edit & restoring option.

Now let us start the Logic.

1)    Create the object with the fields need to be stored & declare object array globally.

      public  class MyCalss

       {

            public string Value1 { get; set; }
            public DateTime Value2 { get; set; }
            public int Value3 { get; set; }
            public int Value4 { get; set; }
            public int Value5 { get; set; }
            public string Value6 { get; set; }
  --------
  --------
}
Global Class array Declaration as :
MyCalss [] myClassDetails;
2)    In the party text box text changed event setting the object array length equaling to the no. of Party’s entered.
i)             private void txtParties_TextChanged(object sender, EventArgs e)
        {
                   if (!string.IsNullOrEmpty(txtParties.Text))
                   {
myClassDetails = new MyCalss [Convert.ToInt32(txtParties.Text)];
BindComboParties(); //binding the combobox or drop down with the values 1 to no. of parties entered
                  }
        }
          /// <summary>
        /// this will bind the party no's to combo box from 1 to no. of parties entered in the txtParties text box
        /// </summary>
ii)           private void BindComboParties()
        {
            cmbParties.Items.Clear();
for (int item = 1; item <= Convert.ToInt32(txtParties.Text); item++)
            {
                cmbParties.Items.Add(Convert.ToString(item));
            }            
            cmbParties.SelectedIndex = 0;
        }
/// <summary>
        /// When Vehicle Screen combobox Party selcted then Save the inputs in Vehcle class variable & Display in return navigation
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

3)    Now in the drop down selected index changed event write the logic to save & populate the details as below
int preViousSelectedIndex = 0;//declaring globally
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {        
                        myClassDetails [preViousSelectedIndex] =
            new MyCalss
            {
                Value1 = Convert.ToInt32(cmbParties.SelectedItem),
                Value2 = txtVehPlate.Text,
                Value3  = txtVehPlateSourceFrom.Text,
                 ------
            };
            if (vehDetails[Convert.ToInt32(cmbParties.Text) - 1] != null)
            {
                txtVehPlate.Text = myClassDetails [Convert.ToInt32(cmbParties.Text) - 1].Value1;
                txtVehPlateSourceFrom.Text = myClassDetails [Convert.ToInt32(cmbParties.Text) - 1].   Value2;
                   ------
            }
            else
            {
                txtVehPlate.Text = string.Empty;
                txtVehPlateSourceFrom.Text = string.Empty;
                ---Emptying all the text boxes for other party details
            }
            preViousSelectedIndex = Convert.ToInt32(cmbParties.Text) - 1;
        }
}
 

In The logic in the above Logic, I am storing the values in the object if it is null, else displaying the values in the textboxes & then emptying the text boxes for the next party values & setting the preViousSelectedIndex.

In more detail let us say for the first time I have entered the party details for the part number 1 & next I want to enter 2nd party details, So I will select the Party number 2 in the drop down, Here the  point  of interest is I am making this the exact time to store the object array.

In this case I will hit with comboBox1_SelectedIndexChanged Event here I am storing the details for the Party No. 1 in the object array myClassDetails [preViousSelectedIndex = 0] & emptying the text boxes.
Since preViousSelectedIndex is 0 for the first time it stores the object array at 0 index & then I am taking current Index value of combo box in the preViousSelectedIndex to store the values in the   object array with correct index (i.e. always 1 less since combo box index starts from zero and my object I am storing from index 0).

After once it gets complete saving data in to database can be done just looping object array.

So, friends that’s the small logic I wanted to share, I am very sure you might have other ideas but I felt this is the  way which I should proceed.
I have implemented this in Windows Mobile CE,Same logic can be implemented in web page completely at client side with Javascript.

I hope you have benefited, please put your precious comments & suggestions.

No comments: