.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Search an Element in ArrayList

Posted By:Syed Shakeer Hussain       Posted Date: January 02, 2011    Points: 30    Category: ASP.NET    URL: http://www.dotnetspark.com  

This article shows you how to search an Element in an Existing ArrayList,if the element is not found in an ArrayList add New element in to the ArrayList
 

using System.Collections;

namespace dataTableFilter

{

public partial class Form1 : Form

{

public Form1 ()

{

InitializeComponent();

}

ArrayList li = new ArrayList();

private void Form_Load(object sender, EventArgs e)

{

li.Add("20");

li.Add("10");

li.Add("40");

}

public int searchList(string listvalue)

{

int a = li.BinarySearch(listvalue);

return a;

}

private void button1_Click(object sender, EventArgs e)

{

string value = textBox1.Text;

if (searchList(value )>=0)

{

MessageBox.Show("Element found");

}

else

{

li.Add(value);

MessageBox.Show("Element Added sucessfully");

}

}

}

}

Search and Remove an Element from a ArrayList:-

ArrayList li = new ArrayList();

li.Add("20");

li.Add("10");

li.Add("40");

public int searchList(string listvalue)

{

int a = li.BinarySearch(listvalue);

return a;

}

private void buttonRemove_Click(object sender, EventArgs e)

{

if (li.Count > 0)

{

if (searchList(textBox1.Text) >= 0)

{

li.Remove(textBox1.Text);

MessageBox.Show("Element Removed from a ArrayList");

}

}

}


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend