Home > OS >  CS0103 - The name 'GetListFromCVS' does not exist in the current context
CS0103 - The name 'GetListFromCVS' does not exist in the current context

Time:12-12

Pretty new to C# and object-oriented programming in general. I'm currently recieving this error when trying to call GetListFromCSV method that resides in another ".cs" file but resides in the same namespace. I'm not sure why I'm not able to call this method?

I originally had the code in GetListFromCSV method in main but wanted to put it in it's own class file to practice the SOLID principles. Maybe it doesn't make sense in this case?

Any help would be appreciated.

Thanks!

Main:

using MathNet.Numerics;
using System.Collections.Generic;

namespace SimPump_Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            // Get CSV file location
            string dirCSVLocation = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            string fileCSVLocation = dirCSVLocation   @"\PumpCurveCSVFiles\pumpcurve.csv";

            // Read CSV contents into variable "records"
            //CSVToList CSVIsntance = new CSVToList();
            List<PumpCurveCSVInput> records = GetListFromCVS(fileCSVLocation);

            //float pumpFlowOutput;
            double[] pumpFlow = new double[records.Count];
            double[] pumpHead = new double[records.Count];

            int i = 0;
            foreach (var record in records)
            {
                //if (record.pumpHead == 50)
                //{
                //    pumpFlowOutput = record.pumpFlow;
                //}

                pumpFlow[i] = record.pumpFlow;
                pumpHead[i] = record.pumpHead;

                i  ;
            }

            // Determine pump curve
            Polynomial.Fit(pumpFlow, pumpHead, 3);


        }
    }
}

GetListFromCSV Method:

using CsvHelper;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;

namespace SimPump_Demo
{
    public class CSVToList
    {
        public static List<PumpCurveCSVInput> GetListFromCVS(string fileCSV)
        {
            List<PumpCurveCSVInput> records;
            using (var reader = new StreamReader(fileCSV))
            using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
            {
                records = csv.GetRecords<PumpCurveCSVInput>().ToList();
            }

            return records;
        }
    }
}

CodePudding user response:

Even though GetListFromCVS is a static method, it still belongs to the CSVToList class. Therefore you must call it like this:

List<PumpCurveCSVInput> records = CSVToList.GetListFromCVS(fileCSVLocation);

Just use the name of the class without creating an instance.

If you make your method non-static

public class CSVToList
{
    public List<PumpCurveCSVInput> GetListFromCVS(string fileCSV)
    {
        // Your code
    }
}

In that case you should create an instance of the CSVToList class before being able to use this method

var csvHelper = new CSVToList();
List<PumpCurveCSVInput> records = csvHelper.GetListFromCVS(fileCSVLocation);
  • Related