Add Item to Array

There is no Add method for basic arrays.
//Add an item to the end of an existing array
string[] ar1 = new string[] {“I“, “Like“, “To“}

// create a temporary array with an extra slot
// at the end

string[] ar2 = new string[ar1.Length + 1];

// add the contents of the ar1 to ar2 at position 0
ar1.CopyTo(ar2, 0);

// add the desired value
ar2.SetValue(“Code.”, ar1.Length);

// overwrite ar1 with ar2 the contents of ar1 should now be {“I”, “Like”, “To”, “Code.”}
ar1 = ar2;

Or:
Array.Resize(ref ar1, ar1.Length + 1);
Array.Copy(new object[] { “Code.” }, 0, ar1, ar1.Length – 1, 1);

Advertisements

Passing Values between User Controls using Events and Delegates

On a content page there are two user controls e.g. UC1 and UC2. Contents of UC1 are:
1. List box having list of States
2. List box having list of Cities in the State (Pre-condition: User selects State from ListBoxStates and respective cities in the selected state gets populated in the ListBoxCities)
3. Button control, which will be used for confirming the user’s selections. Let us name it as btnSelect.
Contents of UC2 are:
1. List of selected cities from UC1. Let us name it as ListBoxSelectedCities.
Our requirement is whenever user selects (multiple) cities from ListBoxCities and clicks on btnSelect in UC1 then the selected cities should get populated in ListBoxSelectedCities in UC2.

In UC1, we need to declare the following objects:

public delegate void PassSelectedValues(string[] selectedCities);
public event PassSelectedValues citiesSelected;

Now in UC1 from the button click event we can raise citiesSelected event, delegate of which will carry the required values which we need to pass in between UC1 and UC2. In button click event we have to create the list of selected cities from ListBoxCities and we have to raise the citiesSelected event like this:

citiesSelected(selectedCities);

Here selectedCities is an array of strings which we have to create by looping through the ListBoxCities.
We have to declare a public property in UC2 which can be used to set the ListBoxSelectedCities. For example in UC2:

public string[] SelectedCitiesList{
set { ListBoxSelectedCities.DataSource = value;
ListBoxSelectedCities.DataBind();
}
}

In the content page we have to declare the event handler for the event (citiesSelected) which we have raised from UC1. Thus in content page on page load we have to declare the handler of this event like this:

UC1.citiesSelected += new citiesUserControl.PassSelectedValues(passValuesHandlerMethod);

This handler method in the content page should have the same signature as of the delegate’s signature defined in UC1.

protected void passValuesHandlerMethod(string[] selectedCities){ }

Now from this method we can pass the value coming from UC1 to UC2 like this:

protected void passValuesHandlerMethod(string[] selectedCities){
UC2.SelectedCitiesList = selectedCities;
}

In this way whichever values we are selecting in UC1 will get passed to UC2.

Source: http://www.dotnetfunda.com/articles/article201.aspx