string nameLabels =trainingTable.Rows[rowsNumber]["FaceName"].ToString();

                         string[] labels = (string[])nameLabels;


                 // data comes from ms acces database 
                 // i want to to put all faces names in a string array but in the second line error comes canntot implicitly convert string to string[]
2 Years
Discussion Span
Last Post by deceptikon

The error is correct. nameLabels is a string (as denoted by your ToString call earlier to convert the cell value), and string is most certainly not a compatible type with string[].

Without more details on what values the "FaceName" cells contain, I can only speculate, but judging by your description of the requirement, I'd approach it like this:

var labels = new string[trainingTable.Rows.Count];

for (int i = 0; i < trainingTable.Rows.Count; i++)
    labels[i] = trainingTable.Rows[i]["FaceName"].ToString();

Of course, arrays are annoying to work with, and my preference would be a list instead:

var labels = new List<string>();

foreach (DataRow row in trainingTable.Rows)

Or if you favor potentially less performant one-liners:

var labels = trainingTable.AsEnumerable().Select(x => x["FaceName"].ToString()).ToList();

Which can be altered to get an array instead of a list by ending with ToArray rather than ToList.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.