Malik Kashmiri
Malik Kashmiri

Reputation: 5851

cannot implicit convert type string string[]

I want to store string into string array, but it shows error. Here is my code:

List <ResponseObject> myresponse =JsonConvert.DeserializeObject<List<ResponseObject>>(responseFromServer);
string [] DomainArray;
for (int i = 0; i < myresponse.Count; i++)
{
    for (int j = 0; j < myresponse[i].EmailAddressSuffixes.Count; j++)
    {
        DomainArray = myresponse[i].EmailAddressSuffixes[j];
    }
}

Upvotes: 1

Views: 65

Answers (4)

Malik Kashmiri
Malik Kashmiri

Reputation: 5851

I made two changes in my above code. first from @Hugo and second from @Roma

List <ResponseObject> myresponse = JsonConvert.DeserializeObject<List<ResponseObject>>(responseFromServer);
List<string> DomainArray = new List<string>();
for (int i = 0; i < myresponse.Count; i++)
{
    for (int j = 0; j < myresponse[i].EmailAddressSuffixes.Count; j++)
    {
        DomainArray.Add(myresponse[i].EmailAddressSuffixes[j]);
    }
}

Upvotes: 0

M. Schena
M. Schena

Reputation: 2107

Since arrays are not dynamic, think about working with lists:

List <ResponseObject> myresponse =JsonConvert.DeserializeObject<List<ResponseObject>>(responseFromServer);
List<string> DomainArray = new List<string>();
for (int i = 0; i < myresponse.Count; i++)
{
    for (int j = 0; j < myresponse[i].EmailAddressSuffixes.Count; j++)
    {
        DomainArray.add(myresponse[i].EmailAddressSuffixes[j]);
    }
}

Upvotes: 0

bolt19
bolt19

Reputation: 2053

You are trying to assign the DomainArray (which is an array of strings) to a single string.

Try this, it adds all the values to a list then converts to list to an array:

    List<ResponseObject> myresponse = JsonConvert.DeserializeObject<List<ResponseObject>>(responseFromServer);
    List<string> DomainList = new List<string>();
    for (int i = 0; i < myresponse.Count; i++)
    {
        for (int j = 0; j < myresponse[i].EmailAddressSuffixes.Count; j++)
        {
            DomainList.Add(myresponse[i].EmailAddressSuffixes[j]);
        }
    }
    var DomainArray = DomainList.ToArray();

Upvotes: 1

shadow
shadow

Reputation: 1903

List <ResponseObject> myresponse =JsonConvert.DeserializeObject<List<ResponseObject>>(responseFromServer);
var DomainArray = new List<string>();
for (int i = 0; i < myresponse.Count; i++)
{
for (int j = 0; j < myresponse[i].EmailAddressSuffixes.Count; j++)
{
DomainArray.Add( myresponse[i].EmailAddressSuffixes[j] );
}
}

Then you can get the array (if required) using DomainArray.ToArray()

Upvotes: 0

Related Questions