TLamb
TLamb

Reputation: 147

What causes "Variable does not exist in current context" message

I'm trying to write a very simple C# program in VS2015. I keep getting a message "The name (variable) does not exist in the current context." This is true for both my bool variable and string variable.

    namespace PTouch
{
    public class PTouch
    {
        bool lb_rc;
        string strLabel;
        lb_rc = false;
        strLabel = "C:\BenchTop10\Standard 1in.lbx";
        bpac.Document doc = new Document();
        lb_rc = doc.Open("C:\BenchTop10\Standard 1in.lbx");
        if lb_rc != false
        {
            doc.StartPrint("", 0);
            doc.PrintOut(1, 0);
            doc.EndPrint();
            doc.Close();
         }
         else
         {
                MessageBox.Show("Open Error: " + doc.ErrorCode);
         }
    }
}

There are probably several problems with this code, but the first one is error about the variables not existing in the current context. Any assistance is greatly appreciated.


Upvotes: 0

Views: 501

Answers (1)

TLamb
TLamb

Reputation: 147

I just discovered the problem... All the code has to be inside of a method, not just the class. I added a method to the class, public void PrintLabel() { bool lb_rc; ... }

All errors cleared up, except MessageBox... Thank you, Tracy

Upvotes: 1

Related Questions