0

I am running into below error at line tblSoftwareImageTestPlan.SoftwareImageTestPlanID = SoftwareImageTestPlanData.SoftwareImageTestPlanID,how can I fix this error?

public List<SoftwareImageTestPlan>  AddSoftwareImageRecord(IEnumerable<SoftwareImageTestPlan> SoftwareImageTestPlans_WithParticularSoftwareImageID)
{
    tblSoftwareImageTestPlan SoftwareImageTestPlan = new tblSoftwareImageTestPlan();

    foreach (var SoftwareImageTestPlanData in SoftwareImageTestPlans_WithParticularSoftwareImageID)
    {
        tblSoftwareImageTestPlan.SoftwareImageTestPlanID = SoftwareImageTestPlanData.SoftwareImageTestPlanID;//error at line
    }

    return null;
}

Error:

An object reference is required for the non-static field,method,or property 'tblSoftwareImageTestPlan.SoftwareImageTestPlanID'

Craig W.
  • 17,838
  • 6
  • 49
  • 82
carte blanche
  • 10,796
  • 14
  • 46
  • 65
  • Rob - I looked at it,its not clear to me how to fix this error,can you provide guidance? – carte blanche Dec 21 '16 at 00:14
  • @Rob that is not the problem. The problem is that he is using the class instead of the instance in the for. – Klinger Dec 21 '16 at 00:18
  • 1
    Please don't start local variables with a capital letter. – itsme86 Dec 21 '16 at 00:18
  • 2
    This is not a duplicate, just a silly error. Inside the for change 'tblSoftwareImageTestPlan.SoftwareImageTestPlanID = ...' to 'SoftwareImageTestPlan.SoftwareImageTestPlanID = ...'. – Klinger Dec 21 '16 at 00:19
  • 1
    This code is confusing to look at because the naming conventions are exactly backwards: Class names should be title case, local variables should be camel case: e.g. `TblSoftwareImageTestPlan softwareImageTestPlan = new TblSoftwareImageTestPlan();` – Blorgbeard Dec 21 '16 at 00:33
  • It's also unclear why you're assigning to `tblSoftwareImageTestPlan.SoftwareImageTestPlanID` in a loop; it's only going to have the final value after the loop is done. – Blorgbeard Dec 21 '16 at 00:35

1 Answers1

3

You are using the class instead of the instance, change to as follows:

    tblSoftwareImageTestPlan SoftwareImageTestPlan = new tblSoftwareImageTestPlan();

    foreach (var SoftwareImageTestPlanData in SoftwareImageTestPlans_WithParticularSoftwareImageID)
    {
        SoftwareImageTestPlan.SoftwareImageTestPlanID = SoftwareImageTestPlanData.SoftwareImageTestPlanID;//error at line
    }

Also, your code is not following "conventional" C# coding convention. See here for Microsoft guidelines on this, and also here

Klinger
  • 4,900
  • 1
  • 30
  • 35