c#对象实例化的对象如何在另一个事件中调用 40
publicvoidForm1_Load(objectsender,EventArgse){intport=8089;stringhost="192.168.1.116"...
public void Form1_Load(object sender, EventArgs e)
{
int port = 8089;
string host = "192.168.1.116";
IPAddress ip = IPAddress.Parse(host);
IPEndPoint ipe = new IPEndPoint(ip, port);
Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
c.Connect(ipe);
}
private void button1_Click(object sender, EventArgs e)
{
string sendStr = "hello!This is a socket test";
byte[] bs = Encoding.ASCII.GetBytes(sendStr);
c.Send(bs, bs.Length, 0);
}
在一个窗体下这个对象c想被多个事件中使用 但是提示
错误 1 当前上下文中不存在名称“c”
请高手给想个什么办法解决! 展开
{
int port = 8089;
string host = "192.168.1.116";
IPAddress ip = IPAddress.Parse(host);
IPEndPoint ipe = new IPEndPoint(ip, port);
Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
c.Connect(ipe);
}
private void button1_Click(object sender, EventArgs e)
{
string sendStr = "hello!This is a socket test";
byte[] bs = Encoding.ASCII.GetBytes(sendStr);
c.Send(bs, bs.Length, 0);
}
在一个窗体下这个对象c想被多个事件中使用 但是提示
错误 1 当前上下文中不存在名称“c”
请高手给想个什么办法解决! 展开
1个回答
展开全部
Socket c =null;
public void Form1_Load(object sender, EventArgs e)
{
int port = 8089;
string host = "192.168.1.116";
IPAddress ip = IPAddress.Parse(host);
IPEndPoint ipe = new IPEndPoint(ip, port);
c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
c.Connect(ipe);
}
private void button1_Click(object sender, EventArgs e)
{
string sendStr = "hello!This is a socket test";
byte[] bs = Encoding.ASCII.GetBytes(sendStr);
c.Send(bs, bs.Length, 0);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询