title: Check If Variable Exist
body: <p>I'm building a program in C# using Visual Studio 2010. And was wondering if there's a way to check it a variable exist.</p>
  
  <p>So basically, my <code>main.cs</code> is calling several other files (<code>file1.cs</code>, <code>file2.cs</code>, <code>file3.cs</code>, etc). Each of this files are very similar but some are missing some variables.</p>
  
  <p>For example, <code>var v1</code> exists in <code>file1.cs</code> and <code>file2.cs</code>, but not <code>file3.cs</code></p>
  
  <p>In my <code>main.cs</code>, it's something like this.</p>
  
  <pre><code>string a = MyProgram.file1.v1;
  string b = MyProgram.file2.v1;
  string c = MyProgram.file3.v1; //will break here
  </code></pre>
  
  <p>I know the above code probably won't work at all, but that was the idea.</p>
  
  <p>Is there a way I can check to see if v1 exists in file3.cs? Maybe something like this.</p>
  
  <pre><code>string c = VarExist(MyProgram.file3.v1) ? MyProgram.file3.v1 : "variable not exist";
  </code></pre>
  
  <p>I have tried to use try-catch, but the code won't even compile with that method.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I know the variable doesn't exist, but is there a way for the program itself to check that?</p>
  
  <p>In my main.cs, its looping through those file and applying the same operation. I want it to be able to do something like this:</p>
  
  <pre><code>foreach (file in files) //loop through file1.cs - file3.cs
  {
      if (file.v1 exist)
          //do action1
      if (file.v2 exist)
          //do action2
      if (file.v3 exist)
          //do action3
      //....
  }
  </code></pre>
  
  <p>Right now the program won't even compile, I'll have to create v1 in file3.cs and set it to null. This was the temporary solution because on other parts/function of this program, it's requiring v1 to NOT be in file3.cs (and no I can't change this because it's what the previous develop set up and I don't know the code well enough to change it)</p>
  
  <p>This is a fairly big solution so I'm sorry I can't really post actual code. Thank you all for the time I really appreciates it.</p>