简体   繁体   中英

How do I assign this value to a global variable in c#?

I need to assign a value that's being received from a socket (TCP/IP) to a variable, so I can use it in a label in a form. I'm asking here because I've been searching and trying for hours and can't find anything.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Windows.Forms;

namespace ConsoleApp1
{
class Exemys
{
        static byte[] Buffer { get; set; }
        static Socket sck;
        [STAThread]
        public static void Conectar(/*string[] args*/)
        {
            sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint localEndpoint = new IPEndPoint(IPAddress.Parse("192.168.34.230"), 5202);
            try
            {
                sck.Connect(localEndpoint);
                Console.WriteLine("Exemys connected!\r\n");
            }
            catch
            {
                Console.Write("Unable to connect to Exemys\r\n");
                Conectar(/*args*/);
            }
        while (true)
        {
            Buffer = new byte[sck.SendBufferSize];
            int bytesRead = sck.Receive(Buffer);
            byte[] formatted = new byte[bytesRead];
            for (int i = 0; i < bytesRead; i++)
            {
                formatted[i] = Buffer[i];
            }
            string mensaje = Encoding.ASCII.GetString(formatted);
            Console.Write(mensaje + "\r\n");
            
        }
    }       
}
}

This code is written in a class, and the Form is in other place. The value that I need to assign is mensaje , so I can see it in a Text Box in the Form.

First of all global variables smells like a bad design. Anyway, it seems like you are giving an example of a ConsoleApplication but you have maybe a Windows Form Application ? Obviously you can not run a Form from a Console Application, so just convert everything to a Windows Forms Application to start with.

Anyway, after you have a Form working, you will probably could change your code so that Conectar() , that we in fact we can give a better name like ConnectAndGetMessage() , to actually return the message. Then you call that method from your Form, maybe on the Load handler.

Briefly, you could do something like this:

class Exemys {

    public static string ConnectAndGetMessage() {
        // Here your code! ;)
        return mensaje;
    }
}

class FormWithTextbox {

    private void FormWithLabel_Load(object sender, System.EventArgs e)
    {
        Textbox1.Text = Exemys.ConnectAndGetMessage();
    }
}

Please note that this is not the best solution because you should inject the Exemys as a dependency instead, anyway, that will be for a later improvement.

Another thing to point out about your code, but you will probably get to know quickly is that you are not exiting the while loop, so you will probably end up in an infinite loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM