好的,这是我的代码:
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();
}
}
}它的意思是获取一个文件,读取每一行,把它放在列表框中,然后在单击名称时显示数字。问题是VS正在发送以下错误:
Error CS0229 Ambiguity between 'Form1.phoneList' and 'Form1.phoneList' Phonebook D:\Google Drive\VS Projects\Phonebook\Phonebook\Form1.cs 71
Error CS0229 Ambiguity between 'Form1.phoneList' and 'Form1.phoneList' Phonebook D:\Google Drive\VS Projects\Phonebook\Phonebook\Form1.cs 59
Error CS0229 Ambiguity between 'Form1.phoneList' and 'Form1.phoneList' Phonebook D:\Google Drive\VS Projects\Phonebook\Phonebook\Form1.Designer.cs 31
Error CS0229 Ambiguity between 'Form1.phoneList' and 'Form1.phoneList' Phonebook D:\Google Drive\VS Projects\Phonebook\Phonebook\Form1.Designer.cs 40
Error CS0229 Ambiguity between 'Form1.phoneList' and 'Form1.phoneList' Phonebook D:\Google Drive\VS Projects\Phonebook\Phonebook\Form1.Designer.cs 41
Error CS0229 Ambiguity between 'Form1.phoneList' and 'Form1.phoneList' Phonebook D:\Google Drive\VS Projects\Phonebook\Phonebook\Form1.Designer.cs 42
Error CS0229 Ambiguity between 'Form1.phoneList' and 'Form1.phoneList' Phonebook D:\Google Drive\VS Projects\Phonebook\Phonebook\Form1.Designer.cs 43
Error CS0229 Ambiguity between 'Form1.phoneList' and 'Form1.phoneList' Phonebook D:\Google Drive\VS Projects\Phonebook\Phonebook\Form1.Designer.cs 44
Error CS0229 Ambiguity between 'Form1.phoneList' and 'Form1.phoneList' Phonebook D:\Google Drive\VS Projects\Phonebook\Phonebook\Form1.Designer.cs 92
Error CS1579 foreach statement cannot operate on variables of type 'Phonebook.Form1.phoneList' because 'Phonebook.Form1.phoneList' does not contain a public definition for 'GetEnumerator' Phonebook D:\Google Drive\VS Projects\Phonebook\Phonebook\Form1.cs 71
Error CS0103 The name 'nameListBox' does not exist in the current context Phonebook D:\Google Drive\VS Projects\Phonebook\Phonebook\Form1.cs 73
Error CS0102 The type 'Form1' already contains a definition for 'phoneList' Phonebook D:\Google Drive\VS Projects\Phonebook\Phonebook\Form1.cs 23这个项目是为我的C#入门课程,我确保一切都是正确的,但我只是无法使它正常工作。这里有什么问题?
发布于 2015-11-19 03:17:26
Error CS0102 The type 'Form1' already contains a definition for 'phoneList'您有一个名为phoneList的控件,还有一个名为phoneList的列表。编译器不知道你指的是哪一个。说出其中一个不同的东西。
我看到使用的惯例之一(有时我也会这样做)是使用控制类型缩写作为后缀或前缀。例如:
phoneList_lb,lbPhoneList,phoneListLb
只是告诉你,你不是在处理代码中的某些东西,而是一个控制。
发布于 2015-11-19 03:16:26
我认为您错误地将表单设计器中的ListBox控件命名为phoneList,并声明了另一个名为phoneList的列表。
将控件命名为不同。在designer上创建的每个控件都将具有同名的相应变量声明。
将控件命名为phoneListBox,然后设置值:
phoneListBox.Items.AddRange(phoneList.ToArray());https://stackoverflow.com/questions/33794607
复制相似问题