I am currently working on a C# project and I need add data to a custom List array. I then later on need to enumerate through the list, finding a particular record and then modify the data. However, I am getting an error within Visual Studio
Cannot modify the return value of System.Collections.Generic.List<EmailServer.ThreadCheck>.this[int] because it is not a variable.
Below is the code that initialises the List Array
static List<ThreadCheck> threadKick = new List<ThreadCheck>();
Below is how I am adding data to the List array
public static void addThread(ILibraryInterface library, int threadID, string threadName)
{
if (Watchdog.library == null)
{
Watchdog.library = library;
}
long currentEpochTime = library.convertDateTimeToEpoch(DateTime.Now);
threadKick.Add(new ThreadCheck()
{
threadName = threadName,
threadID = threadID,
epochTimeStamp = currentEpochTime
});
library.logging(classDetails + MethodInfo.GetCurrentMethod().Name, string.Format("Thread ID: {0} has been added to watchdog for {1}",
threadID, threadName));
}
Below is the code where I am trying to modify the data within the list
public static void kickThread(int threadId)
{
lock (threadKick)
{
for (int i = 0; i < threadKick.Count; i++)
{
if (threadKick[i].threadID == threadId)
{
threadKick[i].epochTimeStamp = library.convertDateTimeToEpoch(DateTime.Now);
break;
}
}
}
}
How can I modify the content of the list array without getting the error as above?
Welcome to the wonderful world of evil mutable structs!
Change ThreadCheck
to a class
and everything will work fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With