views:

404

answers:

4

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

+3  A: 

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

Tzury Bar Yochay
@Tzury: You do not need to use the class name.
Grant Wagner
+2  A: 

The code looks okay - if that's your actual code, and not just a minimal example, you need to specify id (or some other variable) within the scope of RenderCompareStatus to pass as an argument to Found.

Mark Rushakoff
+3  A: 

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.

Grant Wagner
Thank you. Well is this working for you. That's what I did and I get a error in the following statement bool isFound = Found(id); (red sqiggly line under Found here)
sa
@sa: I tested this using `csc.exe` and Notepad, not Visual Studio, but you shouldn't be getting any compilation errors, it works as written. If you hover over `Found()` what does the popup say?
Grant Wagner
okay I shall try to do that. But 1 question:Can I call a static method in another static method which is in a static class in C#!!
sa
@sa: Your example is missing the closing `}` on `Class1`. Also, `Found()` has to return a `bool` (as in my example: `return false;`).
Grant Wagner
@sa: My example *is* calling a `static` method (`Found()`) from another `static` method (`RenderCompareStatus()`).
Grant Wagner
The pop says Class1 does not contain a definition for 'Found'
sa
This is the minimal code. But the actual code has { and the returns etc.
sa
@sa: All I can suggest is you edit your question, include the word EDIT:, then copy/paste the code as written from Visual Studio into the question. There must be a typo or some small issue that will only become apparent once we see your exact source code.
Grant Wagner
Grant, THank you. Yes it was a typo and this is the best forum ever.
sa
A: 

Both of your methods are lacking a return statement. You will get a compile error if you do not specify a return value for a non-void method.

It is ok to call a static method with Class1.Found(id). But if you call it from the same class, you can omit "Class1." before the call.

codymanix