C# 列表和列表之间存在歧义。我该如何解决这个问题?
好的,这是我的代码:C# 列表和列表之间存在歧义。我该如何解决这个问题?,c#,.net,C#,.net,好的,这是我的代码: 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 System.IO; namespa
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 System.IO;
namespace Phonebook
{
struct PhoneBookEntry
{
public string name;
public string phone;
}
public partial class Form1 : Form
{
private List<PhoneBookEntry> phoneList =
new List<PhoneBookEntry>();
public Form1()
{
InitializeComponent();
}
//Reads PhoneList.txt, and stores its objects in phoneList.
private void ReadFile()
{
try
{
StreamReader inputFile;
string line;
PhoneBookEntry entry = new PhoneBookEntry();
char[] delim = { ',' }; //Create array.
inputFile = File.OpenText("PhoneList.txt"); //Open the .txt file.
//Read file.
while (!inputFile.EndOfStream)
{
//Read line from file.
line = inputFile.ReadLine();
//Tokenize the line.
string[] tokens = line.Split(delim);
//Store tokens in the entry object.
entry.name = tokens[0];
entry.phone = tokens[1];
//Add it to the list.
phoneList.Add(entry);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//Display names.
private void DisplayNames()
{
foreach (PhoneBookEntry entry in phoneList)
{
nameListBox.Items.Add(entry.name);
}
}
private void Form1_Load(object sender, EventArgs e)
{
ReadFile(); //Read PhoneList.txt
DisplayNames(); //Display names.
}
private void exitButton_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
这个项目是为我的C#入门课程准备的,我确保了一切都是正确的,但我就是不能让它正常工作。这里有什么问题?我认为您在表单设计器中将ListBox控件错误地命名为phoneList,并声明了另一个名为phoneList的列表 以不同的方式命名控件。在designer上创建的每个控件都有各自的同名变量声明 将控件命名为phoneListBox,然后设置值:
phoneListBox.Items.AddRange(phoneList.ToArray());
我认为您错误地将表单设计器中的ListBox控件命名为phoneList,并声明了另一个名为phoneList的列表 以不同的方式命名控件。在designer上创建的每个控件都有各自的同名变量声明 将控件命名为phoneListBox,然后设置值:
phoneListBox.Items.AddRange(phoneList.ToArray());
您的控件名为phoneList
,您的列表名为phoneList
。编译器不知道你指的是哪一个。说出其中一个不同的名字
我看到的一个惯例(有时我也会这样做)是使用控件类型缩写作为后缀或前缀。例如:
电话列表,lbPhoneList,phoneListLb
这会告诉你,你不是在处理代码中的某些东西,而是一个控件
您的控件名为phoneList
,您的列表名为phoneList
。编译器不知道你指的是哪一个。说出其中一个不同的名字
我看到的一个惯例(有时我也会这样做)是使用控件类型缩写作为后缀或前缀。例如:
电话列表,lbPhoneList,phoneListLb
只是告诉你你没有处理代码中的某些内容,而是一个控件。你有一个名为“phoneList”的控件吗?@B.K.是的。列表框被称为“电话列表”。啊,简单的修复。看看我的答案。你有一个名为“电话列表”的控件吗?@B.K.是的。列表框被称为“电话列表”。啊,简单的修复。请参阅我的答案。1对于lbPhoneList,下划线后缀很难看!不过,我个人的看法是,你会讨厌我说的话的。首先,c#是强类型的。匈牙利符号是不必要的,而且很烦人。公共或受保护的属性和字段应为pascal大小写,例如PhoneListBox。变量名告诉您它是一个列表框控件。私有字段应该是驼峰大小写,许多人更喜欢以下划线开头,例如_phoneList。这是为了区别于参数和本地私有变量,它们只是不带下划线的驼峰大小写,例如phoneList。我在支持属性的私有字段上使用u。在不支持属性的字段上,我使用驼峰大小写“thisIsaField”。我从不公开字段,如果需要的话,这是c#用属性公开它。我在属性上使用thisisa属性。我只在表单设计器为您创建的私有字段中使用txtFirstName。我在html中这样做是因为html页面可能会变得庞大,在javascript中我想知道txtJobState是一个输入标记,而不必键入jobStateInput.1对于lbPhoneList,下划线后缀很难看!不过,我个人的看法是,你会讨厌我说的话的。首先,c#是强类型的。匈牙利符号是不必要的,而且很烦人。公共或受保护的属性和字段应为pascal大小写,例如PhoneListBox。变量名告诉您它是一个列表框控件。私有字段应该是驼峰大小写,许多人更喜欢以下划线开头,例如_phoneList。这是为了区别于参数和本地私有变量,它们只是不带下划线的驼峰大小写,例如phoneList。我在支持属性的私有字段上使用u。在不支持属性的字段上,我使用驼峰大小写“thisIsaField”。我从不公开字段,如果需要的话,这是c#用属性公开它。我在属性上使用thisisa属性。我只在表单设计器为您创建的私有字段中使用txtFirstName。我在html中这样做是因为html页面会变得庞大,在javascript中,我想知道txtJobState是一个输入标记,而不必键入jobStateInput。
Error CS0102 The type 'Form1' already contains a definition for 'phoneList'