Results 1 to 14 of 14

Thread: [RESOLVED] [2005] #define equivalent

  1. #1

    Thread Starter
    Hyperactive Member ZaNi's Avatar
    Join Date
    Jun 2006
    Location
    Australia
    Posts
    360

    Resolved [RESOLVED] [2005] #define equivalent

    In previous work in C++, I have used the #define preprocessor command to implement simple formulas throughout my code. For a (trivial) example:

    #define square(x) = x*x

    I want to do something similar (actually all I want is #define NoOfY = NoOfX - 1) to clarify for reading purposes the difference between two different stored variables. The second variable is always relative the the first variable (ie it is always X-1), so I don't want to maintain and update two seperate variables (I think extra variables leaves more scope for bugs and errors), but at the same time I don't want to have every reference to Y being X-1 because its reads easier as Y.

    I hope that all makes sense.

    Anyway, I tried looking for something similar, and I've seen you can set debug variables in the compiler settings for a project and you can also define things using #const, but both these only seem to be visible to #if statements.

    Is there some equivilent I can use to make the code more readable without having to maintain two variables?

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: [2005] #define equivalent

    Do you really need that ?
    As far as I know this is gonna be very hard to dom you'll need to use events for this
    just use NoOfX-1 instead of NoOfY.
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3

    Thread Starter
    Hyperactive Member ZaNi's Avatar
    Join Date
    Jun 2006
    Location
    Australia
    Posts
    360

    Re: [2005] #define equivalent

    I don't need it, but I'm trying to do an upper bound check for two seperate sets of data. The fact that one is proportional to the other is useful (so I can avoid having another variable), but it makes the code more confusing to read because suddenly I'm check the upper bound of one group against the current size of the other group. Its all about making the code easier to read but not making it harder to maintain at the same time.

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [2005] #define equivalent

    you can make a private class property, and return the other variable minus 1.

  5. #5

    Thread Starter
    Hyperactive Member ZaNi's Avatar
    Join Date
    Jun 2006
    Location
    Australia
    Posts
    360

    Re: [2005] #define equivalent

    Alrighty. Thx for the suggestions. Looks like its just a fundamental difference between c++ and vs

  6. #6
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [2005] #define equivalent

    Yes, because C/C++ has preprocessor macros and .NET does not, the only preprocessor instructions it has are conditional compilation (#If).

    Even in C++ I believe the use of #define is discouraged in favour of safer and stricter solutions - as are abound in .NET.

  7. #7
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Re: [2005] #define equivalent

    Quote Originally Posted by ZaNi
    Alrighty. Thx for the suggestions. Looks like its just a fundamental difference between c++ and vs
    well one bad way u can do this i guess is add a timer and make it do w/e u need constantly.

  8. #8
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [2005] #define equivalent

    Quote Originally Posted by high6
    well one bad way u can do this i guess is add a timer and make it do w/e u need constantly.
    If you know it's a bad way why suggest it?

  9. #9
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Re: [2005] #define equivalent

    Quote Originally Posted by penagate
    If you know it's a bad way why suggest it?
    well its not bad just not recommended but it still works.

  10. #10
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [2005] #define equivalent

    No, it is bad. Abusing timers is bad. It's inefficient, your app will go slow, your users will scream, you won't ship any copies.

  11. #11
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Re: [2005] #define equivalent

    Quote Originally Posted by penagate
    No, it is bad. Abusing timers is bad. It's inefficient, your app will go slow, your users will scream, you won't ship any copies.
    timers accually dont effect it in .net.

  12. #12
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [2005] #define equivalent

    Maybe not on your super fast rig. You try running it on a slow computer, .NET is slow to begin with, you don't need to make it worse. Even just think about the concept of it - running a piece of code constantly when you don't need to? Come on, of course it's inefficient.

    Bottom line is don't abuse things that are clearly designed for a single concept. Timers are designed for performing an action at a regular interval, nothing else. If you need to monitor something twice a second that's fine but if you want to wait for something to happen by continually checking if it has, that's not. There are so many better ways to do that there's no excuse for using a timer except laziness.

  13. #13
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: [2005] #define equivalent

    Here is your code:
    VB Code:
    1. Private NoOfX As Integer
    2.     Private ReadOnly Property NoOfY() As Integer
    3.         Get
    4.             Return NoOfX + 1
    5.         End Get
    6.     End Property
    and let's agree to disagree about timers
    Quote Originally Posted by high6
    timers actually don't effect it in .net.
    Any line of code you add to the program affects
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  14. #14
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Re: [2005] #define equivalent

    Quote Originally Posted by ComputerJy
    Here is your code:
    VB Code:
    1. Private NoOfX As Integer
    2.     Private ReadOnly Property NoOfY() As Integer
    3.         Get
    4.             Return NoOfX + 1
    5.         End Get
    6.     End Property
    and let's agree to disagree about timers

    Any line of code you add to the program affects
    i ment to say they dont effect it much for something so little.

    anyways lets put this all behind us.

    anyways awesome code. never knew about this.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width