Conditional Attribute on Method when the condition is met

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Frank Munsberg

    #1

    Conditional Attribute on Method when the condition is met

    Hi There,

    I'm currently in a mild state of confusion about the Conditional
    Attribute. The code below is taken from the implementation of "Design
    By Contract" that I've put in somewhere in my project. As you might
    expect the code outputs "DBC_CHECK_ ALL is defined" when
    Check.Require(. ..) is called and the project is compiled with DEBUG
    settings.

    #define DBC_CHECK_ALL

    public public sealed class Check
    {
    [Conditional("DE BUG")]
    public static void Require(bool assertion, string message)
    {
    #if DBC_CHECK_ALL
    Console.WriteLi ne("DBC_CHECK_A LL is defined");
    #endif
    //...<some code that needs to be executed>...
    }
    //...<some other functions>...
    }

    Now if I replace the [Conditional("DE BUG")] with
    [Conditional("DB C_CHECK_ALL")] the whole method isn't executed
    anymore. Can anyone explain to me why this is happening or what on
    earth I'm missing here?
    I suspect its something blatantly obvious but I can't find it for the
    life of me.

    Greetings
    Frank
  • qglyirnyfgfo@mailinator.com

    #2
    Re: Conditional Attribute on Method when the condition is met

    It works for me, below is the exact code that I used after crating a
    brand new Console application. If I am missing the point, it may help
    if you post a small but complete working example that reproduces your
    problem.

    ----------------------------
    #define DBC_CHECK_ALL

    using System;
    using System.Collecti ons.Generic;
    using System.Text;
    using System.Diagnost ics;

    namespace ConsoleApplicat ion326
    {
    class Program
    {
    static void Main(string[] args)
    {
    Check.Require() ;
    Console.Read();
    }
    }

    public sealed class Check
    {
    [Conditional("DB C_CHECK_ALL")]
    public static void Require()
    {
    Console.WriteLi ne("Inside Require");

    #if DBC_CHECK_ALL
    Console.WriteLi ne("DBC_CHECK_A LL is defined");
    #endif

    }
    }
    }
    ----------------------------


    On Jun 4, 7:15 am, Frank Munsberg <bufferunder... @gmx.dewrote:
    Hi There,
    >
    I'm currently in a mild state of confusion about the Conditional
    Attribute. The code below is taken from the implementation of "Design
    By Contract" that I've put in somewhere in my project. As you might
    expect the code outputs "DBC_CHECK_ ALL is defined" when
    Check.Require(. ..) is called and the project is compiled with DEBUG
    settings.
    >
    #define DBC_CHECK_ALL
    >
    public public sealed class Check
    {
    [Conditional("DE BUG")]
    public static void Require(bool assertion, string message)
    {
            #if DBC_CHECK_ALL
            Console.WriteLi ne("DBC_CHECK_A LL is defined");
            #endif
    //...<some code that needs to be executed>...}
    >
    //...<some other functions>...
    >
    }
    >
    Now if I replace the [Conditional("DE BUG")] with
    [Conditional("DB C_CHECK_ALL")] the whole method isn't executed
    anymore. Can anyone explain to me why this is happening or what on
    earth I'm missing here?
    I suspect its something blatantly obvious but I can't find it for the
    life of me.
    >
    Greetings
    Frank

    Comment

    • Ignacio Machin ( .NET/ C# MVP )

      #3
      Re: Conditional Attribute on Method when the condition is met

      On Jun 4, 8:15 am, Frank Munsberg <bufferunder... @gmx.dewrote:
      Hi There,
      >
      I'm currently in a mild state of confusion about the Conditional
      Attribute. The code below is taken from the implementation of "Design
      By Contract" that I've put in somewhere in my project. As you might
      expect the code outputs "DBC_CHECK_ ALL is defined" when
      Check.Require(. ..) is called and the project is compiled with DEBUG
      settings.
      >
      #define DBC_CHECK_ALL
      >
      public public sealed class Check
      {
      [Conditional("DE BUG")]
      public static void Require(bool assertion, string message)
      {
              #if DBC_CHECK_ALL
              Console.WriteLi ne("DBC_CHECK_A LL is defined");
              #endif
      //...<some code that needs to be executed>...}
      >
      //...<some other functions>...
      >
      }
      >
      Now if I replace the [Conditional("DE BUG")] with
      [Conditional("DB C_CHECK_ALL")] the whole method isn't executed
      anymore. Can anyone explain to me why this is happening or what on
      earth I'm missing here?
      I suspect its something blatantly obvious but I can't find it for the
      life of me.
      >
      Greetings
      Frank
      Work here

      Just note that if you declare your #define in a file it ONLY exist in
      that particular file

      Comment

      • ssg31415926

        #4
        Re: Conditional Attribute on Method when the condition is met

        It's gotta be a typo!. I created a new console project in VS2005 and
        pasted in your code:

        #define DBC_CHECK_ALL

        using System;
        using System.Collecti ons.Generic;
        using System.Text;
        using System.Diagnost ics;

        namespace ConditionalAttr ibutes
        {
        public sealed class Program
        {
        static void Main(string[] args)
        {
        Require(true, "fred");

        Console.WriteLi ne("\r\nPress a key to continue...");
        Console.ReadKey (true);

        }

        //[Conditional("DE BUG")]
        [Conditional("DB C_CHECK_ALL")]
        public static void Require(bool assertion, string message)
        {
        #if DBC_CHECK_ALL
        Console.WriteLi ne("DBC_CHECK_A LL is defined");
        #endif
        //...<some code that needs to be executed>...
        }

        //...<some other functions>...
        }
        }

        It works fine with either ConditionalAttr ibute defined, as you'd
        expect but not as you saw.


        On 4 Jun, 13:15, Frank Munsberg <bufferunder... @gmx.dewrote:
        Hi There,
        >
        I'm currently in a mild state of confusion about the Conditional
        Attribute. The code below is taken from the implementation of "Design
        By Contract" that I've put in somewhere in my project. As you might
        expect the code outputs "DBC_CHECK_ ALL is defined" when
        Check.Require(. ..) is called and the project is compiled with DEBUG
        settings.
        >
        #define DBC_CHECK_ALL
        >
        public public sealed class Check
        {
        [Conditional("DE BUG")]
        public static void Require(bool assertion, string message)
        {
        #if DBC_CHECK_ALL
        Console.WriteLi ne("DBC_CHECK_A LL is defined");
        #endif
        //...<some code that needs to be executed>...}
        >
        //...<some other functions>...
        >
        }
        >
        Now if I replace the [Conditional("DE BUG")] with
        [Conditional("DB C_CHECK_ALL")] the whole method isn't executed
        anymore. Can anyone explain to me why this is happening or what on
        earth I'm missing here?
        I suspect its something blatantly obvious but I can't find it for the
        life of me.
        >
        Greetings
        Frank

        Comment

        • ssg31415926

          #5
          Re: Conditional Attribute on Method when the condition is met

          If anyone else is looking, here are a bunch of references I found.
          The first two are definitely worth a look:



          http://msdn.microsoft.com/en-gb/magazine/cc188766.aspx

          http://blogs.msdn.com/dotnetinterop/...-wpf-apps.aspx





          http://episteme.arstechnica.com/eve/...m/319006601931

          Although FTP is commonly used to transfer files over the Internet, it's somewhat unfriendly to firewalls. Phil Wilson shows you how to create a BITS upload program using C#, which will transfer files over browser ports.








          On 4 Jun, 16:48, "Ignacio Machin ( .NET/ C# MVP )"
          <ignacio.mac... @gmail.comwrote :
          On Jun 4, 8:15 am, Frank Munsberg <bufferunder... @gmx.dewrote:
          >
          >
          >
          Hi There,
          >
          I'm currently in a mild state of confusion about the Conditional
          Attribute. The code below is taken from the implementation of "Design
          By Contract" that I've put in somewhere in my project. As you might
          expect the code outputs "DBC_CHECK_ ALL is defined" when
          Check.Require(. ..) is called and the project is compiled with DEBUG
          settings.
          >
          #define DBC_CHECK_ALL
          >
          public public sealed class Check
          {
          [Conditional("DE BUG")]
          public static void Require(bool assertion, string message)
          {
          #if DBC_CHECK_ALL
          Console.WriteLi ne("DBC_CHECK_A LL is defined");
          #endif
          //...<some code that needs to be executed>...}
          >
          //...<some other functions>...
          >
          }
          >
          Now if I replace the [Conditional("DE BUG")] with
          [Conditional("DB C_CHECK_ALL")] the whole method isn't executed
          anymore. Can anyone explain to me why this is happening or what on
          earth I'm missing here?
          I suspect its something blatantly obvious but I can't find it for the
          life of me.
          >
          Greetings
          Frank
          >
          Work here
          >
          Just note that if you declare your #define in a file it ONLY exist in
          that particular file

          Comment

          • Frank Munsberg

            #6
            Re: Conditional Attribute on Method when the condition is met

            I hope this doesn't become a double posting.

            anyway, I just did the same as everybody else and moved the function
            with the conditionals into a small console app. That worked like a
            charm so things are not totally broken.
            Then I made it a class library to call the Require function via an
            NUnit Test function (2.4.6) because the real project is run by NUnit
            too at this point.

            When run from NUnit the results are the same as in the real project so
            somehow NUnit is messing with either the #define at the top of the cs
            file or with the Conditional. It works when I use the
            Conditinoal("DE BUG") but not the DBC_CHECK_ALL. At least I've tracked
            the culprit down but I still don't know why this is happening. Does
            anyone have an idea?

            Comment

            • ssg31415926

              #7
              Re: Conditional Attribute on Method when the condition is met

              Sorry. Had umpteen tabs open in Firefox and clicked reply in the
              wrong one - was wondering what had happend to my reply to the other
              thread.

              Have you tried the NUnit mailing list - the guys there can be really
              helpful.


              On 5 Jun, 08:46, Frank Munsberg <bufferunder... @gmx.dewrote:
              I hope this doesn't become a double posting.
              >
              anyway, I just did the same as everybody else and moved the function
              with the conditionals into a small console app. That worked like a
              charm so things are not totally broken.
              Then I made it a class library to call the Require function via an
              NUnit Test function (2.4.6) because the real project is run by NUnit
              too at this point.
              >
              When run from NUnit the results are the same as in the real project so
              somehow NUnit is messing with either the #define at the top of the cs
              file or with the Conditional. It works when I use the
              Conditinoal("DE BUG") but not the DBC_CHECK_ALL. At least I've tracked
              the culprit down but I still don't know why this is happening. Does
              anyone have an idea?

              Comment

              • Frank Munsberg

                #8
                Re: Conditional Attribute on Method when the condition is met

                Heh, window madness :)
                I'll try posting something there. If nothing helps then I'll use some
                workaround.
                enclosing the code inside the function(s) with #if DBC_CHECK_ALL <...>
                #endif or something like that should also do the trick. Even if its
                messy its better than nothing.

                Comment

                Working...