1

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.

Ry-
  • 218,210
  • 55
  • 464
  • 476
sora0419
  • 2,308
  • 9
  • 39
  • 58
  • its check compiler when you try build – Grundy Dec 13 '13 at 17:52
  • 1
    No, that is a compile-time error. You are not calling "files". Store your data in a collection of some type and index by name, then check for existance data in the collection. Exposing global variables amongst classes is a bad design. Maybe you need to re-evaluate that first. – OldProgrammer Dec 13 '13 at 17:52
  • If this is a reflection question then this is a possible duplicate of [Finding the Variable Name passed to a Function in C#](http://stackoverflow.com/questions/72121/finding-the-variable-name-passed-to-a-function-in-c-sharp) – DotNetRussell Dec 13 '13 at 17:53
  • 6
    When writting the code, you should already know that file3 doesn't contain v1. You could just write down: string c = "variable not exist"; – the_lotus Dec 13 '13 at 17:53
  • 2
    @the_lotus this is why Reflection exists. Maybe you are reflecting on a dll that you don't have in your position yet. A generic class or a method that handles generic classes – DotNetRussell Dec 13 '13 at 17:54
  • I bet you want reflection. Could you post a more complete code (an example of the contents of file1.cs, for example)? I think that what you have there is an static class with a public field. Anyway, something very similar can be done with dynamic: http://stackoverflow.com/questions/2839598/how-to-detect-if-a-property-exists-on-a-dynamic-object-in-c – Theraot Dec 13 '13 at 17:56
  • @AMR I agree, but it doesn't seem to be the case in this example. I might be wrong. – the_lotus Dec 13 '13 at 17:56
  • @the_lotus I think that is exactly what he is poorly trying to explain lol. He wants to see if a variable exists in a cs file that hasn't been defined. This is a classic case of reflection. – DotNetRussell Dec 13 '13 at 17:58
  • 2
    What it is you are trying to achieve utlimately ? Perhaps we could propose another way than checking if variables "exist". – Francis Ducharme Dec 13 '13 at 18:05
  • @the_lotus Hi all, thank you so much for the time, I have updated my question and explain a bit more, hopefully this can give you guys a better understanding of what I'm trying to do. – sora0419 Dec 13 '13 at 18:06
  • So... what type are those "files"? What I would really like to see is an example of one of those files. I see you said, it doesn't compile... I'll bet for this solution: http://stackoverflow.com/a/11540889/402022 – Theraot Dec 13 '13 at 18:07
  • 1
    @Theraot the files are specific "rule" that applies to different users. It contains a class called userARule, which is a subclass of userRule – sora0419 Dec 13 '13 at 18:10
  • @sora0419 instead of checking if a property exists. The base class could have a DoAction() method that all subclass override with their specific action to execute. That way you'll only have file.DoAction(); – the_lotus Dec 13 '13 at 19:31

2 Answers2

3

If you really need to do this this way, you could use reflection. Be aware, reflection is slow though. You will be able to then use a try/catch each time you attempt to get a property's value.

What you call "variables" should be properties or methods defined (or not) in each of your class modules (file1.cs, file2.cs, etc.)

Community
  • 1
  • 1
Francis Ducharme
  • 4,848
  • 6
  • 43
  • 81
1

Per the comments, you can add your items in a list like this:

var files = new List<userRule>()
{
    MyProgram.file1,
    MyProgram.file2,
    MyProgram.file3
};

This can be done because the type of those "files" inherits from userRule.

Now you can iterate with:

foreach (file in files)
{
    //something
}

For the check, do this:

foreach (file in files)
{
    var property = file.GetType().GetProperty("v1");
    if (property == null)
    {
        // it doesn't have the property
    }
    else
    {
        // it has the property
        // read it:
        var v1 = property.GetValue(file, null);
        // write it:
        property.SetValue(file, v1, null);
    }
}
Theraot
  • 31,890
  • 5
  • 57
  • 86
  • Thanks! This is very helpful. So I ran into a couple issue. 1) No overload for method 'GetValue' takes 1 arguments. 2) Is it possible to set v1 in the original file? something like property.GetValue(file) = 'someString'; this is the variable that store a nickname – sora0419 Dec 13 '13 at 18:43
  • oh and I just realize it seems like reflection is only working for .Net 4.5. Is there a method that will work for 4.0 – sora0419 Dec 13 '13 at 18:47
  • 1
    @sora0419 updated. In .NET 4.5 some convenient methods where added, the overload of GetValue with a single argument is one of those. I also show how to write the property. – Theraot Dec 13 '13 at 18:59