1

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];
    }
}
4
  • 1
    You need to initiatialize the array, like this: string [] DomainArray = new string[myresponse.Count]; Commented Jun 3, 2016 at 10:41
  • that is a declaration of the variable Commented Jun 3, 2016 at 10:43
  • @HugoQuintelaRibeiro: this won't work because it's the count of myresponses AND EmailAddressSuffixes Commented Jun 3, 2016 at 10:45
  • since arrays are not dynamic, think about working with lists Commented Jun 3, 2016 at 10:47

4 Answers 4

1

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();
Sign up to request clarification or add additional context in comments.

Comments

0
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()

Comments

0

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]);
    }
}

Comments

0

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]);
    }
}

5 Comments

You will receive a ArgumentOutOfRangeException as you are setting the array to a length of myresponse rather than the length of all the EmailAddressSuffixes
@AhmedJalal: have a look at my answer used List<string> instead of static arrays
It will only work if each each response item has only one EmailAddressSuffix
after checking again @bolt19 answer is according to my expectations
@AhmedJalal thank you for unaccepting my answer, copy paste it to your edited answer and mark this as answer and accept it to earn reputation for your own.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.