首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >列表和列表之间的歧义。我该怎么解决这个问题?

列表和列表之间的歧义。我该怎么解决这个问题?
EN

Stack Overflow用户
提问于 2015-11-19 03:13:50
回答 2查看 9.9K关注 0票数 1

好的,这是我的代码:

代码语言:javascript
复制
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正在发送以下错误:

代码语言:javascript
复制
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#入门课程,我确保一切都是正确的,但我只是无法使它正常工作。这里有什么问题?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-11-19 03:17:26

代码语言:javascript
复制
Error   CS0102  The type 'Form1' already contains a definition for 'phoneList'

您有一个名为phoneList的控件,还有一个名为phoneList的列表。编译器不知道你指的是哪一个。说出其中一个不同的东西。

我看到使用的惯例之一(有时我也会这样做)是使用控制类型缩写作为后缀或前缀。例如:

phoneList_lb,lbPhoneList,phoneListLb

只是告诉你,你不是在处理代码中的某些东西,而是一个控制。

票数 3
EN

Stack Overflow用户

发布于 2015-11-19 03:16:26

我认为您错误地将表单设计器中的ListBox控件命名为phoneList,并声明了另一个名为phoneList的列表。

将控件命名为不同。在designer上创建的每个控件都将具有同名的相应变量声明。

将控件命名为phoneListBox,然后设置值:

代码语言:javascript
复制
phoneListBox.Items.AddRange(phoneList.ToArray());
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33794607

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档