Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# #define macro equivalent.

C# #define macro equivalent.

Scheduled Pinned Locked Moved C#
csharpc++
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    HAHAHA_NEXT
    wrote on last edited by
    #1

    I wonder if there is an equivalent of C# to the #define prepropcessor directive of C++. The one of C# is not very useful since you can not define macros. Can i use something else, which would perform the same functions. This will really clean my code.

    M 1 Reply Last reply
    0
    • H HAHAHA_NEXT

      I wonder if there is an equivalent of C# to the #define prepropcessor directive of C++. The one of C# is not very useful since you can not define macros. Can i use something else, which would perform the same functions. This will really clean my code.

      M Offline
      M Offline
      Mikko Puonti
      wrote on last edited by
      #2

      Simple answer: no, there isn't equivalant (but you can define symbols for compiler) Complex answer: Some solutions: 1) Develope own preprocessor that handles macros (shouldn't been too hard to do - but will make your code uncompatible) 2) Use #if (compiler symbols) to "select implementation". For example:

      	public abstract class DumpableObject : IDumpableObject
      	{
      #if DEBUG
      		private DumpContext m_contextDump;
      		private int m_iDumpLevel = DumpContext.DEFAULT_DUMP_LEVEL;
      				
      		protected int DumpLevel
      		{
      			get { return m_iDumpLevel; }
      			set { m_iDumpLevel = value; }
      		}
      				
      		public void DumpAll()
      		{				
      			DumpContext context = new DumpContext( this, m_iDumpLevel, DumpContext.DumpDetails.ALL );
      		}
      				
      		public void Dump( DumpContext context )
      		{
      			m_contextDump = context;
      			OnDump();
      			m_contextDump = null;
      		}
      				
      		protected virtual void OnDump()
      		{
      		}
      				
      		protected void Dump( string strName, object obj )
      		{
      			m_contextDump.Dump( strName, obj );
      		}
      #else
      		protected int DumpLevel
      		{
      			get { return 0; }
      			set { }
      		}
      				
      		protected void DumpAll()
      		{
      		}
      				
      		public void Dump( DumpContext context )
      		{
      		}
      				
      		protected virtual void OnDump()
      		{
      		}
      				
      		protected void Dump( string strName, object obj )
      		{
      		}
      #endif
      	}
      

      (Yes I know - it doesn't implement all macro tricks - but at least some). Basicly you really should be able to do almost anything without macros (just use sub methods).

      H 1 Reply Last reply
      0
      • M Mikko Puonti

        Simple answer: no, there isn't equivalant (but you can define symbols for compiler) Complex answer: Some solutions: 1) Develope own preprocessor that handles macros (shouldn't been too hard to do - but will make your code uncompatible) 2) Use #if (compiler symbols) to "select implementation". For example:

        	public abstract class DumpableObject : IDumpableObject
        	{
        #if DEBUG
        		private DumpContext m_contextDump;
        		private int m_iDumpLevel = DumpContext.DEFAULT_DUMP_LEVEL;
        				
        		protected int DumpLevel
        		{
        			get { return m_iDumpLevel; }
        			set { m_iDumpLevel = value; }
        		}
        				
        		public void DumpAll()
        		{				
        			DumpContext context = new DumpContext( this, m_iDumpLevel, DumpContext.DumpDetails.ALL );
        		}
        				
        		public void Dump( DumpContext context )
        		{
        			m_contextDump = context;
        			OnDump();
        			m_contextDump = null;
        		}
        				
        		protected virtual void OnDump()
        		{
        		}
        				
        		protected void Dump( string strName, object obj )
        		{
        			m_contextDump.Dump( strName, obj );
        		}
        #else
        		protected int DumpLevel
        		{
        			get { return 0; }
        			set { }
        		}
        				
        		protected void DumpAll()
        		{
        		}
        				
        		public void Dump( DumpContext context )
        		{
        		}
        				
        		protected virtual void OnDump()
        		{
        		}
        				
        		protected void Dump( string strName, object obj )
        		{
        		}
        #endif
        	}
        

        (Yes I know - it doesn't implement all macro tricks - but at least some). Basicly you really should be able to do almost anything without macros (just use sub methods).

        H Offline
        H Offline
        HAHAHA_NEXT
        wrote on last edited by
        #3

        The problem with this method is that my code size actually Increases very rapidely. I wonder how come there is no cheap tricks. Maybe I can Use attributes for that ? But i will probably have to create a preprocessor directive myself :(( X|

        L 1 Reply Last reply
        0
        • H HAHAHA_NEXT

          The problem with this method is that my code size actually Increases very rapidely. I wonder how come there is no cheap tricks. Maybe I can Use attributes for that ? But i will probably have to create a preprocessor directive myself :(( X|

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #4

          HAHAHA_NEXT wrote: But i will probably have to create a preprocessor directive myself I itch for that too :) I use it (#if) alot. To simulate the enviroment, eg:

          #define HELLO

          bool Blah(string foo)
          {
          #if HELLO
          return true;
          #elseif
          return false;
          #endif
          }

          top secret AdvancedTextBox

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups