0

I used VS2019 to write C# project in Windows7. There is a error when I tried to compile the project:

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

When I tried to used some recommended method from other similar questions, that is, add"static" to the definition of curLSUnits, new error occurs(Member 'ViewerController.curLSUnits' cannot be accessed with an instance reference; qualify it with a type name instead ImageExplorerServer), seems like I could not add "static".

What should I do to fix this "An object reference is required for the non-static field, method, or property 'ViewerController.curLSUnits' " problem?

viewcontroller.cs:

   public UnitData[] curLSUnits;
    public static void SaveToFile(int exportFileIndex, string argDBpath, 
     DataTable dt, int currentRow,int 
    m_IESTrayXTrayYValueEqualToTrayColTrayRow,int m_ExportDefectWithRedRec, 
    BackgroundWorker bgw = null)
    {
        try
        {
            if (exportFileIndex == 2)
            {                    
                string pathExptRpt = Path.GetDirectoryName(argDBpath) + 
       "\\ExportFolder\\" + Path.GetFileNameWithoutExtension(argDBpath) + 
           ".csv";                    
                ExportRpt.WriteDefToCsv(bgw, pathExptRpt, dt, 
          exportFileIndex, currentRow, 
           m_IESTrayXTrayYValueEqualToTrayColTrayRow, 
           m_ExportDefectWithRedRec, curLSUnits);   //WY200619
            } 
            else
            {
                string pathExptRpt = Path.GetDirectoryName(argDBpath) + "\\" 
           + Path.GetFileNameWithoutExtension(argDBpath) + ".xlsx";
                ExportRpt.WriteDefToExcel(bgw, pathExptRpt, dt, 
         exportFileIndex, currentRow, 
           m_IESTrayXTrayYValueEqualToTrayColTrayRow, 
           m_ExportDefectWithRedRec);
            }
        }
        catch (System.Exception ex)
        {
            debug.OutputDebugLogString("[IES] Temp debug log (SaveToFile):" 
    +ex.ToString() +  Environment.NewLine);             
        }
    }

exportRPT.cs:

       public static void WriteDefToCsv(BackgroundWorker bgw, string 
     pathSaveFile, DataTable dtDefDetails, int ExportFileIndex, int 
     currentRow, 
   int IESTrayXTrayYValueEqualToTrayColTrayRow, int ExportDefectWithRedRec, 
    UnitData[] curLSUnits)
    {
        try
        {
   
     
         
    System.IO.Directory.CreateDirectory(Path.GetDirectoryName(pathSaveFile));

            var csv = new StringBuilder();

            //Write Headers
            string Headers;
            SetDefectColumnTitile(out Headers);
            csv.AppendLine(Headers);
       ......       
Bill Yeung
  • 21
  • 5
  • `curLSUnits` is instance level. But `SaveToFile` is `static`. You need to make the former `static`, or the latter **not** `static`. – mjwills Jun 29 '20 at 02:12
  • @ mjwills have you seen my question? I have tried but error occurs.: Member 'ViewerController.curLSUnits' cannot be accessed with an instance reference; qualify it with a type name instead ImageExplorerServer – Bill Yeung Jun 29 '20 at 02:24
  • @mjwills I have tried make the former static, or the latter not static, but both will generate error – Bill Yeung Jun 29 '20 at 02:25
  • `qualify it with a type name instead ImageExplorerServer` Did you try that? – mjwills Jun 29 '20 at 04:00
  • @mjwills I have tried this ;private static ViewerController instance = new ViewerController(); ExportRpt.WriteDefToCsv(bgw, pathExptRpt, dt, exportFileIndex, currentRow, m_IESTrayXTrayYValueEqualToTrayColTrayRow, m_ExportDefectWithRedRec, instance.curLSUnits); But instance.curLSUnits is null – Bill Yeung Jun 29 '20 at 05:51

0 Answers0