C#中没有客户端名称的扩展方法
本文关键字:扩展 方法 客户端 | 更新日期: 2023-09-27 18:27:36
给定以下代码:
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"类型的参数。
你知道怎么解决这个问题吗?
Windows窗体类上没有ClientName属性。但是,由于您是从Form继承的,因此可以添加一个。但这也没有道理。是否确实要类型为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");
}
这是.NET中Form
类的文档:https://msdn.microsoft.com/en-us/library/system.windows.forms.form(v=vs.110).aspx
请注意,没有列出ClientName
的成员。你不能引用它,因为它不存在。