我在两个独立的项目中有两个几乎完全相同的代码块;创建第二个项目是为了使用与第一个相同的ConnectClass。
但是,在新项目(下面显示的第二个代码块)中,编译器提供以下关于错误处理程序的错误消息static void client_ErrorEvents(object sender,a.b.c.ErrorEventArgs e):
以下方法或属性之间的调用不明确:' ProjectName.ConnectClass.client_ErrorEvents(object,a.b.c.ErrorEventArgs)'和' ProjectName.ConnectClass.client_ErrorEvents(object,a.b.c.ErrorEventArgs)'
这是第一个代码块(相关部分)
public partial class ConnectClass
{
public static a.b.c.Client client = new a.b.c.Client();
public static string DriveLetter;
public static string CurrentDate;
//_____________________
public static void StoreVars(Form1 frm)
{
DriveLetter = frm.textBox4.Text;
CurrentDate = frm.textBox5.Text;
}
public static string Connect(Form1 frm, int Call)
{
string host = "127.0.0.1";
int port = 7496;
int clientId = 0;
string Result = "Connected";
try
{
client.Connect(host, port, clientId);
}
catch (SocketException e)
{
Result = e.ToString();
}
//if (Call == 0)
client.Error += new EventHandler<a.b.c.ErrorEventArgs>(client_ErrorEvents);
return Result;
}
static void client_ErrorEvents(object sender, a.b.c.ErrorEventArgs e)
{
int ErrorCode;
string path = DriveLetter + "/ThisPath/File.txt";
FileStream ThisFile = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
StreamWriter sw = new StreamWriter(ThisFile);
DateTime dtNow = DateTime.Now;
ErrorCode = (int)e.ErrorCode;
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.Write(dtNow);
sw.Write(" ");
sw.Write(e.ErrorCode);
sw.Write(" ");
sw.Write(e.ErrorMsg);
sw.Write(sw.NewLine);
sw.Close();
ThisFile.Close();
}
这是第二个代码块(相关部分)
public partial class ConnectClass
{
public static a.b.c.Client client = new a.b.c.Client();
public static string DriveLetter;
public static string CurrentDate;
//_____________________
public static void StoreVars(Form1 frm)
{
DriveLetter = frm.TBx_Drive.Text;
CurrentDate = frm.TBx_Date.Text;
}
public static string Connect(Form1 frm)
{
string host = "127.0.0.1";
int port = 7496;
int clientId = 10;
string Result = "Connected";
try
{
client.Connect(host, port, clientId);
}
catch (SocketException e)
{
Result = e.ToString();
}
client.Error += new EventHandler<a.b.c.ErrorEventArgs>(client_ErrorEvents);
return Result;
}
//这是编制者发出错误信号的地方:
static void client_ErrorEvents(object sender, a.b.c.ErrorEventArgs e)
{
string path = DriveLetter + "/ThisPath/File.txt";
FileStream ThisFile = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
StreamWriter sw = new StreamWriter(ThisFile);
DateTime dtNow = DateTime.Now;
ErrorCode = (int)e.ErrorCode;
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.Write(dtNow);
sw.Write(" ");
sw.Write(e.ErrorCode);
sw.Write(" ");
sw.Write(e.ErrorMsg);
sw.Write(sw.NewLine);
sw.Close();
ThisFile.Close();
}
有什么想法吗?
非常感谢。
答案 0 :(得分:3)
您可以在2个文件(partial
)之间进行单个类拆分,并且两个文件都实现相同的功能。
要么使它们不是部分的(你可能在类上有名称冲突),要么在同一个类中没有重复的方法。
在MSDN上的Partial Classes and Methods in C#文章中查看有关使用partial
的详细信息。