Search code examples
c#implicit-conversion

cannot implicitly convert from type void to Systems.Collections.Generics.List<>


I'm trying to add a new object to a list of objects using string.Equals and getting a conversion error as below

Here is my object:

public class TestNode : CSVBaseModel
{
    public int DatabaseID { get; set; }
    public string EntityType { get; set; } //make object
    public string Name { get; set; }
    public int StepNumber { get; set; }
    public virtual Job JobModel { get; set; }
    public TestNode(int databaseID, string entityType, string name, int stepNumber)
    {
        DatabaseID = databaseID;
        EntityType = entityType;
        Name = name;
        StepNumber = stepNumber;
    }
}

And my list:

List<TestNode> testNodes = new List<TestNode>();

And here is where the error happens (after the colon, in the statement):

job.TestNodes = string.IsNullOrEmpty(columns[2]) ? job.TestNodes : testNodes.Add(new TestNode(nodeCounter += 1, "JobExecutor", columns[2], 0));

Can anyone tell me why it wont convert? Any help would be appreciated.

EDIT :

TestNodes is a list property generic to another object. testNodes is a list specific to the byteArray being compiled. I hope that makes sense. Maybe this gives a little better context:

private Job ProcessFile2(byte[] filestream)
{
    List<TestNode> testNodes = new List<TestNode>();
    Job job = new Job();
    Guid uniqueRecordId = Guid.NewGuid();
    StreamReader streamReader = new StreamReader(new MemoryStream(filestream), System.Text.Encoding.UTF8, true);
    DateTime creationDate = DateTime.Now;
    int nodeCounter = 0;

    string line;
    int linecounter = 0;
    while ((line = streamReader.ReadLine()) != null)
    {
        if (linecounter > 1)
        {
            var columns = line.Split(",");
            job.JobName = string.IsNullOrEmpty(columns[1]) ? job.JobName : columns[1];
            job.TestNodes = string.IsNullOrEmpty(columns[2]) ? job.TestNodes : new List<TestNode> { new TestNode(nodeCounter += 1, "JobExecutor", columns[2], 0) };
            job.TestNodes= string.IsNullOrEmpty(columns[3]) ? job.TestNodes : testNodes.Add(new TestNode(nodeCounter += 1, "OrganisationType", job.OrganisationType, 0));
            job.ContractingOrganisationType = string.IsNullOrEmpty(columns[4]) ? job.ContractingOrganisationType : columns[4];
            job.TestNodes = string.Equals(columns[0], "Job Step") ? testNodes.Add(new TestNode(nodeCounter += 1, "StepName", columns[6], Convert.ToInt32(columns[5]))) : job.TestNodes;
            job.TestNodes = string.Equals(columns[0], "Custom Input") ? testNodes.Add(new TestNode(nodeCounter += 1, "InputName", columns[6], Convert.ToInt32(columns[5]))) : job.TestNodes;
            job.TestNodes = string.Equals(columns[0], "Custom Output") ? testNodes.Add(new TestNode(nodeCounter += 1, "OutputName", columns[6], Convert.ToInt32(columns[5]))) : job.TestNodes;
            job.TestNodes = string.Equals(columns[0], "Generic Input") ? testNodes.Add(new TestNode(nodeCounter += 1, "GenericInputName", columns[6], Convert.ToInt32(columns[5]))) : job.TestNodes;
            job.Record = uniqueRecordId;
            job.CreationDate = creationDate;
            job.ModificationDate = creationDate;
            job.CreateBy = "System";
            job.ModifyBy = "System";
        }
        linecounter++;
    }
    return job;
}

Solution

  • Your use of the ternary operator...

    job.TestNodes = string.IsNullOrEmpty(columns[2]) ? job.TestNodes : testNodes.Add(new TestNode(nodeCounter += 1, "JobExecutor", columns[2], 0));
    

    ...effectively works like this...

    if (string.IsNullOrEmpty(columns[2]))
    {
        job.TestNodes = job.TestNodes;
    }
    else
    {
        job.TestNodes = testNodes.Add(new TestNode(nodeCounter += 1, "JobExecutor", columns[2], 0));
    }
    

    The List<>.Add() method has this signature...

    public void Add (T item);
    

    void means it doesn't return anything. You can't assign the result of a void method to some other storage because there is no result to assign. (Note that this is different than a method that happens to return null.)

    To fix this, perform your call to Add() and subsequent List<> assignment separately...

    if (string.IsNullOrEmpty(columns[2]))
    {
        job.TestNodes = job.TestNodes;
    }
    else
    {
        testNodes.Add(new TestNode(nodeCounter += 1, "JobExecutor", columns[2], 0));
        job.TestNodes = testNodes;
    }
    

    Since the true branch of the if block is effectively a no-op, we can rewrite that as simply...

    if (!string.IsNullOrEmpty(columns[2]))
    {
        testNodes.Add(new TestNode(nodeCounter += 1, "JobExecutor", columns[2], 0));
        job.TestNodes = testNodes;
    }
    

    Depending on how job.TestNodes gets assigned it might be necessary to instead do...

    job.TestNodes = testNodes;
    if (!string.IsNullOrEmpty(columns[2]))
    {
        testNodes.Add(new TestNode(nodeCounter += 1, "JobExecutor", columns[2], 0));
    }