array into string[]

A

altman

Hello,
here is my vode:

***
ArrayList liste = new ArrayList();
SqlDataReader reader2 = macommande_mc.ExecuteReader();

string mc = "";

while (reader2.Read())
{
mc +=
reader2["VALUE_KEYWORD"].ToString() ;
liste.Add(mc);
}

I need to add all the values from my data reader into this:

string [] keywords = { "value1", "value2", "value3", "valueX" }

I tried this: string[] keywords = { liste.Tostring() } but it didn't work

Any idea?
Thanks,
 
H

Hans Kesting

altman laid this down on his screen :
Hello,
here is my vode:

***
ArrayList liste = new ArrayList();
SqlDataReader reader2 = macommande_mc.ExecuteReader();

string mc = "";

while (reader2.Read())
{
mc += reader2["VALUE_KEYWORD"].ToString()
;
liste.Add(mc);
}

I need to add all the values from my data reader into this:

string [] keywords = { "value1", "value2", "value3", "valueX" }

I tried this: string[] keywords = { liste.Tostring() } but it didn't work

Any idea?
Thanks,

string[] keywords = liste.ToArray(typeof(string));

should do the trick.

Hans Kesting
 
A

altman

No, unfortunately I've got an error:

Cannot convert ent le type 'System.Array' en 'string[]'.

I found this way:

while (reader2.Read())
{
mc += reader2["VALUE_KEYWORD"].ToString() + "," ;
}
reader2.Close();

int count = 1;
mc = mc.Substring(0, mc.Length - count);

string[] keywords = mc.Split(',');


Hans Kesting said:
altman laid this down on his screen :
Hello,
here is my vode:

***
ArrayList liste = new ArrayList();
SqlDataReader reader2 = macommande_mc.ExecuteReader();

string mc = "";

while (reader2.Read())
{
mc +=
reader2["VALUE_KEYWORD"].ToString() ;
liste.Add(mc);
}

I need to add all the values from my data reader into this:

string [] keywords = { "value1", "value2", "value3", "valueX" }

I tried this: string[] keywords = { liste.Tostring() } but it didn't work

Any idea?
Thanks,

string[] keywords = liste.ToArray(typeof(string));

should do the trick.

Hans Kesting
 
G

Göran Andersson

altman said:
No, unfortunately I've got an error:

Cannot convert ent le type 'System.Array' en 'string[]'.

I found this way:

while (reader2.Read())
{
mc += reader2["VALUE_KEYWORD"].ToString() + "," ;
}
reader2.Close();

int count = 1;
mc = mc.Substring(0, mc.Length - count);

string[] keywords = mc.Split(',');

Well, that won't work if any of the values happened to contain a comma...

You just have to cast the result from the ToArray method, as it returns
an Object reference:

string[] keywords = (string[])liste.ToArray(typeof(string));


However, unless you are limited to using framework 1.1 you should not
use an ArrayList at all, use a strongly typed List<T> instead:


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

while (reader2.Read()) {
items.Ass(reader2["VALUE_KEYWORD"].ToString());
}
reader2.Close();

string[] keywords = items.ToArray();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top