Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I call a static method inside another method?

fundamental question: 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 Answers

I expanded your 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 is not working, please edit your question, supplying more details such as the precise error you are getting and a more complete sample of the code that is producing the error.

EDIT: Changed public static bool Found(string id) to private static bool Found(string id) recompiled and it still works.

like image 195
Grant Wagner Avatar answered Mar 28 '26 17:03

Grant Wagner


You use the class name e.g. Class1.Found

like image 42
Tzury Bar Yochay Avatar answered Mar 28 '26 17:03

Tzury Bar Yochay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!