C# single list from different classes
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
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?
c# list
add a comment |
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?
c# list
1
if (PersonData == null): you cannot access Persondata.Member in this case
– Klaus Gütter
Nov 16 '18 at 11:44
add a comment |
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?
c# list
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?
c# list
c# list
edited Nov 16 '18 at 11:59
Psymon25
asked Nov 16 '18 at 11:42
Psymon25Psymon25
327
327
1
if (PersonData == null): you cannot access Persondata.Member in this case
– Klaus Gütter
Nov 16 '18 at 11:44
add a comment |
1
if (PersonData == null): you cannot access Persondata.Member in this case
– Klaus Gütter
Nov 16 '18 at 11:44
1
1
if (PersonData == null): you cannot access Persondata.Member in this case
– Klaus Gütter
Nov 16 '18 at 11:44
if (PersonData == null): you cannot access Persondata.Member in this case
– Klaus Gütter
Nov 16 '18 at 11:44
add a comment |
1 Answer
1
active
oldest
votes
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,
...
};
}
1
thanks going to try this now as i have never try a mapping before so happy to learn something new ;)
– Psymon25
Nov 16 '18 at 12:00
1
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
– Psymon25
Nov 16 '18 at 12:20
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53337184%2fc-sharp-single-list-from-different-classes%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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,
...
};
}
1
thanks going to try this now as i have never try a mapping before so happy to learn something new ;)
– Psymon25
Nov 16 '18 at 12:00
1
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
– Psymon25
Nov 16 '18 at 12:20
add a comment |
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,
...
};
}
1
thanks going to try this now as i have never try a mapping before so happy to learn something new ;)
– Psymon25
Nov 16 '18 at 12:00
1
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
– Psymon25
Nov 16 '18 at 12:20
add a comment |
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,
...
};
}
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,
...
};
}
answered Nov 16 '18 at 11:56
Tolga EvcimenTolga Evcimen
4,16893971
4,16893971
1
thanks going to try this now as i have never try a mapping before so happy to learn something new ;)
– Psymon25
Nov 16 '18 at 12:00
1
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
– Psymon25
Nov 16 '18 at 12:20
add a comment |
1
thanks going to try this now as i have never try a mapping before so happy to learn something new ;)
– Psymon25
Nov 16 '18 at 12:00
1
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
– Psymon25
Nov 16 '18 at 12:20
1
1
thanks going to try this now as i have never try a mapping before so happy to learn something new ;)
– Psymon25
Nov 16 '18 at 12:00
thanks going to try this now as i have never try a mapping before so happy to learn something new ;)
– Psymon25
Nov 16 '18 at 12:00
1
1
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
– Psymon25
Nov 16 '18 at 12:20
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
– Psymon25
Nov 16 '18 at 12:20
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53337184%2fc-sharp-single-list-from-different-classes%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
if (PersonData == null): you cannot access Persondata.Member in this case
– Klaus Gütter
Nov 16 '18 at 11:44