Generic list's items are all the same












0















Im populating a List with my object (Event).

The object has a field called DocumentDate that is what I wish to update in each item.
But after the loop, all items in the list are the same! I want to know why:



The code:



private List<Events> CreateEventsBetween(string start, string end, string repeatState, Event defaultEvent)
{
var states = Resources.GetStringArray(Resource.Array.repeat_states);
DateTime.TryParse(start, out DateTime dtStart);
DateTime.TryParse(end, out DateTime dtEnd);
List<Event> events = new List<Event>();

if (repeatState == states[0])
while(dtStart<= dtEnd)
{
var e = defaultEvent;
e.DocumentDate = dtStart;
events.Add(e);

dtStart= dtStart.AddDays(i);
}
...
...
...

return events;
}


Here every item in the list of events have the same DocumentDate's, when they should have different ones










share|improve this question

























  • Instead of the while loop, you could also assign the events in a single line using System.Linq, for example: var events = Enumerable.Range(0, dtEnd.Subtract(dtStart).Days).Select(i => new Event {DocumentDate = dtStart.AddDays(i)}).ToList();

    – Rufus L
    Nov 15 '18 at 17:24













  • Sorry, I corrected the generic List thing, now, I don't think that would do for me, my object has other fields (about 24 or so), and I need the other fields to be equal to the defaultEvent, just changing the DocumentDate.

    – Erick Santander
    Nov 16 '18 at 12:32











  • In that case you may need to implement a deep copy method on your class if the goal is to allow callers of this method to define their own default property values. That way you can create a new instance based on the one passed in. this question has answers that describe how to implement a deep clone method on a class.

    – Rufus L
    Nov 16 '18 at 16:49
















0















Im populating a List with my object (Event).

The object has a field called DocumentDate that is what I wish to update in each item.
But after the loop, all items in the list are the same! I want to know why:



The code:



private List<Events> CreateEventsBetween(string start, string end, string repeatState, Event defaultEvent)
{
var states = Resources.GetStringArray(Resource.Array.repeat_states);
DateTime.TryParse(start, out DateTime dtStart);
DateTime.TryParse(end, out DateTime dtEnd);
List<Event> events = new List<Event>();

if (repeatState == states[0])
while(dtStart<= dtEnd)
{
var e = defaultEvent;
e.DocumentDate = dtStart;
events.Add(e);

dtStart= dtStart.AddDays(i);
}
...
...
...

return events;
}


Here every item in the list of events have the same DocumentDate's, when they should have different ones










share|improve this question

























  • Instead of the while loop, you could also assign the events in a single line using System.Linq, for example: var events = Enumerable.Range(0, dtEnd.Subtract(dtStart).Days).Select(i => new Event {DocumentDate = dtStart.AddDays(i)}).ToList();

    – Rufus L
    Nov 15 '18 at 17:24













  • Sorry, I corrected the generic List thing, now, I don't think that would do for me, my object has other fields (about 24 or so), and I need the other fields to be equal to the defaultEvent, just changing the DocumentDate.

    – Erick Santander
    Nov 16 '18 at 12:32











  • In that case you may need to implement a deep copy method on your class if the goal is to allow callers of this method to define their own default property values. That way you can create a new instance based on the one passed in. this question has answers that describe how to implement a deep clone method on a class.

    – Rufus L
    Nov 16 '18 at 16:49














0












0








0








Im populating a List with my object (Event).

The object has a field called DocumentDate that is what I wish to update in each item.
But after the loop, all items in the list are the same! I want to know why:



The code:



private List<Events> CreateEventsBetween(string start, string end, string repeatState, Event defaultEvent)
{
var states = Resources.GetStringArray(Resource.Array.repeat_states);
DateTime.TryParse(start, out DateTime dtStart);
DateTime.TryParse(end, out DateTime dtEnd);
List<Event> events = new List<Event>();

if (repeatState == states[0])
while(dtStart<= dtEnd)
{
var e = defaultEvent;
e.DocumentDate = dtStart;
events.Add(e);

dtStart= dtStart.AddDays(i);
}
...
...
...

return events;
}


Here every item in the list of events have the same DocumentDate's, when they should have different ones










share|improve this question
















Im populating a List with my object (Event).

The object has a field called DocumentDate that is what I wish to update in each item.
But after the loop, all items in the list are the same! I want to know why:



The code:



private List<Events> CreateEventsBetween(string start, string end, string repeatState, Event defaultEvent)
{
var states = Resources.GetStringArray(Resource.Array.repeat_states);
DateTime.TryParse(start, out DateTime dtStart);
DateTime.TryParse(end, out DateTime dtEnd);
List<Event> events = new List<Event>();

if (repeatState == states[0])
while(dtStart<= dtEnd)
{
var e = defaultEvent;
e.DocumentDate = dtStart;
events.Add(e);

dtStart= dtStart.AddDays(i);
}
...
...
...

return events;
}


Here every item in the list of events have the same DocumentDate's, when they should have different ones







c# list loops






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 12:28







Erick Santander

















asked Nov 15 '18 at 16:56









Erick SantanderErick Santander

5911




5911













  • Instead of the while loop, you could also assign the events in a single line using System.Linq, for example: var events = Enumerable.Range(0, dtEnd.Subtract(dtStart).Days).Select(i => new Event {DocumentDate = dtStart.AddDays(i)}).ToList();

    – Rufus L
    Nov 15 '18 at 17:24













  • Sorry, I corrected the generic List thing, now, I don't think that would do for me, my object has other fields (about 24 or so), and I need the other fields to be equal to the defaultEvent, just changing the DocumentDate.

    – Erick Santander
    Nov 16 '18 at 12:32











  • In that case you may need to implement a deep copy method on your class if the goal is to allow callers of this method to define their own default property values. That way you can create a new instance based on the one passed in. this question has answers that describe how to implement a deep clone method on a class.

    – Rufus L
    Nov 16 '18 at 16:49



















  • Instead of the while loop, you could also assign the events in a single line using System.Linq, for example: var events = Enumerable.Range(0, dtEnd.Subtract(dtStart).Days).Select(i => new Event {DocumentDate = dtStart.AddDays(i)}).ToList();

    – Rufus L
    Nov 15 '18 at 17:24













  • Sorry, I corrected the generic List thing, now, I don't think that would do for me, my object has other fields (about 24 or so), and I need the other fields to be equal to the defaultEvent, just changing the DocumentDate.

    – Erick Santander
    Nov 16 '18 at 12:32











  • In that case you may need to implement a deep copy method on your class if the goal is to allow callers of this method to define their own default property values. That way you can create a new instance based on the one passed in. this question has answers that describe how to implement a deep clone method on a class.

    – Rufus L
    Nov 16 '18 at 16:49

















Instead of the while loop, you could also assign the events in a single line using System.Linq, for example: var events = Enumerable.Range(0, dtEnd.Subtract(dtStart).Days).Select(i => new Event {DocumentDate = dtStart.AddDays(i)}).ToList();

– Rufus L
Nov 15 '18 at 17:24







Instead of the while loop, you could also assign the events in a single line using System.Linq, for example: var events = Enumerable.Range(0, dtEnd.Subtract(dtStart).Days).Select(i => new Event {DocumentDate = dtStart.AddDays(i)}).ToList();

– Rufus L
Nov 15 '18 at 17:24















Sorry, I corrected the generic List thing, now, I don't think that would do for me, my object has other fields (about 24 or so), and I need the other fields to be equal to the defaultEvent, just changing the DocumentDate.

– Erick Santander
Nov 16 '18 at 12:32





Sorry, I corrected the generic List thing, now, I don't think that would do for me, my object has other fields (about 24 or so), and I need the other fields to be equal to the defaultEvent, just changing the DocumentDate.

– Erick Santander
Nov 16 '18 at 12:32













In that case you may need to implement a deep copy method on your class if the goal is to allow callers of this method to define their own default property values. That way you can create a new instance based on the one passed in. this question has answers that describe how to implement a deep clone method on a class.

– Rufus L
Nov 16 '18 at 16:49





In that case you may need to implement a deep copy method on your class if the goal is to allow callers of this method to define their own default property values. That way you can create a new instance based on the one passed in. this question has answers that describe how to implement a deep clone method on a class.

– Rufus L
Nov 16 '18 at 16:49












1 Answer
1






active

oldest

votes


















2














Because all events are the same reference. You assign it at var e = defaultEvent;.



Instead you need to initialize different with new:



var e = new Event{ DocumentDate = dtStart }; // other properties as well





share|improve this answer


























  • the date aux was supposed to be dtStart, sorry, I was translating the code to put it in here, and forgot to change that, thanks. I was suspicious about the defaultEvent, but since I changed the date I thought it would mater, anyways, thanks again!

    – Erick Santander
    Nov 15 '18 at 17:16













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


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53324400%2fgeneric-lists-items-are-all-the-same%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














Because all events are the same reference. You assign it at var e = defaultEvent;.



Instead you need to initialize different with new:



var e = new Event{ DocumentDate = dtStart }; // other properties as well





share|improve this answer


























  • the date aux was supposed to be dtStart, sorry, I was translating the code to put it in here, and forgot to change that, thanks. I was suspicious about the defaultEvent, but since I changed the date I thought it would mater, anyways, thanks again!

    – Erick Santander
    Nov 15 '18 at 17:16


















2














Because all events are the same reference. You assign it at var e = defaultEvent;.



Instead you need to initialize different with new:



var e = new Event{ DocumentDate = dtStart }; // other properties as well





share|improve this answer


























  • the date aux was supposed to be dtStart, sorry, I was translating the code to put it in here, and forgot to change that, thanks. I was suspicious about the defaultEvent, but since I changed the date I thought it would mater, anyways, thanks again!

    – Erick Santander
    Nov 15 '18 at 17:16
















2












2








2







Because all events are the same reference. You assign it at var e = defaultEvent;.



Instead you need to initialize different with new:



var e = new Event{ DocumentDate = dtStart }; // other properties as well





share|improve this answer















Because all events are the same reference. You assign it at var e = defaultEvent;.



Instead you need to initialize different with new:



var e = new Event{ DocumentDate = dtStart }; // other properties as well






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 16 '18 at 12:51

























answered Nov 15 '18 at 16:58









RangoRango

365k46472730




365k46472730













  • the date aux was supposed to be dtStart, sorry, I was translating the code to put it in here, and forgot to change that, thanks. I was suspicious about the defaultEvent, but since I changed the date I thought it would mater, anyways, thanks again!

    – Erick Santander
    Nov 15 '18 at 17:16





















  • the date aux was supposed to be dtStart, sorry, I was translating the code to put it in here, and forgot to change that, thanks. I was suspicious about the defaultEvent, but since I changed the date I thought it would mater, anyways, thanks again!

    – Erick Santander
    Nov 15 '18 at 17:16



















the date aux was supposed to be dtStart, sorry, I was translating the code to put it in here, and forgot to change that, thanks. I was suspicious about the defaultEvent, but since I changed the date I thought it would mater, anyways, thanks again!

– Erick Santander
Nov 15 '18 at 17:16







the date aux was supposed to be dtStart, sorry, I was translating the code to put it in here, and forgot to change that, thanks. I was suspicious about the defaultEvent, but since I changed the date I thought it would mater, anyways, thanks again!

– Erick Santander
Nov 15 '18 at 17:16






















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53324400%2fgeneric-lists-items-are-all-the-same%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

List item for chat from Array inside array React Native

Thiostrepton

Caerphilly