C# C中没有客户端名称的扩展方法#
鉴于以下代码:C# C中没有客户端名称的扩展方法#,c#,asp.net,webforms,C#,Asp.net,Webforms,鉴于以下代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using NetworksApi.TCP.CLIENT
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NetworksApi.TCP.CLIENT;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Form1 client;
public Form1()
{
InitializeComponent();
}
private void textBox2_KeyDown(object sender, KeyEventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
if (textBox3.Text!= "" &&textBox4.Text!="")
{
client = new Form1();
client.ClientName = textBox4.Text;
client.ServerIp = textBox3.Text;
client.Connect();
}
else
{
MessageBox.Show("Fill it completely");
}
}
private void button3_Click(object sender, EventArgs e)
{
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
System.Environment.Exit(System.Environment.ExitCode);
}
}
}
每当我尝试编译时,都会收到以下错误消息:
“WindowsFormsApplication1.Form1”不包含的定义
ClientName并且没有扩展方法“ClientName”接受第一个
类型的参数
你知道如何解决这个问题吗?这是.NET中
表单类的文档:
请注意,ClientName
没有列出任何成员。无法引用它,因为它不存在。Windows窗体类上没有ClientName属性。但是,由于您是从表单继承的,所以可以添加一个。但这也没有道理。是否确实希望类型为Form1
的变量具有ClientName
、ServerIP
的属性以及Connect()
的方法?更可能的情况是,您想要其他预先存在的类,或者创建自己的类
public class ClientService
{
public string ClientName {get; set;}
public string ServerIp {get; set;}
public void Connect()
{
//logic here
}
}
并将您的UI逻辑更改为
if (!String.IsNullOrEmpty(textBox3.Text) && !String.IsNullOrEmpty(textBox4.Text))
{
var client = new ClientService();
client.ClientName = textBox4.Text;
client.ServerIp = textBox3.Text;
client.Connect();
}
else
{
MessageBox.Show("Fill it completely");
}