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

[1060 byte] By [Tdar] at [2007-12-21]
# 1
u never give responsefile a type, is a string? an int? etc
Develop3r at 2007-9-10 > top of Msdn Tech,Visual C#,Visual C# Language...
# 2

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);
}

JamesCurran at 2007-9-10 > top of Msdn Tech,Visual C#,Visual C# Language...