0

I'm trying to call two functions, ShowInventories and DoTransaction. Both of these functions are inside a '#if'. I'm not sure what a '#if' means or if there is a specif way to call a function from it.

I've simply tried to call the functions ShowInventories and DoTransaction and then running what I need to through them like this:

ShowInventories(Thing being sent through);

DoTransaction(Thing being sent through);

The #if statement looks like this:

#if INVENTORY
static void ShowInventories(string playerName, Inventory playerInv, Inventory storeInv)
{
    Console.SetCursorPosition(2, 2);
    Console.Write(playerName + "'s inventory: ");
    playerInv.DisplayInventory(4, 3);

    Console.SetCursorPosition(39, 2);
    Console.Write("The Store's inventory: ");
    storeInv.DisplayInventory(41, 3);
}

static void DoTransaction(string playerName, Inventory playerInv, Inventory storeInv, bool buy)
{
    if (buy)
    {
        DoBuy(playerName, playerInv, storeInv);
    }
    else
    {
        DoSell(playerName, playerInv, storeInv);
    }
}
...
#endif

I didn't know if the code inside of two function matters but I put them in either way. They are calling other functions somewhere else in the code but I'm not sure that it matters to include.

The problem is that it is telling me that 'both function do not exist in the current context' even through the '#if' is right underneath this code. I also do not understand what '#if' is, if someone could help clear things up for me I would really appreciate it. Thank you in advanced.

RiderJon
  • 57
  • 7
  • Where is the `# if` statement that declares these two methods. The identifier after #if is required to create the post-call condition to your methods. – Augusto Vasques May 01 '19 at 00:54
  • Not really an answer but you likely should use [conditional attributes](https://stackoverflow.com/questions/3788605/if-debug-vs-conditionaldebug) instead when you do that at function level... Also some refactoring may let you remove those conditional compilation and use alternative implementations of an interface instead. – Alexei Levenkov May 01 '19 at 01:17

2 Answers2

2

#if is what we call a preprocessor directive. What that means is, when your code is compiled, the compiler will only include what is inside

 #if DEBUG
  .....
 #endif

when your project has the property DEBUG declared (which it usually will if it is in DEBUG mode for example).

So in your code you have

 #if INVENTORY
     static void ShowInventories(string playerName, Inventory playerInv, Inventory storeInv)
     {
     ......
     }
 #endif

but because your project properties do not declare INVENTORY, that code inside #if is not included and the compiler cannot find the methods.

Jim W
  • 4,866
  • 1
  • 27
  • 43
2

This sounds like a preprocessor directive, the code included between the #if and the #endif will only be compiled if the relevant symbol is declared. Different symbols will be declared for various build configurations.

Often the DEBUG symbol is declared only in the Debug build configuration and not the Release configuration. This can allow easy omission of expensive profiling code for release builds.

Normally Visual Studio will grey out code when the symbol is missing. You will get compiler errors if you try and access this code from a build that does not include it.

Be careful as excessive use of these directives may be a code smell, see Quote needed: Preprocessor usage is bad OO practice

Dave Anderson
  • 11,836
  • 3
  • 58
  • 79