Click here to Skip to main content
15,898,371 members
Home / Discussions / C#
   

C#

 
GeneralRe: Design pattern framework Pin
Wes Aday20-Sep-07 11:04
professionalWes Aday20-Sep-07 11:04 
GeneralRe: Design pattern framework Pin
Pete O'Hanlon20-Sep-07 11:18
mvePete O'Hanlon20-Sep-07 11:18 
Questioneasy newbie question - SOLVED [modified] Pin
shwaguy20-Sep-07 10:27
shwaguy20-Sep-07 10:27 
AnswerRe: easy newbie question Pin
lost in transition 20-Sep-07 10:31
lost in transition 20-Sep-07 10:31 
GeneralRe: easy newbie question Pin
Scott Dorman20-Sep-07 10:38
professionalScott Dorman20-Sep-07 10:38 
GeneralRe: easy newbie question Pin
lost in transition 20-Sep-07 10:47
lost in transition 20-Sep-07 10:47 
GeneralRe: easy newbie question Pin
Scott Dorman20-Sep-07 10:51
professionalScott Dorman20-Sep-07 10:51 
AnswerRe: easy newbie question Pin
Scott Dorman20-Sep-07 10:36
professionalScott Dorman20-Sep-07 10:36 
First, please wrap large code blocks in <pre> tags, not <code> in order to preserve the formatting.

C#
namespace file_i_o
{
   public partial class Form1 : Form
   {
      public Form1()
      {
         InitializeComponent();
      }
 
      private void button1_Click(object sender, EventArgs e)
      {
         string path = @"c:\testc.txt";
         try
         {
            StreamWriter textOut = new StreamWriter(new FileStream(path, FileMode.Create, FileAccess.Write));
         }
         catch (IOException ioe)
         {
            MessageBox.Show(ioe.Message);
         }
 
         StreamWriter textOut = new StreamWriter(new FileStream(path, FileMode.Create, FileAccess.Write));
         textOut.Write("test"); //*****THIS IS WHERE MY ERROR IS*****
         textOut.Close();
      }
   }
}
The issue you are running into is due to the scoping rules of the language. If you want to do this, you need to declare the variable outside of the try block (StreamWriter textOut; or StreamWriter textOut = null;) and then simply "new" the variable in both places. Your code would end up looking like this:
C#
namespace file_i_o
{
   public partial class Form1 : Form
   {
      public Form1()
      {
         InitializeComponent();
      }
 
      private void button1_Click(object sender, EventArgs e)
      {
         string path = @"c:\testc.txt";
         StreamWriter textOut = null;
         try
         {
            textOut = new StreamWriter(new FileStream(path, FileMode.Create, FileAccess.Write));
         }
         catch (IOException ioe)
         {
            MessageBox.Show(ioe.Message);
         }
 
         textOut = new StreamWriter(new FileStream(path, FileMode.Create, FileAccess.Write));
         textOut.Write("test"); //*****THIS IS WHERE MY ERROR IS*****
         textOut.Close();
      }
   }
}



Scott.

—In just two days, tomorrow will be yesterday.

[Forum Guidelines] [Articles] [Blog]

GeneralRe: easy newbie question Pin
Pete O'Hanlon20-Sep-07 11:26
mvePete O'Hanlon20-Sep-07 11:26 
GeneralRe: easy newbie question Pin
Scott Dorman20-Sep-07 11:32
professionalScott Dorman20-Sep-07 11:32 
AnswerRe: easy newbie question Pin
shwaguy20-Sep-07 12:06
shwaguy20-Sep-07 12:06 
QuestionGridView Row updating Pin
ss.mmm20-Sep-07 9:57
ss.mmm20-Sep-07 9:57 
QuestionPerformanceCounter problems Pin
JMichael246820-Sep-07 8:53
JMichael246820-Sep-07 8:53 
AnswerRe: PerformanceCounter problems Pin
JMichael246821-Sep-07 4:36
JMichael246821-Sep-07 4:36 
QuestionSpeed up Treeview [Solved] Pin
DaveyM6920-Sep-07 8:52
professionalDaveyM6920-Sep-07 8:52 
AnswerRe: Speed up Treeview Pin
Dave Kreskowiak20-Sep-07 9:12
mveDave Kreskowiak20-Sep-07 9:12 
GeneralRe: Speed up Treeview Pin
DaveyM6920-Sep-07 10:55
professionalDaveyM6920-Sep-07 10:55 
GeneralRe: Speed up Treeview Pin
Scott Dorman20-Sep-07 10:58
professionalScott Dorman20-Sep-07 10:58 
GeneralRe: Speed up Treeview Pin
DaveyM6920-Sep-07 11:13
professionalDaveyM6920-Sep-07 11:13 
AnswerRe: Speed up Treeview [modified] Pin
Scott Dorman20-Sep-07 10:56
professionalScott Dorman20-Sep-07 10:56 
GeneralRe: Speed up Treeview Pin
Pete O'Hanlon20-Sep-07 11:19
mvePete O'Hanlon20-Sep-07 11:19 
GeneralRe: Speed up Treeview Pin
Scott Dorman20-Sep-07 11:37
professionalScott Dorman20-Sep-07 11:37 
GeneralRe: Speed up Treeview Pin
DaveyM6920-Sep-07 11:43
professionalDaveyM6920-Sep-07 11:43 
AnswerRe: Speed up Treeview Pin
DaveyM6920-Sep-07 11:31
professionalDaveyM6920-Sep-07 11:31 
GeneralRe: Speed up Treeview Pin
Scott Dorman20-Sep-07 18:26
professionalScott Dorman20-Sep-07 18:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.