Click here to Skip to main content
15,910,877 members
Home / Discussions / C#
   

C#

 
Questionthe form stops Pin
som4a20-Sep-07 12:50
som4a20-Sep-07 12:50 
AnswerRe: the form stops Pin
Luc Pattyn20-Sep-07 13:05
sitebuilderLuc Pattyn20-Sep-07 13:05 
QuestionMultiTabbed WebBrowser (unique Instances) Pin
Beef Feeter20-Sep-07 12:29
Beef Feeter20-Sep-07 12:29 
AnswerRe: MultiTabbed WebBrowser (unique Instances) Pin
Scott Dorman20-Sep-07 19:20
professionalScott Dorman20-Sep-07 19:20 
GeneralRe: MultiTabbed WebBrowser (unique Instances) Pin
Beef Feeter20-Sep-07 22:26
Beef Feeter20-Sep-07 22:26 
GeneralRe: MultiTabbed WebBrowser (unique Instances) Pin
Scott Dorman21-Sep-07 4:11
professionalScott Dorman21-Sep-07 4:11 
Questionc# Images into Video file [modified] Pin
BenGriffiths20-Sep-07 11:59
BenGriffiths20-Sep-07 11:59 
AnswerRe: c# Images into Video file Pin
Christian Graus20-Sep-07 14:26
protectorChristian Graus20-Sep-07 14:26 
QuestionCVS with big C# Winforms project Pin
jesarg20-Sep-07 11:44
jesarg20-Sep-07 11:44 
AnswerRe: CVS with big C# Winforms project Pin
Paul Conrad23-Sep-07 6:42
professionalPaul Conrad23-Sep-07 6:42 
QuestionMultiple files through context menu Pin
Johan Martensson20-Sep-07 10:40
Johan Martensson20-Sep-07 10:40 
QuestionDesign pattern framework Pin
Wes Aday20-Sep-07 10:29
professionalWes Aday20-Sep-07 10:29 
AnswerRe: Design pattern framework Pin
Pete O'Hanlon20-Sep-07 10:44
mvePete O'Hanlon20-Sep-07 10:44 
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 

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.