[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?
Re: [2005] #define equivalent
Do you really need that :confused: ?
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.
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.
Re: [2005] #define equivalent
you can make a private class property, and return the other variable minus 1.
Re: [2005] #define equivalent
Alrighty. Thx for the suggestions. Looks like its just a fundamental difference between c++ and vs
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.
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.
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?
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.
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. :)
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.
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.
Re: [2005] #define equivalent
Here is your code:
VB Code:
Private NoOfX As Integer
Private ReadOnly Property NoOfY() As Integer
Get
Return NoOfX + 1
End Get
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
Re: [2005] #define equivalent
Quote:
Originally Posted by ComputerJy
Here is your code:
VB Code:
Private NoOfX As Integer
Private ReadOnly Property NoOfY() As Integer
Get
Return NoOfX + 1
End Get
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.