C# Variable Scoping

309 Views Asked by At

I'm new to C#, having written a little in a CMS, but my background is mostly JavaScript related. That said, I am working in the "Scripting" client in OpenText Capture Center. When executing the code below I get the error "The Name 'srfOnly' does not exist in the current context"

If I move the variable declarations to within the function, I get the same error, If I move them to them to global I get the same error but on a different line number.

How can I access the variables srfOnly and otherDocs throughout the code?

    //Parameters:
    //DataPool data
    //ITrace trace

    // Checking if condition is fulfilled.
    if (checkDocuments(data))
    {
       // Getting batch field named 'cc_SkipValidation'.
       // Setting new value.

       DOKuStar.Data.Xml.Bool skipValidationField = data.RootNode.Fields["cc_SkipValidation"] as DOKuStar.Data.Xml.Bool;
       bool srfOnly = false;
       bool otherDocs = false;

        if(otherDocs == true)
        {
             skipValidationField.SetValue(false);
        }
        if(srfOnly == true && otherDocs == false)
        {
             skipValidationField.SetValue(true);
          skipValidationField.State = DataState.Ok;
        }

    }
    // !!! Closing bracket is neccessary !!!
    }
    // ------------------ Functions
    public bool checkDocuments(DataPool dataPool)
    {
        foreach (Document doc in dataPool.RootNode.Documents)
        {
             if (doc.Name == "ServiceRequestForm")
             {
               srfOnly = true;
             }
             else if (doc.Name != "ServiceRequestForm")
             {
                 otherDocs = true;
             }
        }


    trace.WriteInfo("Trace info for Validation of srfOnly = " + srfOnly);
    trace.WriteInfo("Trace info for Validation of otherDocs = " + otherDocs);

    // !!! No closing bracket needed !!!
2

There are 2 best solutions below

0
On

A variable is only accessible within the current block (and the blocks within that block). If you want to access the srfOnly variable within the checkDocuments method you can pass it as a parameter:

public bool checkDocuments(DataPool dataPool, bool srfOnly)

If you want the variable to be accessible from anywhere in the class, you can declare it as a a property of the class or an instance variable as following:

private bool _srfOnly;

2
On

Variables are limited in scope by where they exist in your code. If you declare a variable within an if{} block, the variable only exists inside that block. If you declare a variable inside of a class but not within a class method, the variable is accessible to every method in the class. If you want a variable to be accessible to every class with in a project, you would normally declare it in a public static class.

For example...

public static class GlobalClass
{
    public static string myGlobal="";    
}  

class myClass
{
    string myClassVariable = "";
    private void method()
    {
        //myGlobal is accessible using this
        GlobalClass.myGlobal ="some value";
        //myClassVariable is accessible here
        myClassVariable = "somevalue";
        if(condition)
        {
            //myClassVariable is also accessible here
            myClassVariable = "somevalue";
            string ifBlockVariable = "";
        }
        //ifBlockVariable is not accessible here
    }
}