0

I can't find an answer to my problem. In dotNet/C#, is it possible to check if a variable was declared to some type and if not, declare it? Thanks

[Edit] In this case, C# is used as a preexecute language in Open Text CMS. C# code can be used in any module. Using a non-declared variable throws hard to debug errors, as does double-declaring a variable. That's why I'd like to check.

[Edit2] Yes it is most probably compiled somewhere, but the errors are thrown (or rather not thrown) on runtime

[Edit3] Further explanation:
In Open Text, every page can hold several modules, several instances of a module and the same instance of a module several times. In each module, you can use C# as a "pre-execute" language. This is mostly really easy scripting to maneuver around the failings of OpenText. You introduce small variables, set them to true or false, and three lines later write a condition based on the variable. We could (and do) declare a bunch of variables in an initialization block of the page, but since there are so many, it would help to be able to check if a variable was declared and if not, declare it.
I like the idea of changing this to a key/value dictionary but this is a really large site with loads of pages/modules and instances and I'm looking for a working solution without changing the whole thing.
The actual code is really simple most oft he time:

var hasHeadline = false; // this will throw an error if hasHeadline was declared before
hasHeadline = true; // if some CMS condition is met. this will throw an error if hasHeadline wasn't declared
if(hasHeadline) { ** CMS code ** }

As I said, this will show up in multiple instances over which I don't have full control. The resulting "error" will be that the whole code block is stripped from the page.

thomas
  • 2,297
  • 2
  • 22
  • 23
  • The compiler handles that for you. You cant reference an undeclared variable. – Palle Due Apr 21 '20 at 09:09
  • https://stackoverflow.com/questions/3561202/check-if-instance-is-of-a-type – raven Apr 21 '20 at 09:10
  • 1
    I'm having trouble with your claim that in your situation that "C# isn't compiled". That simply isn't possible. C# cannot be "interpreted" or otherwise executed line-by-line because the C# language was expressly designed around being compiled into CIL. I think you misunderstand how OpenTextCMS uses C#. Can you provide a link to their documentation or other source that claims that C# "isn't compiled" in OpenTextCMS? – Dai Apr 21 '20 at 09:23
  • 1
    Apart from all what was already said on C# being compiled, maybe you have a X/Y problem. Maybe what you need is rather a dictionary of key-value pairs. In this case, it's quite easy to create-if-not-exist the entry in the dictionary. Please add some details on what you want to achieve. – Pac0 Apr 21 '20 at 09:25
  • @Dai I don't really care if and where it is compiled. My problem is that both non- and double declaration won't work and I can't debug it, since I don't have access to the right logs. I'm looking for a way to prevent this from happening. – thomas Apr 21 '20 at 09:28
  • @pac0 Thanks, I like the idea. – thomas Apr 21 '20 at 09:31
  • @thomas Please provide a copy or link to OpenTextCMS' documentation for how they use C# with their system so we can try to understand the situation better and try to help you. I had a quick google around myself but I could only find marketing material (and I find it galling that a product called "OpenText" is not actually "open" (as in, open-source)). – Dai Apr 21 '20 at 09:32
  • Hi @Dai, you are right. OpenText is far from open. I added some info on our use case, I hope this helps. As a JS/PHP person it's hard to understand why I can't just check if a variable was declared or not ... – thomas Apr 21 '20 at 09:50
  • @thomas Please post your **actual C# code** and the **exact error message** you get when you try to use an undeclared variable (you can redact sensitive information, of course). – Dai Apr 21 '20 at 09:52
  • I agree with the last comment from Dai. A [mcve] would really be helpful to help potential answerers. Currently, we can just make blind recommendations and assumptions, and I feel this will bounce between you and answers many times – Pac0 Apr 21 '20 at 10:14
  • Sorry guys. As I said, as a JS/PHP person this is a weird problem to have. I posted a minimal example but this is really all there is to it. – thomas Apr 21 '20 at 10:19
  • Would this work? : _assuming_ your code is very simple and you don't _validly_ use same indentifiers in different portions of the code, I think you might be better off scripting some "sanity check". Use something like a bash command line tool `grep` to list all the `var .....` in all your pieces of C# code. Count lines. Remove duplicate lines. Count again. If count is different, you have duplicate lines, thus you have the same line `var xxx` twice. You can refine this logic to show in which files you have duplicates. – Pac0 Apr 21 '20 at 10:25

2 Answers2

1

Declare a single variable that is dynamic, e.g. an ExpandoObject.

dynamic Globals = new ExpandoObject();

Use this variable to store all of your global state.

Globals.hasHeadline = false;  //No declaration needed, so 
Globals.hasHeadline = true;   //no chance of a duplicate declaration
John Wu
  • 50,556
  • 8
  • 44
  • 80
0

There's no need to. C# is a statically typed programming language ("type" refers to more than just class, struct, and interface: "static typing" means the "types" (shapes) of data, objects and values in your program are known statically - i.e. at compile-time). If something isn't declared in scope then your code simply won't compile.

This also applies to locals (local variables, method parameters, etc).

This won't compile:

class Foo
{
    void Foo( String x )
    {
        if( z > 0 ) { // `z` isn't declared as a field, parameter or local.
            // ...
        }
    }
}


Similarly, this won't compile:

class Foo
{
    public string x;
}

class Bar
{
    void Baz( Foo foo )
    {
        if( foo.z > 0 ) { // `z` is not declared in `Foo`

        }
    }
}

That said, there are some things you do need to check-before-using in C#, such as:

  • Nullable references or nullable values.
  • Entries in a Dictionary or other keyed collection.
  • Type-checking when you want a known subclass or interface (As C# still does not natively support algebraic types, grrrr)

...but none of those involve checking for declarations.

Dai
  • 141,631
  • 28
  • 261
  • 374
  • This is a special case since we use C# as a pre-execute language in Open Text CMS. It is not compiled. – thomas Apr 21 '20 at 09:16
  • 1
    @thomas OpenText still uses `csc` to compile C# to CIL (otherwise it wouldn't be C#). If you feel my answer is inaccurate or doesn't apply to your case please update your question-posting with an example of where you need to check for a value's existence or not. – Dai Apr 21 '20 at 09:19