Can't call class's method c#

Viewed 93

I know I'm missing something obvious but for some reason I can't call a method from a class. I have one file named Searches with a SearchGroup method in it. I am attempting to call that method from another file by the following, but it's saying

'Searches' does not contain a definition for 'SearchGroup' :

List<Searches.NameScore> People = Searches.SearchGroup("Test");

My Searches class looks like:

public class Searches
    {
        private readonly IConfiguration _config;

        public Searches(IConfiguration configuration)
        {
            _config = configuration;
        }
        
        public class FirstLoadGroup
        {
            public int IdGroup { get; set; }
            public string Name { get; set; }
        }
        public class NameScore
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public double Score { get; set; }
        }        
        public async Task<List<NameScore>> SearchGroup(string Input)
        {            
           //processy stuff

            //Querying stuff
            DataAccess data = new DataAccess();
            string sql = "SELECT IdGroup, Name FROM Groups";
            List<FirstLoadGroup> Names = await data.LoadData<FirstLoadGroup, dynamic>(sql, new { }, _config.GetConnectionString("DataDB"));

            //Sorting and Comparing stuff
            return SortedNames;

            //return list of names in order of lowest to highest matching score
        }
    }
3 Answers

First up take those classes out of searches (generally you should avoid declaring classes inside other classes):

public class Searches
{
    private readonly IConfiguration _config;

    public Searches(IConfiguration configuration)
    {
        _config = configuration;
    }
           
    public async Task<List<NameScore>> SearchGroup(string Input)
    {            
       //processy stuff

        //Querying stuff
        DataAccess data = new DataAccess();
        string sql = "SELECT IdGroup, Name FROM Groups";
        List<FirstLoadGroup> Names = await data.LoadData<FirstLoadGroup, dynamic>(sql, new { }, _config.GetConnectionString("DataDB"));

        //Sorting and Comparing stuff
        return SortedNames;

        //return list of names in order of lowest to highest matching score
    }
}

    
    public class FirstLoadGroup
    {
        public int IdGroup { get; set; }
        public string Name { get; set; }
    }
    public class NameScore
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public double Score { get; set; }
    }        
     

Then you need to be calling your method like:

Searches s = new Searches(some config)
List<NameScore> people = await s.SearchGroup("Test");

That said, if you have a property like:

public Searches Searches { get; set; }

..then Searches.SearchGroup is acceptable, but the fact that C# seems to know what Searches is but not that it has a SearchGroup method would imply that you have two+ classes called Searches and the one it's found for your code's context is not the one with the SearchGroup method. Check your usings and aliases

You can only call static methods without any instance of the class. To call SearchGroup method, you either

  1. make it a static method

    public static async Task<List> SearchGroup(string Input)

  2. or create a instance of the call and then call method on it.

    s = new Searches(); await s.SearchGroup("Test");

Try using await

List<Searches.NameScore> People = await Searches.SearchGroup("Test");
Related