C++ to VB conversion and preprocessor definition

G

Guest

I am contemplating converting a huge collection of functions from C++ to
VB.NET.
I am concerned with a style used in the original code where a variable is
set to a constant value with the # define preprocessor symbol. For example:

void Mysub (...){
# define indx 2
double x[indx] = {2.3, 3.4};
blah blah;
# undef indx
}

This all seems silly to me and I would like to replace it (in fact I'll have
to replace it) with something like
dim x(2) as double or even

dim i as int = 2
dimx(i) as double

Does anyone know the reason behind this define/undefine preprocessor symbols
stuff? Can anyone see any harm in my replacing them with standard variables
having procedure scope?
 
R

rowe_newsgroups

I am contemplating converting a huge collection of functions from C++ to
VB.NET.
I am concerned with a style used in the original code where a variable is
set to a constant value with the # define preprocessor symbol. For example:

void Mysub (...){
# define indx 2
double x[indx] = {2.3, 3.4};
blah blah;
# undef indx

}

This all seems silly to me and I would like to replace it (in fact I'll have
to replace it) with something like
dim x(2) as double or even

dim i as int = 2
dimx(i) as double

Does anyone know the reason behind this define/undefine preprocessor symbols
stuff? Can anyone see any harm in my replacing them with standard variables
having procedure scope?
Does anyone know the reason behind this define/undefine preprocessor symbols
stuff? Can anyone see any harm in my replacing them with standard variables
having procedure scope?

I don't see any reason to not replace it, though I'm not a C++
programmer and I don't know if what happens in "blah blah;" so I could
be wrong :)

Thanks,

Seth Rowe
 
O

\(O\)enone

mark said:
Does anyone know the reason behind this define/undefine preprocessor
symbols stuff? Can anyone see any harm in my replacing them with
standard variables having procedure scope?

The #define preprocessor definition tells the compiler to effectively
perform a search and replace on the sourcecode as it is compiling, replacing
all instances of the defined keyword with the value to which it has been
defined. So in your example, the actual code that gets compiled is:

\\\
void Mysub (...){
double x[2] = {2.3, 3.4};
blah blah;
}
///

The advantages of using #define over just putting the value directly into
the source code are:

- you can refer to the defined value multiple times, allowing all of the
references to be changed by simply changing the #define value
- you can give a name to the value which identifies what it actually means
("indx" may be more meaningful than the value 2)
- you can use more complex #defines that put calculations, calls to
functions, etc. into the value and then use these as if they were functions
in the code

In this particular instance, all it's doing is defining a constant. The VB
equivalent to this would be:

\\\
Public Sub MySub
Const indx As Integer = 2
Dim x(indx) As Double
blah blah
End Sub
///

....although remember that in C++, declaring an array as x[2] will create an
array with two elements, with indices 0 and 1. In VB.NET it'll create an
array with three elements, with indices 0, 1 and 2, so you may wish to use
"Dim x(indx-1)" to keep exactly the same functionality as in the original
C++ code.

HTH,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top