-->

Friday, March 28, 2014

Remove first element from array , Example of List and Resize method

Introduction

If you want to remove first element from an array. First of all, fill array with some values.Create a list collection and fill it with array list. Remove first element using RemoveAt(0) method , here 0 is the first index of array. After remove, must resize array using Resize( )  method. Again pass resized list into array.

Source Code

    <form id="form1" runat="server">
    <asp:Button ID="Button1" runat="server" Text="Animal" Width="100px"
        onclick="Button1_Click" ForeColor="Red" />
    <div>
   
        <asp:Label ID="Label1" runat="server" Text="Label" BackColor="Yellow"
            BorderStyle="Solid" Font-Underline="True" ForeColor="Blue"></asp:Label>
   
    </div>
    </form>

CodeBehind Code

#region remove_first_index
    protected void Button1_Click(object sender, EventArgs e)
    {
        string[] animal = new string[]
        {
            "Cow",
            "Monkey",
            "Black buffalo",
            "Donkey",
            "Yak"
        };

        Label1.Text = "animal array.........<br />";
        foreach (string s in animal)
        {
            Label1.Text += s + "<br />";
        }
        List<string> animallist = animal.ToList();
        animallist.RemoveAt(0);

        Array.Resize(ref animal, animal.Length - 1);

        for (int i = 0; i < animal.Length; i++)
        {
            animal[i] = animallist[i];
        }


        foreach (string s in animal)
        {
            Label1.Text += s + "<br />";
        }
    }
    #endregion

Code Generate the following output

Remove first element from array , Example of List and Resize method

Thursday, March 27, 2014

How to Implement Client-Side Validation in ASP.NET MVC

Implementing Client-Side Validation in an Asp.Net MVC application requires some app settings in the web.config file that must be true. These settings are used to enable two jquery i.e. jquery.validate.min.js and jquery.validate.unobtrusive.min.js in our project.

<appSettings>
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>

These setting are by default enabled in an MVC project so programmer just remember to check these lines if validation are not working at all. In Visual studio there is an in-built validation attribute mostly used in all the required fields.

[Required]
(Field to be required…..)

This attribute is defined in System.ComponentModel.DataAnnotations namespace and it is used to not complete the page submission if user don’t enter any value in the respective field. The error message shown is the standard message ("Required") by default, it can be changed by the programmer by using:

[Required (ErrorMessage = "This field is Required ")]
(Field to be required…..)

After doing the changes in model, in view page programmer have to write the element which shows the error for that specified field.

(Field to be entered)
@Html.ValidationMessageFor("the labda expression for the property")

Some more lines are used by Visual Studio to adds these references automatically on view. It adds following code snippet to the bottom of the view:

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

Run the respected view, don’t insert any value, click on submit then it will show the specified error without submitting the page. This is client validation, when this page submitted and then show errors, it will called server side validation discussed in the next article.

Wednesday, March 26, 2014

Example of array in c# , print array element at specified position

Introduction

It is a simple example of array in c#, You can print element of array using for loop. Also print array element at specific position or index number. Simple, lower bound initialized from some value, where you want to print array element. Let's take an simple example

Source code

    <form id="form1" runat="server">
    <div>
 
        <asp:Button ID="Button1" runat="server" Text="Button" Width="81px"
            onclick="Button1_Click" />
 
    </div>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </form>

Business Logic Code

    protected void Button1_Click(object sender, EventArgs e)
    {
        string[] fruits = new string[]
        {
            "papaya",
            "Apple",
            "Banana",
            "Grapes",
            "Orange",
            "Pine-apple",  
            "Mango"
        };

        Label1.Text = "fruits array elements.........<br />";
        for (int i = 0; i < fruits.Length; i++)
        {
            Label1.Text += fruits[i] + "<br />";
        }

        Label1.Text += "<br />fruits array elements [index size 1 to 5].........<br />";
        for (int i = 1; i <= 5; i++)
        {
            Label1.Text += fruits[i] + "<br />";
        }

        Label1.Text += "<br />fruits array elements [index size 3 to 5].........<br />";
        for (int i = 3; i <= 5; i++)
        {
            Label1.Text += fruits[i] + "<br />";
        }
 }

Code Generate the following output

Example of array in c# , print array element at specified position

Append, modify or Edit string in ASP.NET C#

Introduction

"A group of characters known as string", Like "Hello World!". If you want to change in it, use StringBuilder class, which is a mutable string class. Mutable means, if you want create an object with some character value, also want to change in it further , that is possible with same object. So here we take StringBuilder class for appending string.

<form id="form1" runat="server">
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button"
        Width="68px" BackColor="#FFFF66" />
    <div style="width: 88px">
 
        <asp:Label ID="Label1" runat="server" Text="Label" BackColor="#FFCC66"
            BorderColor="#CC0000"></asp:Label> 
    </div>
    </form>

Business Logic Code

protected void Button1_Click(object sender, EventArgs e)
 
        {
            StringBuilder stringd = new System.Text.StringBuilder();
            stringd.AppendLine("those are string.");
            stringd.AppendLine(" [ ");
            stringd.Append('H');
            stringd.AppendLine(" ] ");
            stringd.AppendLine("those are another string");

            Label1.Text = stringd.ToString();
        }  

Code Generate the following Output

output show Append, modify or Edit string in ASP.NET C#

Tuesday, March 25, 2014

Remove last element of array in C#

Using the Resize method you can decrement size of an array. Resize method creates a new array with new specified size Also copy old array items into newly created. Let's take an simple example to remove last element. Take less than one size of original array size. Newly created array skip last element of original array size.

<form id="form1" runat="server">
    <asp:Button ID="Button1" runat="server" Height="37px" onclick="Button1_Click"
        Text="Vegetable" Width="100px" />
    <div>
 
        <asp:Label ID="Label1" runat="server" Text="Label" Width="100px" ForeColor="Black" BackColor="Yellow"></asp:Label>
 
    </div>
    </form>

Business Logic code

 protected void Button1_Click(object sender, EventArgs e)
    {
        string[] Vegtables = new string[]
        {
            "1.Tomato",
            "2.Sweet-potato",
            "3.Onion",
            "4.Potato",
            "5.Carrot",
            "6.Locky",
            "7.Brinjal"
        };

        Label1.Text = "Vegtables array...<br />";
        foreach (string Vegtable in Vegtables)
        {
            Label1.Text += Vegtable + "<br />";
        }

        Array.Resize(ref Vegtables, Vegtables.Length - 1);

        Label1.Text += "<br />Vegtables array [last element remove...<br />";
        foreach (string Vegtable in Vegtables)
        {
            Label1.Text += Vegtable + "<br />";
        }
    }

Code Generate the following Output


Resize array method

Print element of array at specific position in c#, LINQ subset array

Using the LINQ subset array you can print element of array at specific position through Skip( ) method. If you want to print array element at position number 3 then you should skip three starting index of array. Like
ArrayName .Skip(3).Take(2).ToArray( ) .

Source

<form id="form1" runat="server">
    <div>  
        <asp:Button ID="Button1" runat="server" Height="33px" onclick="Button1_Click"
            Text="Button" Width="96px" />  
    </div>
    <p style="width: 118px">
        <asp:Label ID="Label1" runat="server" Height="40px" Text="Label" Width="100px"></asp:Label>
    </p>
    </form>

Business Logic Code

 protected void Button1_Click(object sender, EventArgs e)
    {
        string[] Vegetables = new string[]
        {
            "Tomato",
            "Potato",
            "Onion",
            "Carrot",
            "Sweet-Potato",
            "Locky"

        };

        Label1.Text = "Vegetables array...<br />";
        foreach (string Vegetable in Vegetables)
        {
            Label1.Text += Vegetable + "<br />";
        }
        string[] arraySubset = Vegetables.Skip(3).Take(2).ToArray();

        Label1.Text += "<br />Vegetables sub array [element after 3 and count 2]...<br />";
        foreach (string Vegetable in arraySubset)
        {
            Label1.Text += Vegetable + "<br />";
        }

Print element of array at specific position in c#, LINQ subset array

Insertion sort and let us consider an array of size 10 for Data Structure in 'C'

Insertion sort: 

                 In this technique of sorting, the fact of inserting the pivotal element  at its proper position from n (size of the array) number of elements is used. The pivotal element to be inserted is picked from the array itself. The selected pivotal element is inserted at the position from the left part of the array that is treated as sorted.
                   Initially the first element of the array is treated as left part of the array and being one element that is treated as sorted part of the array. The first pivotal element that is selected for insertion is the second element of the array. the pivotal element is compared with the first element and if the first element is greater than the pivotal element, then it is moved to the second position. Now we remain with no elements compared to be compared in the left part, the position where the pivotal element is found as the first position. The pivotal element is inserted at the first position to over the pass. If the first element is not greater than the pivotal element then the position of the pivotal element is right and remains at the same place. After the first pass the left part of the array containing two elements is in sorted order.
                 In the further passes the element of the unsorted part of the array is selected as pivotal element and a position to insert it in the sorted part (left array) of the array is found by shifting the sorted elements to the right if required. So after every pass the pivotal element is inserted at its proper position in the array. As the insertion operation is being performed to insert the pivotal element, this technique of sorting the array is called as ‘’insertion sort’’.

 Let us consider an array of size 10 with the following elements:

                              91 18 22 43 34 10 88 11 33 77

We can observe the elements and see that the elements are not in any order. So to arrange the elements in ascending order we can apply the insertion sort.
In the first pass, we select the element 18 is pivotal element. The left part of the of the array before the pivotal element contains only one element and it is the sorted part of the array. 18 is compared with 91,91 is greater than 18. So, 91 is shifted to the right by one position. Now there are no elements to be compared in the sorted part of the array with the pivotal element. Now the pivotal element is inserted at first position of the array. After this pass the array looks as follows: [sorted elements-BOLD, pivotal element – underlined].
                                    18 91 22 43 34 10 88 11 33 77
In the second pass, 22 is selected as pivotal element. 22 is compared with 91, as 91 is greater then 22, 91 is shifted towards its right by one position. Again 22 is compared with 18,18 is not greater then the pivotal element 22. So, the comparison is stopped because the position where the pivotal element 22 is to be inserted is found. The pivotal element 22 is inserted at second position. After this pass the array looks as follows:
                                     18 22 91 43 34 10 88 11 33 77
In the third pass, 43 is selected as pivotal element from the unsorted part of the array. 43 is compared with 91 and  91 is shifted towards its right by one position. 43 is again compared with 22. 22 is not greater then 43 and comparison is stops. 43 is inserted at third element of array.  After this pass the array looks as follows:
                                     18 22 43 91 34 10 88 11 33 77
In the fourth pass, 34 is selected as pivotal element from the unsorted part of the array. 34 is compared with 91 and 91 is shifted towards its right by one position. 34 is again compared with 43. 43 is also right shifted by one position. 34 is now comparison with 22. 22 is not greater then 34 and comparison stops. 34 is inserted at third element of array.  After this pass the array looks as follows:
                                     18 22 34 43 91 10 88 11 33 77
In the fifth pass, 10 is selected as pivotal element from the unsorted part of the array. As 10 smaller then all the sorted elements of the left part of the array, each element is shifted to right by one position respectively. 10 is inserted as the first element of array.  After this pass the array looks as follows:
                                     10 18 22 34 43 91 88 11 33 77
In the sixth pass, 88 is selected as pivotal element from the unsorted part of the array. 88 is compared with 91 and  91 is shifted towards its right by one position because it is greater then 88. 88 is now compared with 43. and comparison is stops. 88 is inserted at sixth element of array.  After this pass the array looks as follows:
                                     10 18 22 34 43 88 91 11 33 77
In the seventh pass, 11 is selected as pivotal element from the unsorted part of the array. 11 is compared with all the element up to 18 and they are  shifted to right by one position respectively. The comparison is stops when 11 is compared with 10. So, 11 is inserted at second element of the array.  After this pass the array looks as follows:
                                     10 11 18 22 34 43 88 91 33 77
In the eighth pass, 33 is selected as pivotal element from the unsorted part of the array. 33 is compared with all the element up to 34 and they are  shifted to right by one position respectively. The comparison is stops when 33 is compared with 22. So, 33 is inserted at fifth element of the array.  After this pass the array looks as follows:
                                     10 11 18 22 33 34 43 88 91 77
In the ninth and final pass, 77 is selected as pivotal element from the unsorted part of the array. 77 is compared with 91 and 88 and they are shifted to right by one position respectively. The comparison is stops when 77 is compared with 43. So, 77 is inserted at eighth element of the array.  After this pass the array looks as follows:
                                     10 11 18 22 33 34 43 77 88 91 
© Copyright 2013 Computer Programming | All Right Reserved