Click here to Skip to main content
15,885,869 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Array USStates is populated with the values as shown but if i want
to access this values within the array, how is it posssible.
Refrencing through USStates(1) dosent work.

USStates.Add(new USState("Alabama", "AL"));


<pre lang="cs">// Populate the list box using an array as DataSource.
           <b> ArrayList USStates = new ArrayList();</b>
           <b> USStates.Add(new USState(&quot;Alabama&quot;, &quot;AL&quot;))</b>;
            USStates.Add(new USState(&quot;Washington&quot;, &quot;WA&quot;));
            USStates.Add(new USState(&quot;West Virginia&quot;, &quot;WV&quot;));
            USStates.Add(new USState(&quot;Wisconsin&quot;, &quot;WI&quot;));
            USStates.Add(new USState(&quot;Wyoming&quot;, &quot;WY&quot;));
            ListBox1.DataSource = USStates;

            // Set the long name as the property to be displayed and the short
            // name as the value to be returned when a row is selected.  Here
            // these are properties; if we were binding to a database table or
            // query these could be column names.
            ListBox1.DisplayMember = &quot;LongName&quot;;
            ListBox1.ValueMember = &quot;ShortName&quot;;

public class USState
    {
        private string myShortName;
        private string myLongName;

        public USState(string strLongName, string strShortName)
        {

            this.myShortName = strShortName;
            this.myLongName = strLongName;
        }

        public string ShortName
        {
            get
            {
                return myShortName;
            }
        }

        public string LongName
        {

            get
            {
                return myLongName;
            }
        }

    }
}</pre>
Posted

Please retrieve your data like below.You must cast the particular index value to UsState class.

C#
ArrayList arrayList=new ArrayList();
            var usStateItem=new UsState("Us","Usa");
            arrayList.Add(usStateItem);
            UsState usState = (UsState)arrayList[0];


Hope this helps
 
Share this answer
 
v3
You may use the [] operator to access ArrayList items, e.g.
C#
ArrayList al=new ArrayList();
al.Add("foo");
al.Add("boo");
al.Add("goo");
Console.WriteLine("Item 1 is {0}", al[1]);
al[1] = "hoo";
Console.WriteLine("Now item 1 is {0}", al[1]);



Please consider using List<string></string> instead of the ArrayList.
 
Share this answer
 
USState obj = (USState)USStates[0];
string longname= obj.LongName;
string shortname= obj.ShortName;

//if u create List instead of ArrayList

C#
List<USState> USStates=new List<USState>();
string longname=USStates[0].LongName;
string shortname=USStates[0].ShortName;
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900