Visual Studio 2010 C#“已经定义了一个具有相同参数类型错误的成员。”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13237146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Visual Studio 2010 C# "already defined a member with same parameter types error."
提问by William Bergendahl
im having a problem in visual studio it keeps saying i have defined a member with same parameter types. Im new to C# programming and i dont really know what to do. These are the errors that are occurring:
我在 Visual Studio 中遇到问题,它一直说我定义了一个具有相同参数类型的成员。我是 C# 编程的新手,我真的不知道该怎么做。这些是正在发生的错误:
Error 1 Type 'Secret.AddPage' already defines a member called 'AddPage' with the same parameter types
Error 2 Type 'Secret.AddPage' already defines a member called 'PhoneApplicationPage_Loaded' with the same parameter types
错误 1 类型 'Secret.AddPage' 已经定义了一个名为 'AddPage' 的成员,具有相同的参数类型
错误 2 类型 'Secret.AddPage' 已经定义了一个名为 'PhoneApplicationPage_Loaded' 的成员,具有相同的参数类型
Here is the code i have written so far any help is greatly appreciated.
这是我到目前为止编写的代码,非常感谢任何帮助。
enter code here
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Device.Location;
namespace secret
{
public partial class AddPage : PhoneApplicationPage
{
private string location = "";
public AddPage()
{
InitializeComponent();
GeoCoordinateWatcher myWatcher = new GeoCoordinateWatcher();
var myPosition = myWatcher.Position;
// Eftersom koden k?rs i emulatorn kan den inte f? tillg?ng till riktiga GPS-v?rden
// D?rf?r h?rdkodas koordinaterna till slottet i Gamla stan s? att MSR MAPS Web Services
//kan testas.
double latitude = 40.717;
double longitude = -74;
if (!myPosition.Location.IsUnknown)
{
latitude = myPosition.Location.Latitude;
longitude = myPosition.Location.Longitude;
}
myTerraService.TerraServiceSoapClient client = new myTerraService.TerraServiceSoapClient();
client.ConvertLonLatPtToNearestPlaceCompleted += new EventHandler<myTerraService.ConvertLonLatPtToNearestPlaceCompletedEventArgs>(client_ConvertLonLatPtToNearestPlaceCompleted);
client.ConvertLonLatPtToNearestPlaceAsync(new myTerraService.LonLatPt { Lat = latitude, Lon = longitude });
}
void client_ConvertLonLatPtToNearestPlaceCompleted(object sender, myTerraService.ConvertLonLatPtToNearestPlaceCompletedEventArgs e)
{
location = e.Result;
//throw new NotImplementedException();
}
private void AppBar_Cancel_Click(object sender, EventArgs e)
{
navigateBack();
}
private void AppBar_Save_Click(object sender, EventArgs e)
{ // spara en ny anteckning
if (location.Trim().Length == 0)
{
location = "Ok?nd";
}
navigateBack();
}
private void navigateBack()
{
NavigationService.Navigate(new Uri("/secret;component/NotesMainPage.xaml", UriKind.Relative));
}
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
editTextBox.Focus();
}
}
}
采纳答案by Habib
You are creating a partial class, so you probably have these members defined in another source file for your partial class.
您正在创建分部类,因此您可能在分部类的另一个源文件中定义了这些成员。
You may look at the solution explorer, find that source file and either remove it from there or you may remove these members from your current partial class.
您可以查看解决方案资源管理器,找到该源文件并从那里删除它,或者您可以从当前的部分类中删除这些成员。
You may see: Partial Classes and Methods (C# Programming Guide)
您可能会看到:部分类和方法(C# 编程指南)
To search for the other source file containing the partial class, right click on the Class Name AddPageand select Go to Definition. You will see multiple results in Find Symbol result window in visual studio.
要搜索包含分部类的其他源文件,请右键单击类名称AddPage并选择Go to Definition。您将在 Visual Studio 的“查找符号”结果窗口中看到多个结果。
回答by alan
Check for another partial class in which you've already defined the AddPage()constructor or PhoneApplicationPage_Loaded()methods. You can achieve this by Ctrl+Fand searching the solution for the method signatures:
检查已在其中定义AddPage()构造函数或PhoneApplicationPage_Loaded()方法的另一个分部类。您可以通过Ctrl+F并搜索方法签名的解决方案来实现此目的:
public AddPage()
and
和
PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
回答by jdpilgrim
I had something very similar recently, and it turned out that when importing existing code files I had imported the objdirectory itself!

我最近有一些非常相似的事情,结果发现在导入现有代码文件时我已经导入了obj目录本身!

This directory contained, for example, the auto-generated (and automatically imported) MainWindow.g.i.csfile. So I was effectively including the same partial class definition twice, hence the "already defined" errors.
例如,该目录包含自动生成(和自动导入)的MainWindow.g.i.cs文件。所以我有效地包含了两次相同的部分类定义,因此出现了“已经定义”的错误。
How this help someone else!
这如何帮助别人!
回答by Duan Walker
i had a project where i opened the main program.cs in notepad++, made some edits and did a "save as" to make a copy of the file in the same folder. i later opened the same project in visual studio and got this same error when trying to compile. i just had to exclude the files i created from making a copy from the project by right clicking on the problem file and selecting "exclude from project". did a build and viola! the copy was still in the folder just not being included in the build.

我有一个项目,我在记事本++中打开主程序.cs,进行了一些编辑并进行了“另存为”以在同一文件夹中制作文件的副本。我后来在 Visual Studio 中打开了同一个项目,并在尝试编译时遇到了同样的错误。我只需要通过右键单击问题文件并选择“从项目中排除”来排除我从项目中复制创建的文件。做了一个构建和中提琴!该副本仍在文件夹中,只是未包含在构建中。

回答by Honza P.
In my case the reason of this error was finally as simple as this. I added a new DB table to my EDMX in my DB project. I accidentally ticked the box for generation of class with methods (but these were already generated in the project). The EDMX file after this contained two similarly named classes AB.Context.tt and AB.xxx.Context.tt and both contained the same methods. As the classes were partial the mentioned error arose.
在我的情况下,这个错误的原因最终就这么简单。我在我的数据库项目中向我的 EDMX 添加了一个新的数据库表。我不小心勾选了使用方法生成类的框(但这些已经在项目中生成了)。此后的 EDMX 文件包含两个类似命名的类 AB.Context.tt 和 AB.xxx.Context.tt,并且都包含相同的方法。由于课程是部分的,因此出现了上述错误。
The solution was to remove accidentally and freshly added secondary AB.xxx.Contex.tt file and rebuild the project.
解决方案是删除意外和新添加的辅助 AB.xxx.Contex.tt 文件并重建项目。

