Question

Cubangt on Thu, 24 Jan 2013 21:43:24


We are working with TCP communication and for alot of what we are doing we have different calls to the device that are basically identical with the exception of 2 potential variables.. So i would like to move this into its own method or whatever so i can reuse it anywhere in my application..

I would like to take what is within the TRY section and setup to be reusable/globally available within the application

        public static struct_ReturnVal SA_bool_Fid_Ignite(bool bool_OnOff)
        {
            OpenTcpPort();
            bool bool_ReturnValue = false;
            int int_ReturnValue = 0;
            long long_ReturnValue = 0;
            string string_ReturnValue = string.Empty;
            int int_Error = 0;
            string string_ErrorMsg = string.Empty;
            struct_ReturnVal s_Return;

            try
            {
                NetworkStream stream = client.GetStream();
                BinaryWriter bWriter = new BinaryWriter(stream);
                BinaryReader bReader = new BinaryReader(stream);

                byte[] HexString;
                byte[] SendToBeagle;
                SAPacket packetSA;
                RawSerializer<SAPacket> serializerRaw;
                string CmdString = "IGNITE," + Convert.ToInt32(bool_OnOff);
                System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();

                HexString = encoding.GetBytes(CmdString.PadRight(1024, ' '));

                packetSA.Type = 1;
                packetSA.Size = (UInt32)CmdString.Length;
                packetSA.Offset = 0;
                packetSA.Data = HexString;

                serializerRaw = new RawSerializer<SAPacket>();

                SendToBeagle = new byte[1036];
                SendToBeagle = serializerRaw.RawSerialize(packetSA);

                // Send the binary data out the port
                bWriter.Write(SendToBeagle);
                bWriter.Flush();

                bool_ReturnValue = true;
                bReader.Read(SendToBeagle, 0, 1036);
                packetSA = serializerRaw.RawDeserialize(SendToBeagle);
                string_ReturnValue = new string(encoding.GetChars(packetSA.Data, 0, (int)packetSA.Size));
            }
            catch (FormatException ex)
            {
                // Inform the user if the hex string was not properly formatted
                string_ErrorMsg = "Format Exception: Message: " + ex.Message + " Thrown By: " + ex.TargetSite;
            }
            catch (ArgumentNullException ex)
            {
                string_ErrorMsg = "Argument Null Expection error: " + ex.Message + " Thrown By: " + ex.TargetSite;
            }
            catch (ArgumentException ex)
            {
                string_ErrorMsg = "Argument Expection error: " + ex.Message + " Thrown By: " + ex.TargetSite;
            }
            catch (InvalidOperationException ex)
            {
                string_ErrorMsg = "Invalid Operation Expection error: " + ex.Message + " Thrown By: " + ex.TargetSite;
            }
            catch (IOException ex)
            {
                string_ErrorMsg = "IO Expection error: " + ex.Message + " Thrown By: " + ex.TargetSite;
            }

            // If string_ReturnValue = 1 Ignite was Successful, 0 Ignite was Unsuccessful
            s_Return = SA_struct_InfoError(bool_ReturnValue, int_ReturnValue, long_ReturnValue, string_ReturnValue, int_Error, string_ErrorMsg);
            return (s_Return);



Sponsored



Replies

PaulLinton on Thu, 24 Jan 2013 21:48:19


In visual studio, select the body of the try, right click, Refactor, Extract method...

Cubangt on Thu, 24 Jan 2013 21:53:04


I have done that and use that all the time, but when i do, it breaks that portion of the calls, it has something to do with the network stream portion.

thank you

There has to be a way even, manually, that i can write that logic in a way i can reuse anywhere in the application.

PaulLinton on Thu, 24 Jan 2013 21:54:05


Show the code which is broken.

Cubangt on Thu, 24 Jan 2013 22:03:36


well no error if i leave it as it is, its after i extract to method

it changes it to this:

            try
            {
                TestMethod(bool_OnOff, ref bool_ReturnValue, ref string_ReturnValue);
            }

im trying to recreate the error, but at the moment its not giving me the error or any returned values..

How can i gain access to the "ref string_ReturnValue"???

PaulLinton on Thu, 24 Jan 2013 22:05:12


By using its name

if(string_ReturnValue == "Something of interest")

Cubangt on Thu, 24 Jan 2013 22:29:41


ok finally got the error..

Invalid Operation Expection error: The operation is not allowed on non-connected sockets. Thrown By: System.Net.Sockets.NetworkStream GetStream()

this is happens mostly using the extracted method way..