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.
So basically, my main.cs
is calling several other files (file1.cs
, file2.cs
, file3.cs
, etc). Each of this files are very similar but some are missing some variables.
For example, var v1
exists in file1.cs
and file2.cs
, but not file3.cs
In my main.cs
, it's something like this.
string a = MyProgram.file1.v1;
string b = MyProgram.file2.v1;
string c = MyProgram.file3.v1; //will break here
I know the above code probably won't work at all, but that was the idea.
Is there a way I can check to see if v1 exists in file3.cs? Maybe something like this.
string c = VarExist(MyProgram.file3.v1) ? MyProgram.file3.v1 : "variable not exist";
I have tried to use try-catch, but the code won't even compile with that method.
EDIT
I know the variable doesn't exist, but is there a way for the program itself to check that?
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:
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
//....
}
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)
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.