C
C#2y ago
Ziomek64

❔ A property is changed even though it shouldn't?

This is driving me absolutely bonkers but something weird is going on.
else
{
GraphQLResponse<Data> filt = apiListData;

GraphQLResponse<Data> startFilter = filt;

foreach (var List in startFilter.Data.MediaListCollection.lists)
{
foreach (var listEntry in List.entries)
{
listEntry.CustomListsList = new ObservableCollection<customListItem>();
if (listEntry.CustomLists != null)
{
foreach (var VARIABLE in listEntry.CustomLists)
{
listEntry.CustomListsList.Add(
new customListItem
{
key = VARIABLE.Key,
value = VARIABLE.Value
}
);
}
}
}
}

var filtered = startFilter.Data.MediaListCollection.lists;

foreach (var x in filtered)
{
x.entries = x.entries.Where(x => x.media.Title.UserPreferred.ToLowerInvariant().Contains(query))
.ToList();
}
Console.WriteLine(filtered);
else
{
GraphQLResponse<Data> filt = apiListData;

GraphQLResponse<Data> startFilter = filt;

foreach (var List in startFilter.Data.MediaListCollection.lists)
{
foreach (var listEntry in List.entries)
{
listEntry.CustomListsList = new ObservableCollection<customListItem>();
if (listEntry.CustomLists != null)
{
foreach (var VARIABLE in listEntry.CustomLists)
{
listEntry.CustomListsList.Add(
new customListItem
{
key = VARIABLE.Key,
value = VARIABLE.Value
}
);
}
}
}
}

var filtered = startFilter.Data.MediaListCollection.lists;

foreach (var x in filtered)
{
x.entries = x.entries.Where(x => x.media.Title.UserPreferred.ToLowerInvariant().Contains(query))
.ToList();
}
Console.WriteLine(filtered);
I'm making a local variable at the beginning to which I assign data from apiListData. I'm making some changes on that local variables, especially that foreach (var x in filtered) which filters data. When I do debug point on that console.writeline apiListData from the beginning which was accessed only is affected even though it shouldn't. And don't mind second local variable, I was doing it only to avoid this weird situation kekw Whole .cs - https://pastebin.com/4pezNHzB apiListData is just raw data that I'm assigning in Refresh task to keep it for filtering purposes.
Pastebin
using System.Collections.ObjectModel;using System.ComponentModel;us...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
25 Replies
Jimmacle
Jimmacle2y ago
classes are reference types, so filt and startFilter and apiListData all "point" to the same object
Ziomek64
Ziomek64OP2y ago
How do I "copy" it then so it isn't altered by the whole process
Jimmacle
Jimmacle2y ago
there is no standard way to do it
Ziomek64
Ziomek64OP2y ago
what do you suggest here
Jimmacle
Jimmacle2y ago
without knowing anything about the library you're using i'd look for any support for it in the library first are you sure you need to make a copy of it and you can't do whatever you're trying to do another way?
Ziomek64
Ziomek64OP2y ago
I want to keep apiListData untouched somehow
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Ziomek64
Ziomek64OP2y ago
Are you just correcting code or fixing my problem?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Ziomek64
Ziomek64OP2y ago
And the setter is not accessed when that change happens, this is normal too?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Ziomek64
Ziomek64OP2y ago
Yes i was trying to say that that perhaps through that observable it changed
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Ziomek64
Ziomek64OP2y ago
that observable is binded to collectionview but it one-way
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Ziomek64
Ziomek64OP2y ago
yes
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Ziomek64
Ziomek64OP2y ago
Where do I set break point then
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX2y ago
tebeco#0205
- x.coll = new ObsCol()
+var tmp = new ObsCol()
seed()
+x.coll = tmp
- x.coll = new ObsCol()
+var tmp = new ObsCol()
seed()
+x.coll = tmp
React with ❌ to remove this embed.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Ziomek64
Ziomek64OP2y ago
but where, I haven't ever used it that seed thing I'm going to sleep, cause I'm tired Will message tomorrow but you can reply
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Ziomek64
Ziomek64OP2y ago
I don't have any add loop on lines 836-936 (when I get data from API) but I think I get your point, I'll try later when I get home Am i doing it wrong again?
GraphQLResponse<Data> filt = apiListData;

var tmp = new List<List>();
foreach (var list in apiListData.Data.MediaListCollection.lists)
{
tmp.Add(list);
}

foreach (var List in tmp)
{
foreach (var listEntry in List.entries)
{
listEntry.CustomListsList = new ObservableCollection<customListItem>();
if (listEntry.CustomLists != null)
{
foreach (var VARIABLE in listEntry.CustomLists)
{
listEntry.CustomListsList.Add(
new customListItem
{
key = VARIABLE.Key,
value = VARIABLE.Value
}
);
}
}
}
}

var filtered = tmp;

foreach (var x in filtered)
{
x.entries = x.entries.Where(x => x.media.Title.UserPreferred.ToLowerInvariant().Contains(query.ToLowerInvariant()))
.ToList();
}
Console.WriteLine(filtered);
GraphQLResponse<Data> filt = apiListData;

var tmp = new List<List>();
foreach (var list in apiListData.Data.MediaListCollection.lists)
{
tmp.Add(list);
}

foreach (var List in tmp)
{
foreach (var listEntry in List.entries)
{
listEntry.CustomListsList = new ObservableCollection<customListItem>();
if (listEntry.CustomLists != null)
{
foreach (var VARIABLE in listEntry.CustomLists)
{
listEntry.CustomListsList.Add(
new customListItem
{
key = VARIABLE.Key,
value = VARIABLE.Value
}
);
}
}
}
}

var filtered = tmp;

foreach (var x in filtered)
{
x.entries = x.entries.Where(x => x.media.Title.UserPreferred.ToLowerInvariant().Contains(query.ToLowerInvariant()))
.ToList();
}
Console.WriteLine(filtered);
I'm populating tmp with lists from apiListData on that last console.writeline apiListData is affected as before again which lines were you talking about What if i make apiListData serialized back to json and deserialize when needed? That should make "a copy". Idk how performant would that be with approx 500 items max, on mobile device
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server