Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Cannot modify the return value" error when modifying content of List<T>

Tags:

c#

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?

like image 974
Boardy Avatar asked Jan 15 '23 19:01

Boardy


1 Answers

Welcome to the wonderful world of evil mutable structs!

Change ThreadCheck to a class and everything will work fine.

like image 154
SLaks Avatar answered Jan 21 '23 17:01

SLaks