Can a static method be called inside another method?

The main question is how can I call a static method inside another method. Please, help!

public static class Class1
{
  public static string RenderCompareStatus()
  {
      bool isFound = Class1.Found(id);
  }

  private static bool Found(string id)
  {

  }

      

// Error message: does not contain definition for found

+2


source to share


4 answers


I've expanded the sample into a fully working example:

using System;

public static class Class1
{
    public static void Main()
    {
        Console.WriteLine(RenderCompareStatus());
    }

    public static string RenderCompareStatus()
    {
        String id = "test";
        bool isFound = Found(id);
        return "Test: " + isFound;
    }

    private static bool Found(string id)
    {
        return false;
    }
}

      

And the results:



Test: False

      

EDIT: If the above example is similar to your code, but your code does not work, edit your question to provide more details, such as the exact error, getting, and a more complete sample of the code causing the error.

EDIT: changed public static bool Found(string id)

to private static bool Found(string id)

recompiled and it still works.

+5


source


You use the class name for example Class1.Found



+3


source


The code looks ok - if your actual code is not just a minimal example, you need to specify id

(or some other variable) in the framework RenderCompareStatus

to pass as an argument Found

.

+2


source


Both methods lack a return statement. You will get a compilation error if you do not provide a return value for a non-empty method.

You can call a static method with Class1.Found (id). But if you are calling it from one class, you can omit "Class1". before calling.

0


source







All Articles