locked
How to loop through an array and add phone numbers to a contact RRS feed

  • Question

  • User294868 posted

    Hi,

    The example I've got for adding a number is as follows which works:

    var cellPhone = new CNLabeledValue<CNPhoneNumber>(CNLabelPhoneNumberKey.iPhone, new CNPhoneNumber("713-555-1212")); var workPhone = new CNLabeledValue<CNPhoneNumber>("Bob", new CNPhoneNumber("408-555-1212")); contact.PhoneNumbers = new CNLabeledValue<CNPhoneNumber>[] { cellPhone, workPhone };

    I've got an array of phone number and type and I want to add the numbers like the example above, how would I implement this?

    Thanks in advance,

    Pete

    Thursday, January 26, 2017 10:24 PM

Answers

  • User294868 posted

    Hi,

    Many thanks for coming back to me, seems .Count doesn't exist but your code helped me get to the answer with the following:

    `CNLabeledValue[] phoneNumbers = new CNLabeledValue[newContact.phonenumbers.phonenumber.Length];

    for (int i = 0; i < newContact.phonenumbers.phonenumber.Length; i++) { var newPhone = new CNLabeledValue( newContact.phonenumbers.phonenumber[i].type, new CNPhoneNumber(newContact.phonenumbers.phonenumber[i].number)); phoneNumbers[i] = newPhone; }

    contact.PhoneNumbers = phoneNumbers;`

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Friday, January 27, 2017 6:16 AM

All replies

  • User294868 posted

    Some additional information, I have deserialized some XML into an object so I have:

    newContact.phonenumbers.phonenumber[0].type and newContact.phonenumbers.phonenumber[0].number newContact.phonenumbers.phonenumber[1].type and newContact.phonenumbers.phonenumber[1].number

    etc etc

    Thursday, January 26, 2017 11:07 PM
  • User21936 posted

    How about:

    for (int i = 0; i < newContact.phonenumbers.phonenumber.Count; i++) {
        var newPhone = new CNLabeledValue<CNPhoneNumber>(
            newContact.phonenumbers.phonenumber[i].type, 
            new CNPhoneNumber(newContact.phonenumbers.phonenumber[i].number));
    
        contact.PhoneNumbers = new CNLabeledValue<CNPhoneNumber>[] { newPhone };
    }
    
    Friday, January 27, 2017 1:19 AM
  • User294868 posted

    Hi,

    Many thanks for coming back to me, seems .Count doesn't exist but your code helped me get to the answer with the following:

    `CNLabeledValue[] phoneNumbers = new CNLabeledValue[newContact.phonenumbers.phonenumber.Length];

    for (int i = 0; i < newContact.phonenumbers.phonenumber.Length; i++) { var newPhone = new CNLabeledValue( newContact.phonenumbers.phonenumber[i].type, new CNPhoneNumber(newContact.phonenumbers.phonenumber[i].number)); phoneNumbers[i] = newPhone; }

    contact.PhoneNumbers = phoneNumbers;`

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Friday, January 27, 2017 6:16 AM