Click here to Skip to main content
16,008,010 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
#define DEBUG
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConditionalAttribute
{
    public class MyClass : Attribute
    {
        [Conditional("DEBUG")]
        public static void Message(string msg)
        {
            Console.WriteLine(msg);
        }
    }
    class Test
    {
        static void function1()
        {
            MyClass.Message("In function 1.");
            function2();
        }
        static void function2()
        {
            MyClass.Message("In function 2:");
        }
        public static void Main(string[] args)
        {
            MyClass.Message("In Main function. ");
            function1();
            Console.ReadKey();
        }
    }
}
Posted
Comments
Derin J Tom 29-Sep-15 1:48am    
I am new to the C# world. I am learning it myself. I am getting an error when the classes are declared in the namespace. There were no errors when I removed the namespace. Please help.
What is that error?
Derin J Tom 29-Sep-15 3:59am    
Error 1 'ConditionalAttribute' is not an attribute class

I am not getting any error when the classes are declared without the namespace.

Please help.
Krunal Rohit 29-Sep-15 1:55am    
What error occurred ?

-KR
Derin J Tom 29-Sep-15 4:00am    
Error 1 'ConditionalAttribute' is not an attribute class

I am not getting any error when the classes are declared without the namespace.

Please help.

1 solution

Couple of things:
Don't use #define in C# - it doesn't work like it does in C or C++. #define values can only be used in #if and suchlike, not to affect "normal" code

You are deriving MyClass from the abstract class Attribute - but you aren't implementing any of the required methods.
Since you are a beginner, just remove the derivation:
C#
public class MyClass
{
    [Conditional("DEBUG")]
    public static void Message(string msg)
    {
        Console.WriteLine(msg);
    }
}
And ensure you are compiling the debug version of your code.

"Learning it myself" is not a good solution: get a book, or better go on a course - and follow it from the beginning to the end. Just trying to learn it as you go along is a good way to confuse yourself, as I think you have here. C# shares a lot of similar syntax with C and C++, but it is a very different language under the hood, and you shouldn't try to use C or C++ methodology and thinking or it will get very confusing!
 
Share this answer
 
Comments
Derin J Tom 29-Sep-15 10:21am    
Thank you :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900