How to compile only certain part of the code in .net.

Hi,
      I have a C# application in which i am using a Interop COM Dll properties of which are slightly different in two versions.I want  to have a single project  for this application which should support both version.But i now the version only at runtime. So i want to compile certain code based on the current version.How do i do that ?I tried with #if but could not succeed.Kindly help.
jisha123Asked:
Who is Participating?
 
Fernando SotoConnect With a Mentor RetiredCommented:
Hi jisha123;

When the program starts up set a class level variable to indicate which version is to be run during the execution. This way when you come to a point when you want to call a function from the dll insert an if statement to determin which one to run, something like the following.

        private bool ver1;

        private void Form1_Load(object sender, EventArgs e)
        {
            // Run a check to see which version to run
            // ....
            if (version == 1)
            {
                ver1 = true;
            }
            else
            {
                ver1 = false;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (ver1)
            {
                // Call the dll in version 1
            }
            else
            {
                // Call the dll in version 2
            }
        }

Fernando
0
 
existenz2Connect With a Mentor Commented:
In this case, it's better to use reflection to call the correct dll. More information about reflection can be found here:
http://my.execpc.com/~gopalan/dotnet/reflection.html

or just search on reflection here on EE. Enough questions have been asked about it :)
0
 
jisha123Author Commented:
Hi,
   Thank for your suggestion.But in my case i will be adding COM dll reference at design time.There are certain properties or methods in each dll (each version) which are not supported in the other.So i cannot just write a
if {} else statement because , irrespective of version both code will compile and methods or properties which are not supported will through error while compling.Kindly help in this.
0
 
existenz2Commented:
Actually a combination from FernandoSoto's answer and mine would solve your problem i think.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.