PTR01
PTR01

Reputation: 83

Debug.WriteLine(string, params object[]) is a method but is used like a type

I'm currently working on a To-Do list project and I am at the ending stages of development, however my syntax for outputting to the debug seems to be incorrect, I'm not sure what the error is and any help would be appreciated.

As shown above, the error is CS0118:

'System.Diagnostics.Debug.WriteLine(string, params object[]) is a method but is used like a type'

#define testing
using System;
using System.Collections.Generic;
using System.Diagnostics;

#if(testing)
    public static string[] tempStringArr = new string[5] 
        { "Hey", "Greetings", "Hello", "Hi", "Example" };
    public static string tempKey = "Hello";

    public static int linearSearchTitleTest(string titleKey, string[] titleArr)
    { 
        for (int i = 0; i < titleArr.Length - 1; i++)
        {
            if (titleKey == titleArr[i])
            {
                return i;
            }
        }
        return -1;
    }

    int testResult = linearSearchTitleTest(tempKey, tempStringArr);
    Debug.WriteLine(Convert.ToString(testResult));
#endif

Upvotes: 2

Views: 640

Answers (3)

Beatles1692
Beatles1692

Reputation: 5320

Actually by using conditional compilation you are telling the compiler you want to compile which parts of your code and which parts you want not to compile and apparently these parts wont be in any form in the final compiled file. You can use reflector and other IL preview tools to check it .

Nevertheless after including and excluding these parts your code should be in correct format in the compiler view otherwise the compiler can't do its job.

In the other words #if , #else and #endif can not and will not change the compiler behavior whatsoever but they change the code before any compilation takes place.

Upvotes: 0

Bruno
Bruno

Reputation: 4665

Your Debug.WriteLine call is not in the scope of a function. Try to put this code in a function, maybe in main() for testing purpose?

int testResult = linearSearchTitleTest(tempKey, tempStringArr);
Debug.WriteLine(Convert.ToString(testResult));

For example :

class Program
{
    public static string[] tempStringArr = new string[5] { "Hey", "Greetings", "Hello", "Hi", "Example" };
    public static string tempKey = "Hello";

    static void Main(string[] args)
    {
        int testResult = linearSearchTitleTest(tempKey, tempStringArr);
        Debug.WriteLine(Convert.ToString(testResult));
    }

    public static int linearSearchTitleTest(string titleKey, string[] titleArr)
    {
        for (int i = 0; i < titleArr.Length - 1; i++)
        {
            if (titleKey == titleArr[i])
            {
                return i;
            }
        }
        return -1;
    }
}

Upvotes: 1

whoisj
whoisj

Reputation: 388

C# is an objective language. Meaning: all code must be part of an object.

Try wrapping your code in a namespace, a class, and a method. Usually this something like

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            // your code here ...
        }
    }
}

Upvotes: 1

Related Questions