Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. easy newbie question - SOLVED [modified]

easy newbie question - SOLVED [modified]

Scheduled Pinned Locked Moved C#
helpquestion
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    shwaguy
    wrote on last edited by
    #1

    I get the following error when running my code: "A local variable named 'textOut' cannot be declared in this scope because it would give a different meaning to 'textOut', which is already used in a 'child' scope to denote something else" 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(); } } } If I put the "textOut.Write("test");" code in my try statement all is fine. I think this sucks; I found that I cannot reuse textOut anywhere else in my code, this just does not seem right! Please help and enlighten me. Many Thanks, Stuntman -- modified at 18:06 Thursday 20th September, 2007

    L S S 3 Replies Last reply
    0
    • S shwaguy

      I get the following error when running my code: "A local variable named 'textOut' cannot be declared in this scope because it would give a different meaning to 'textOut', which is already used in a 'child' scope to denote something else" 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(); } } } If I put the "textOut.Write("test");" code in my try statement all is fine. I think this sucks; I found that I cannot reuse textOut anywhere else in my code, this just does not seem right! Please help and enlighten me. Many Thanks, Stuntman -- modified at 18:06 Thursday 20th September, 2007

      L Offline
      L Offline
      lost in transition
      wrote on last edited by
      #2

      You are declaring textOut twice in the same method. In your 'catch' block don't use StreamWriter in front of textOut.


      God Bless, Jason
      I am not perfect but I try to be better than those before me. So those who come after me will be better than I am.

      S 1 Reply Last reply
      0
      • S shwaguy

        I get the following error when running my code: "A local variable named 'textOut' cannot be declared in this scope because it would give a different meaning to 'textOut', which is already used in a 'child' scope to denote something else" 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(); } } } If I put the "textOut.Write("test");" code in my try statement all is fine. I think this sucks; I found that I cannot reuse textOut anywhere else in my code, this just does not seem right! Please help and enlighten me. Many Thanks, Stuntman -- modified at 18:06 Thursday 20th September, 2007

        S Offline
        S Offline
        Scott Dorman
        wrote on last edited by
        #3

        First, please wrap large code blocks in <pre> tags, not <code> in order to preserve the formatting.

        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:

        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]

        P 1 Reply Last reply
        0
        • L lost in transition

          You are declaring textOut twice in the same method. In your 'catch' block don't use StreamWriter in front of textOut.


          God Bless, Jason
          I am not perfect but I try to be better than those before me. So those who come after me will be better than I am.

          S Offline
          S Offline
          Scott Dorman
          wrote on last edited by
          #4

          It's not in the catch block. Take a look at my response to see the code in a formatted block. Even if it was in the catch block, it still wouldn't work because then textOut would be undefined in the catch block. In either case, the variable should be declared outside of the try.

          Scott.


          —In just two days, tomorrow will be yesterday. [Forum Guidelines] [Articles] [Blog]

          L 1 Reply Last reply
          0
          • S Scott Dorman

            It's not in the catch block. Take a look at my response to see the code in a formatted block. Even if it was in the catch block, it still wouldn't work because then textOut would be undefined in the catch block. In either case, the variable should be declared outside of the try.

            Scott.


            —In just two days, tomorrow will be yesterday. [Forum Guidelines] [Articles] [Blog]

            L Offline
            L Offline
            lost in transition
            wrote on last edited by
            #5

            Scott Dorman wrote:

            It's not in the catch block.

            I guess my eyes got confused.

            Scott Dorman wrote:

            it still wouldn't work because then textOut would be undefined in the catch block

            You right I wasn't paying much attention.


            God Bless, Jason
            I am not perfect but I try to be better than those before me. So those who come after me will be better than I am.

            S 1 Reply Last reply
            0
            • L lost in transition

              Scott Dorman wrote:

              It's not in the catch block.

              I guess my eyes got confused.

              Scott Dorman wrote:

              it still wouldn't work because then textOut would be undefined in the catch block

              You right I wasn't paying much attention.


              God Bless, Jason
              I am not perfect but I try to be better than those before me. So those who come after me will be better than I am.

              S Offline
              S Offline
              Scott Dorman
              wrote on last edited by
              #6

              No worries. Whenever I see a post like that, at a minimum I will respond saying they should wrap the code in <pre> tags. Sometimes, as I did with this one, I'll reformat it for them (and anyone else that responds) and provide an answer.

              Scott.


              —In just two days, tomorrow will be yesterday. [Forum Guidelines] [Articles] [Blog]

              1 Reply Last reply
              0
              • S Scott Dorman

                First, please wrap large code blocks in <pre> tags, not <code> in order to preserve the formatting.

                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:

                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]

                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #7

                I have to point out the obvious here. This code is really bad practice. In the try block, he initializes the textOut variable and if he gets an IOException he shows the message. Then, he initialises the try textOut variable in exactly the same way - if it didn't work once, is it really going to work again. Finally, the textOut.Close() should really be in a finally block.

                Deja View - the feeling that you've seen this post before.

                S 1 Reply Last reply
                0
                • P Pete OHanlon

                  I have to point out the obvious here. This code is really bad practice. In the try block, he initializes the textOut variable and if he gets an IOException he shows the message. Then, he initialises the try textOut variable in exactly the same way - if it didn't work once, is it really going to work again. Finally, the textOut.Close() should really be in a finally block.

                  Deja View - the feeling that you've seen this post before.

                  S Offline
                  S Offline
                  Scott Dorman
                  wrote on last edited by
                  #8

                  Absolutely! I missed those issues completely. The second calls to textOut probably either shouldn't be there or should be moved into the try block (with the redudant initialization removed).

                  Scott.


                  —In just two days, tomorrow will be yesterday. [Forum Guidelines] [Articles] [Blog]

                  1 Reply Last reply
                  0
                  • S shwaguy

                    I get the following error when running my code: "A local variable named 'textOut' cannot be declared in this scope because it would give a different meaning to 'textOut', which is already used in a 'child' scope to denote something else" 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(); } } } If I put the "textOut.Write("test");" code in my try statement all is fine. I think this sucks; I found that I cannot reuse textOut anywhere else in my code, this just does not seem right! Please help and enlighten me. Many Thanks, Stuntman -- modified at 18:06 Thursday 20th September, 2007

                    S Offline
                    S Offline
                    shwaguy
                    wrote on last edited by
                    #9

                    Thanks guys, I sincerely appreciate the criticism and analysis. Problem solved. thanks again,:) stuntman

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups