June 29, 2007
Hi Guys
I got the following program from a website. Website address is given below. I wish to know what else can be used to replace the "Add" word. Please help me.
myAL.Add("Hello");
myAL.Add("World");
myAL.Add("!");
Thank you
//http://msdn2.microsoft.com/en-us/library/system.collections.arraylist.aspx
using System;
using System.Collections;
public class SamplesArrayList
{
public static void Main()
{
// Creates and initializes a new ArrayList.
ArrayList myAL = new ArrayList();
myAL.Add("Hello");
myAL.Add("World");
myAL.Add("!");
// Displays the properties and values of the ArrayList.
Console.WriteLine( "myAL" );
Console.WriteLine( " Count: {0}", myAL.Count );
Console.WriteLine( " Capacity: {0}", myAL.Capacity );
Console.Write( " Values:" );
PrintValues( myAL );
}
public static void PrintValues( IEnumerable myList )
{
foreach ( Object obj in myList )
Console.Write( " {0}", obj );
Console.WriteLine();
}
}
Loading
Posted Jun 30, 2007, 6:46 AM
Thank you very much for your help Alan
AlanPosted Jun 30, 2007, 6:11 AM
http://msdn2.microsoft.com/en-us/library/system.collections.arraylist_members(vs.80).aspx
The variable myAL refers to an ArrayList object and Add is a public method which takes an object as a parameter, so the following lines:
myAL.Add("Hello");
myAL.Add("World");
myAL.Add("!");
are perfectly normal usage :)
Posted Jun 29, 2007, 6:58 PM
June 30, 2007
1)Why is program not compiling when “Add” replaced by “Addition”.
myAL.Addition("Hello");
myAL.Addition("World");
myAL.Addition("!");
2)Not only that object, dot, and method call is used to access its public methods here we not using it for the usual purpose.
Please explain.
AlanPosted Jun 29, 2007, 6:36 PM
You could replace the three calls to the Add() method with this call to the AddRange() method:
myAL.AddRange(new string[]{"Hello","World", "!"});
What's happening here is that a new string array is created containing the three above strings and then this array is added to the ArrayList. The effect is exactly the same as adding the strings individually.