C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Using ArrayList


Posted Date: 02 Feb 2004    Resource Type: Articles    Category: .NET Framework
Author: Tony JohnMember Level: Gold    
Rating: Points: 10



ArrayList can take all kinds of objects. You may add few integers, then some strings, and even some complex types including DataSet, DataTable etc.

Create an ArrayList and add 4 different type of items into it...


ArrayList myList = new ArrayList();

myList.Add ("DotNetSpider"); // Add a string.
myList.Add(1032); // Add an integer
myList.Add( DateTime.Now ); // Add current time.
myList.Add( new DataTable() ); // Add a datatable


Now iterate through the ArrayList and read one by one. We added 4 items of different types.
So, after retrieving check the type of each item and display it.

// Now retrieve items one by one.
foreach (object item in myList)
{
if ( item.GetType() == typeof(string) )
{
Console.WriteLine( "Item is a string..." );
}
else if ( item.GetType() == typeof(int) )
{
Console.WriteLine( "Item is a integer..." );
}
else if ( item.GetType() == typeof(DateTime) )
{
Console.WriteLine( "Item is a DateTime object..." );
}
else if ( item.GetType() == typeof(DataTable) )
{
Console.WriteLine( "Item is a DataTable..." );
}
}


The above code produces the following output:

Item is a string...
Item is a integer...
Item is a DateTime object...
Item is a DataTable...




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Create and Use a Hashtable
Previous Resource: Isolated Storage in .Net to store application data
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use