how to add string to array  c#

R8VI
R8VI used Ask the Experts™
on
hi,

I haev a for loop

and in that loop i am getting name from the db and assigning it to string sName.
I have an array called string [] PName;
I want to add sName to pName every time there is something how do i do this.

Please help

Thanks,

r8VI
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Gary DavisDir Internet Svcs

Commented:
A string array needs to be allocated with the maximum number of entries - it can't be increased. To have a collection that can be added to, use ArrayList or List.
var names = new List<string>();
then in your loop:
names.Add(sName);
You can then convert the list into an array if necessary.
Gary Davis

Author

Commented:
so i tried

V = new list<string>();

but this gives me error V does not exists in the current context
using System.Collections;
....
ArrayList pName = new ArrayList();

then loop:
....
pName.Add(sName);
....

There is also a sample in the code section.
using System;
using System.Collections;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int i = 0;
            ArrayList ItemList = new ArrayList();
            ItemList.Add("Item4");
            ItemList.Add("Item5");
            ItemList.Add("Item2");
            ItemList.Add("Item1");
            ItemList.Add("Item3");
            MessageBox.Show ("Shows Added Items");
            for (i = 0; i < = ItemList.Count - 1; i++)
            {
                MessageBox.Show(ItemList[i].ToString());
            }
            //insert an item 
            ItemList.Insert(3, "Item6");
            //sort itemms in an arraylist 
            ItemList.Sort();
            //remove an item 
            ItemList.Remove("Item1");
            //remove item from a specified index 
            ItemList.RemoveAt(3);
            MessageBox.Show("Shows final Items the ArrayList");
            for (i = 0; i < = ItemList.Count - 1; i++)
            {
                MessageBox.Show(ItemList[i].ToString());
            }
        }
    }
}

Open in new window

Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Gary DavisDir Internet Svcs

Commented:
> V = new list<string>();

> but this gives me error V does not exists in the current context
You first have to declare V so use one of these:
var V = new list<string>();
string V = new list<string>();
 
List<string> V = new List<string>();
V.add("item1");
V.add("item2");
V.add("item3");
if (V[2]=="item3")
{
MessageBox.Show("It's OK!");
}
kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
>>  string V = new list<string>();

I believe you mean

    List<string> V = new List<string>();

The former would result in a cast exception. Also, I believe StringCollection is optimized for string lists/collections:

    System.Collections.Specialized.StringCollection V = new System.Collections.Specialized.StringCollection();
i would like to accept yjchong514   and pl give point


ID:33116899  
Author:yjchong514      

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial