Introduction
In this article I will describe how to swap the values of two arrays. Previously, I have always swapped the values of variables. But now I have decided, why not interchange the values of two arrays. So in this article we are doing something different.
Use the following procedure to create it.
Step 1
First of all you must create a new Windows Store Application.
- Open Visual Studio 2012
- "File" -> "New" -> "Project..."
- Choose "Template" -> "Visual C#" -> "Window Store app"
- Choose "Blank App (XAML)" then rename the application
Step 2
Write the following XAML code in "Mainpage.Xaml" (that is available in Solution Explorer):
<Page
x:Class="swap_array_app.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:swap_array_app"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="Red">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="275*"/>
<ColumnDefinition Width="321*"/>
<ColumnDefinition Width="770*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="60*"/>
<RowDefinition Height="41*"/>
<RowDefinition Height="42*"/>
<RowDefinition Height="56*"/>
<RowDefinition Height="44*"/>
<RowDefinition Height="45*"/>
<RowDefinition Height="43*"/>
<RowDefinition Height="50*"/>
<RowDefinition Height="56*"/>
<RowDefinition Height="331*"/>
</Grid.RowDefinitions>
<TextBlock Text="First array before swapping " Grid.Column="1" Grid.Row="1" HorizontalAlignment="Left" Foreground="White" FontSize="20" FontWeight="ExtraBold" Width="258"></TextBlock>
<TextBlock x:Name="text1" Grid.Column="1" Grid.Row="2" Width="200" HorizontalAlignment="Left" Foreground="White" FontSize="20"></TextBlock>
<TextBlock Text="Second array before swapping " Grid.Column="1" Grid.Row="3" HorizontalAlignment="Left" Foreground="White" FontSize="20" FontWeight="ExtraBold" Width="285"></TextBlock>
<TextBlock x:Name="text2" Grid.Column="1" Grid.Row="4" Width="285" HorizontalAlignment="Left" Foreground="White" FontSize="20"></TextBlock>
<TextBlock Text="First array After swapping " Grid.Column="1" Grid.Row="5" HorizontalAlignment="Left" Foreground="White" FontSize="20" FontWeight="ExtraBold" Width="258" Visibility="Collapsed" Name="hidetextblock1"></TextBlock>
<TextBlock x:Name="text3" Grid.Column="1" Grid.Row="6" Width="200" HorizontalAlignment="Left" Foreground="White" FontSize="20"></TextBlock>
<TextBlock Text="Second array After swapping " Grid.Column="1" Grid.Row="7" HorizontalAlignment="Left" Foreground="White" FontSize="20" FontWeight="ExtraBold" Width="285" Visibility="Collapsed" Name="hidetextblock2" ></TextBlock>
<TextBlock x:Name="text4" Grid.Column="1" Grid.Row="8" Width="200" HorizontalAlignment="Left" Foreground="White" FontSize="20"></TextBlock>
<Button x:Name="button1" Content="Show array After swapping" Background="Yellow" Foreground="Black" Grid.Column="2" Grid.Row="4" Click="button1_Click"></Button>
</Grid>
</Page>
Step 3
Now write the following C# code for the button within "Mainpage.Xaml.cs".
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
namespace swap_array_app
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
showarray();
}
int i,j;
int[] arr1 = { 10, 20, 30, 40, 50 };
int[] arr2 = { 15, 30, 45, 60, 75 };
int[] arr3 = new int[5];
public void showarray()
{
for (i = 0; i < arr1.Length; i++)
{
text1.Text += arr1[i] + " ";
}
for (j = 0; j < arr2.Length; j++)
{
text2.Text += arr2[j] + " ";
}
}
private void button1_Click(object sender, RoutedEventArgs e)
{
swaparry();
this.hidetextblock1.Visibility = Visibility.Visible;
this.hidetextblock2.Visibility = Visibility.Visible;
}
public void swaparry()
{
for (i = 0; i < arr1.Length; i++)
{
arr3[i] = arr1[i];
arr1[i] = arr2[i];
arr2[i] = arr3[i];
}
for (i = 0; i < arr1.Length; i++)
{
text3.Text += arr1[i] + " ";
}
for (j = 0; j < arr2.Length; j++)
{
text4.Text += arr2[j] + " ";
}
}
}
}
Step 4
Run your app.
After clicking on the button.