does not exist in the current context

Hello, how do i correct this erorr "The name 'responsefile' does not exist in the current context"
on this code

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Web;

namespace MYQ_CLASS
{
class Class2
{
private string dirpath = Directory.GetCurrentDirectory().ToString();

private bool tester()
{
bool dbuger1 = false;
String strRes = "test";

if (dbuger1 == true)
{
responsefile = System.IO.File.CreateText(dirpath + "\\" + "ResultMessage.xml");
}

//do something
if (dbuger1 == true)
{
responsefile.Write(strRes);
}
}
}

Tdar



Answer this question

does not exist in the current context

  • AndreiNK

    I'm going to guess that you made a typo in when you posted you code to the forum. My guess is that you actually have:

    if (dbuger1 == true)
    {
    StreamWriter responsefile = System.IO.File.CreateText(dirpath + "\\" + "ResultMessage.xml");
    }

    Which means that the scope ("life") of responsefile is just between the open & close braces of the if(). By the time you get down to the Write a bit later, the compile knows nothing of it. What you need to do is:

    StreamWriter responsefile;
    if (dbuger1 == true)
    {
    responsefile = System.IO.File.CreateText(dirpath + "\\" + "ResultMessage.xml");
    }

    //do something
    if (dbuger1 == true)
    {
    responsefile.Write(strRes);
    }



  • ScottTTx

    u never give responsefile a type, is a string an int etc

  • does not exist in the current context