0

I have 2 parts on an API that share some similarities but function differently. I am currently trying to take data from a list object of People from class B and add this data to a list of People created from Class A(hopefully explained well enough?)

The People structure in the 2 classes are actually the same:

[XmlRoot(ElementName = "people")]
public class People
{
        [XmlElement(ElementName = "member")]
        public List<Member> Member { get; set; }
}


[XmlRoot(ElementName = "member")]
public class Member
{
    [XmlElement(ElementName = "firstName")]
    public string FirstName { get; set; }
    [XmlElement(ElementName = "lastName")]
    public string LastName { get; set; }
    [XmlAttribute(AttributeName = "memberId")]
    public string MemberId { get; set; }
    [XmlAttribute(AttributeName = "memberNotes")]
    public string Notes { get; set; }
    [XmlElement(ElementName = "departed")]
    public string Departed { get; set; }
    [XmlElement(ElementName = "currentPosition")]
    public Name CurrentPosition { get; set; }
}

In normal operation the following code sets the People list just fine:

public People PersonData { get; set; }
...
....
var results = ApiA.People;
PersonData = results.Member; //during normal operation only one entry of member is returned

However in another operation the results returns a Larger list of member objects so I am trying to add to the same list to ensure handling later on uses single method for both operations due to the same data structure, what I am trying is as follows:

if(PersonData == null)
    PersonData = new API_A.People();


var results = ApiB.People; //person data here belongs to API_B.Person


foreach (var res in results)
{
    if (res?.Member != null)
    {
        if (PersonData == null)
        {
            PersonData.Member.AddRange(res.People.Member.Cast<API_A.Member>());
            break;
        }
        else
            PersonData.Member.Union(res.People.Member.Cast<API_A.Member>());
    }
}

No errors are returned in the ide but during operation I continually receive a NullReferenceException during the add range operation; so as I am still learning I would really appreciate understanding what I am doing wrong here?

1
  • 1
    if (PersonData == null): you cannot access Persondata.Member in this case Commented Nov 16, 2018 at 11:44

1 Answer 1

1

2 problems are obvious.

If PersonData is null, you cannot access to PersonData.Member before creating the PersonData object first. So in your case it should be:

PersonData = new People();

Next problem you'll have is the casting. Even if everything is same in 2 different classes, unless there is an inheritance relation between those, you cannot cast one to another. What you should do is to map your one class to the other. Just create a mapper method somewhere else that maps your API_A.Member to API_B.Member and/or vica versa. This kind of mapping workarounds are widely used, feel safe creating this heavy looking mapping method.

Example:

API_A.Member MapBToA(API_B.Member member)
{
    return new API_A.Member {
        CharacterName = member.CharacterName,
        ...
    };
}
Sign up to request clarification or add additional context in comments.

2 Comments

thanks going to try this now as i have never try a mapping before so happy to learn something new ;)
Thanks for this made progress and this has simply rectified the issue and cant believe i have never this before as it seems so simple

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.