_MSC_VER

R

RedLars

I would like to execute different code depending on whether using
vs2002 or vs2008. I have seen other articles refer to _MSC_VER but I
do not understand how to use this variable using C#.

The following code I must admit is based on a C++ example I found
(shame on me):

private void Foo()
{
#if (_MSC_VER >= 1500)
// VS2008 code
#else
// VS2002 code
#endif
}

The code generates the following error message when I try to compile
it:
") expected"
"Single-line comment or end-of-line expected".
The first error message refers to the '>' sign.

I am not familiar with using preprocessor directives in C#. How could
I solve this issue?
 
J

Jeff Johnson

I would like to execute different code depending on whether using
vs2002 or vs2008.

The #if family of preprocessor directives is for COMPILING your code. Since
you said you want to EXECUTE different code, it suggests to me that you're
trying to make this happen at run-time. If so, you can't do it this way,
because, as I said, they only work when compiling, and therefore the
resulting executable will only behave one way.
 
R

RedLars

The #if family of preprocessor directives is for COMPILING your code. Since
you said you want to EXECUTE different code, it suggests to me that you're
trying to make this happen at run-time. If so, you can't do it this way,
because, as I said, they only work when compiling, and therefore the
resulting executable will only behave one way.

Sorry, I meant compile
 
J

Jeff Johnson

Sorry, I meant compile

From MSDN:

#if lets you begin a conditional directive, testing a symbol or symbols to
see if they evaluate to true.

In other words, the symbol itself (_MSC_VER in your case) must represent a
Boolean value. You're trying to treat it as an integer and compare it to a
value. This is not allowed in C#. So simply put, you can't do it the way
you're trying to do it. If you only have two possibilities (VS2008 and
VS2002) then I recommend creating a symbol called VS2008 and only using it
when compiling to that target, so your code would look like this:

private void Foo()
{
#if (VS2008)
// VS2008 code
#else
// VS2002 code
#endif
}
 
P

Peter Duniho

RedLars said:
[...]
private void Foo()
{
#if (_MSC_VER >= 1500)
// VS2008 code
#else
// VS2002 code
#endif
}

The code generates the following error message when I try to compile
it:
") expected"
"Single-line comment or end-of-line expected".
The first error message refers to the '>' sign.

I am not familiar with using preprocessor directives in C#. How could
I solve this issue?

As Jeff says, C# #if directives don't allow for comparisons like that.
Either the preprocessor name is defined or it's not.

That said, it's doubtful that you need version-specific behavior in your
code. Without knowing exactly what the code is, it's not possible to
say for sure. But C# isn't C++ and so whatever conditional thing
someone thought they needed in C++, it's unlikely they need it in C#
(they might not have even needed it in C++, but that's neither here nor
there).

Pete
 
R

RedLars

RedLars said:
[...]
private void Foo()
{
   #if (_MSC_VER >= 1500)
           // VS2008 code
   #else
           // VS2002 code
   #endif
}
The code generates the following error message when I try to compile
it:
") expected"
"Single-line comment or end-of-line expected".
The first error message refers to the '>' sign.
I am not familiar with using preprocessor directives in C#. How could
I solve this issue?

As Jeff says, C# #if directives don't allow for comparisons like that.
Either the preprocessor name is defined or it's not.

That said, it's doubtful that you need version-specific behavior in your
code.  Without knowing exactly what the code is, it's not possible to
say for sure.  But C# isn't C++ and so whatever conditional thing
someone thought they needed in C++, it's unlikely they need it in C#
(they might not have even needed it in C++, but that's neither here nor
there).

Pete

Thank you Jeff and Peter for the excellent feedback.
 

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