您好我正在尝试使用列表来构建联系人管理器程序来存储和显示数据。我需要查看显示可用联系人摘要的报告,然后有一个菜单允许用户与该程序进行交互。我需要制作一种方法,允许用户创建一个新的联系人,其中包含他们的姓名,电话号码,电子邮件地址和类型。但我不知道如何用数组做这个。
任何指导都将不胜感激。
static void Main(string[] args)
{
//The MAXPLAYERS constant is the physical table size
const Int32 MAXCONTACTS = 23;
//Declare the player tables
Contact[] contacts = new Contact[MAXCONTACTS];
//Keep track of the actual number of contacts (i.e. logical table size)
Int32 contactCount = 0;
Int32 number = 0;
String lastName = " ";
String phoneNumber = " ";
String emailAddress = " ";
String firstName = " "; ;
Console.WriteLine("Contact List");
// display the menu to the user
Console.WriteLine("Enter option or M for menu:");
//Main Driver
char menuItem;
menuItem = GetMenuItem();
while (menuItem != 'X')
{
ProcessMenuItem(menuItem, firstName, lastName, phoneNumber, emailAddress, contacts, ref contactCount, MAXCONTACTS);
menuItem = GetMenuItem();
}
Console.WriteLine("\nThank you, goodbye");
Console.ReadLine();
}
static char GetMenuItem()
{
char menuItem;
DisplayMenu();
menuItem = IOConsole.GetChar((Console.ReadLine()));
while (menuItem != 'C'
&& menuItem != 'L' && menuItem != 'X' && menuItem != 'R' && menuItem != 'U' && menuItem != 'D')
{
Console.WriteLine("\nError - Invalid menu item");
DisplayMenu();
//menuItem = IOConsole.GetChar((Console.ReadLine()));
}
return menuItem;
}
static void DisplayMenu()
{
Console.WriteLine("C-> Create Contacts");
Console.WriteLine("R-> Remove Contacts");
Console.WriteLine("U-> Update Contacts");
Console.WriteLine("D -> Load data from file");
Console.WriteLine("S-> Save data to file");
Console.WriteLine("L-> View sorted by last name");
Console.WriteLine("F-> View sorted by first name");
Console.WriteLine("P-> View by partial name search");
Console.WriteLine("T-> View by contact type");
Console.WriteLine("Q-> Quit");
}
//Routes to the appropriate process routine based on the user menu choice
static void ProcessMenuItem(Char menuItem, String firstName, String lastName, String phoneNumber,
String emailAddress, Contact[] contacts, ref Int32 contactCount, Int32 MAXCONTACTS)
{
switch (menuItem)
{
case 'C':
createContact();
break;
case 'R':
removeContact();
break;
case 'U':
updateContact();
break;
case 'D':
LoadToFile();
break;
case 'S':
saveToFile();
break;
case 'L':
sortByLastName();
break;
case 'F':
sortByFirstName();
break;
case 'P':
break;
case 'T':
break;
case 'Q':
break;
}
}
public static void createContact()
{
}
这是我的联系对象类中的枚举方法,用于类型。
enum ContactTypesEnum { Family, Friend, Professional }
答案 0 :(得分:0)
有许多可能的方法可以实现这一点,可以使用泛型类型声明类型为List
的对象,例如List<T>
,其中T是对象的类型。
您可以声明List<Contact>
或根据List<Contact>
派生一个类。
为了进一步学习,我建议查看List<T>
课程的一些文章。
参考:https://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx
您还可以查看以下代码作为其他学习资料:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
namespace TestContacts
{
public enum ContactTypesEnum {
Family,
Friend,
Professional
}
public class Contact
{
public int Number { get; set; }
public string PhoneNumber { get; set; }
public string EmailAddress { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public ContactTypesEnum Type { get; set; }
}
//I would suggest you check the List collection with generic data types
//so you will be able to assign your own objects
public class ContactCollection : List<Contact>
{
public Int32 ContactCount
{
get
{
return this.Count;
}
}
}
class Program
{
static void Main(string[] args)
{
Contact c1 = new Contact();
c1.Number = 1; //Id?
c1.LastName = "Doe";
c1.FirstName = "John";
c1.EmailAddress = "johndoe@email.com";
c1.PhoneNumber = "12345678";
c1.Type = ContactTypesEnum.Friend;
//Create more contacts...
//Add all contacts here
ContactCollection contactList = new ContactCollection();
contactList.Add(c1);
//Loop through list
foreach( Contact c in contactList)
{
Console.WriteLine(c.FirstName); //Do something with fields
}
Console.ReadLine();
}
}
}
答案 1 :(得分:0)
如果您使用List<T>
代替数组,正如Joel Legaspi Enriquez所述,您可以轻松地在列表中使用大量有用的 Linq 函数作为Enumerable
通过在程序的乞讨时添加using System.Linq;
来对象
我相信你可以自己使用它。或者C#中的Google Linq ! ;)