如何从C#中的arralist中检索数据? [英] How to retrieve data from arralist in C#?

查看:68
本文介绍了如何从C#中的arralist中检索数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它没有显示数据,

显示System.SampleApp3.collection



我尝试过:



its not dispalying the data,
its showing System.SampleApp3.collection

What I have tried:

namespace SampleApp3
{
    public class BaseEmployee
    {
        public int Id;
        public string Name;
    }

    public class Employee : BaseEmployee
    {
        public string Address;



        public Object Add()
        {
           
            Employee employeeDetail = new Employee();
            Console.Write("Employee Id:");
            employeeDetail.Id = int.Parse(Console.ReadLine());

            Console.Write("Employee Name:");
            employeeDetail.Name = Console.ReadLine();
            Console.Write("Employee Address:");
            employeeDetail.Address = Console.ReadLine();
            return employeeDetail;

        }
     





        static void Main(string[] args)
        {

           ArrayList arraylist = new ArrayList();

         
            Employee employeeDetails = new Employee();

            while (true)
            {
                
                Console.WriteLine("Employee Record Management System\n");
                Console.WriteLine("\n1)Add");
                Console.WriteLine("\n2)Edit");
                Console.WriteLine("\n3)Delete");
                Console.WriteLine("\n4)Dispaly");
                Console.WriteLine("\n5)Exit");
                Console.WriteLine("\n Select opertaion");
                int Option = int.Parse(Console.ReadLine());

                switch (Option)
                {
                    case 1:
                        arraylist.Add(employeeDetails.Add());
                            //arraylist.Add(employeeDetails.Add());
                            break;


                    case 4:
                        foreach(Object obj in arraylist)
                        {
                            
                            Console.Write(obj);
                        }
                       
                        Console.Read();
                       

                        break;



                }

              

            }

推荐答案

首先,停止返回对象 - 返回实际的类,它更有用:

First off, stop returning Objects - return the actual class instead, it's a lot more useful:
public Employee Add()
{
    Employee employeeDetail = new Employee();
    Console.Write("Employee Id:");
    employeeDetail.Id = int.Parse(Console.ReadLine());

    Console.Write("Employee Name:");
    employeeDetail.Name = Console.ReadLine();
    Console.Write("Employee Address:");
    employeeDetail.Address = Console.ReadLine();
    return employeeDetail;
}



但是在真正的应用程序中,你也不应该这样做。您不需要添加方法 - 您应该使用构造函数:


But in a "real" application, you shouldn't do that either. You don't want an "Add" method - you should use a constructor:

public Employee(int id, string name, string address)
    {
    Console.Write("Employee Id:");
    Id = id;
    Name = name;
    Address address;
    }



然后让您的控制台在Main方法中工作并使用结果构建一个新员工:


Then do your Console work in the Main method and use the results to construct a new Employee:

Console.Write("Employee Id:");
int id = int.Parse(Console.ReadLine());
Console.Write("Employee Name:");
string name = Console.ReadLine();
Console.Write("Employee Address:");
string address = Console.ReadLine();
Employee employeeDetail = new Employee(id, name, address);
arrayList.Add(employeeDetail);

这样,您的Employee类可以重复使用,因为它不需要知道信息的来源。



然后,当您从ArrayList中获取对象时,将其强制转换为Employee:

That way, your Employee class is reusable, because it doesn't need to know where the info comes from.

Then, when you fetch an object from your ArrayList, cast it to an Employee:

foreach(Object obj in arraylist)
    {
    Employee emp = obj as Employee;
    Console.Write("{0}: {1}, {2}", emp.Id, emp.Name, emp.Address);
    }



但是...我不会使用ArrayList - 它们非常过时,并且很长时间被Generic集合所取代以前 - 看看使用List< Employee>相反。


But...I wouldn't use an ArrayList - they are very very out of date, and were supplanted by Generic collections a very long time ago - look at using a List<Employee> instead.


您正在尝试编写没有适当ToString()覆盖的对象。有几种方法可以解决这个问题。



修复开关声明:



You're trying to write the object that doesn't have an appropriate ToString() override. There are a few ways you can fix this.

Fix the switch statement:

case 4:
    foreach(Employee obj in arraylist)
    {
        Console.Write(string.Format("{0} {1} {2}",obj.Id, obj.Name, obj.Address));
    }





或者将一个ToString()方法添加到Employee类并保留你已经拥有的switch语句:





Or add a ToString() method to the Employee class and leave the switch statement as you already have it:

public override string ToString()
{
    return string.Format("{0} {1} {2}", this.Id, this.Name, this.Address);
}


这篇关于如何从C#中的arralist中检索数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆