[Solved] How to call a method in a method?


You can do it almost exactly as you described, you were just lacking a few bits. After some minimal fixing, your code would be:

public String MapFinder()
{
    if ((Map.Width == 8 && Map.Height==8))
    {
        return "DefaultMap";
    }
    else
        return "Something Different";
}

public String MapTracker()
{
    if( MapFinder() == "DefaultMap" ) // <- change
    {
        return "Hello DefaultMap";
    }
    else
    {
        return "Hello StrangeMap"; // <- change
    }
}

I’ve marked the changes. I’ve made three:

  • fixed typo: you’ve got a ; in first if instead of )
  • fixed typo: you’ve got an unclosed quotes " in strangemap
  • I’ve replaced StringFromMapFinder with a method call

However, usually, you will rather want to store the result of that call somewhere and later inspect it:

public String MapTracker()
{
    String mapFinderResult;
    mapFinderResult = MapFinder();
    if( mapFinderResult == "DefaultMap" )
    {
        return "Hello DefaultMap";
    }
    else
    {
        return "Hello StrangeMap";
    }
}

Here, I’ve created a variable named mapFinderResult, the I’ve called the method and stored the result in that variable, and then in if I’ve used the variable to check what was returned. This longer version is just the same as previous, just with the difference that the result is stored in variable instead of being directly used in if condition.

I won’t describe it more because I’d need to write a very long lesson. Please fetch some C# tutorial and read further about methods calling methods and using variables.

0

solved How to call a method in a method?