用户定义的集合 [英] user defined collection

查看:70
本文介绍了用户定义的集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个用户定义的集合.

 使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
命名空间 CompanyDocs
{
    公共 文档
    {
        公共 字符串名称;
        公共 字符串姓;
        公共 字符串标题;
        // 构造函数
        公共 Documents()
        {}
        公共文档(字符串 iName,字符串 iSurname ,字符串 iTitle)
        {
            名称= iName;
            姓= iSurname;
            标题= iTitle;
        }
        公共 虚拟 字符串 GetData()
        {
            返回名称+ "  +姓+ "  +标题;
        }
    }
} 



这是我的收藏集:

 命名空间 CompanyDocs
{
    公共  StoreObjects:List< Documents>
    {
        
        公共 StoreObjects(){} // 构造函数

        公共 无效 GetDataOfAll()
        {
             foreach (文档  this 中的
                Console.WriteLine(d.GetData());
        }
    }
} 



我有2种形式:1是我要输入数据的地方,我有3个文本框(即姓名姓氏),并且必须按钮之一是在同一表单中显示RichTextBox中的所有数据.另一个按钮调用另一个表单,其中有一个按钮和文本框,可以按标题搜索文档,并将结果显示在Form2中的另一个RichTextBox中.

我对如何显示此数据感到困惑

我需要的是:
1.方法GetDataOfAll()返回一个字符串,而不是void
2.如何实现搜索工具.

首先,将您的类的名称从文档"更改为文档":复数表示它包含多个文档,而文档不包含此文档. t.您怎么称呼文档"数组?如果您不对单数对象使用复数形式,则可以编写显而易见的代码:

 Document []文档= myLibrary.GetAll(" );
 foreach (文档文件 中的文档)
   {
   ...
   } 

此外,考虑更改StoreObjects类的名称:它是否存储对象"项?或文档"项目?

其次,返回一个字符串而不是一个void:

  public   string  GetDataOfAll()
   {
   StringBuilder sb =  Stringbuilder();
    foreach (文档  this 中的
      {
      sb.Append(d.GetData()+ " );
      Console.WriteLine(d.GetData());
      }
   返回 sb.ToString();
   } 

您不必使用StringBuilder,只需将字符串与
连接

 myReturnString + = d.GetData(); 

但是StringBuilder的效率要高得多.

第三,要进行搜索,请提供两个例程:
在StoreObjects中:

  public 文档查找(字符串 searchText)
   {
    foreach (文档文档 in   this )
      {
      如果(document.Find(searchText))
         {
         返回文档;
         }
      }
   返回 ;
   } 


在文档中:

 公共  bool 查找(字符串 searchText)
   {
   找到布尔 =  false ;
   // 在此处进行文档搜索,如果找到文本,则将"found"设置为"true" 
   找到返回;
   } 



我不知道您要搜索的文档内容,而是将文档搜索留给您!

我应该从表单中调用哪个方法?文档中的方法还是myLibrary中的方法?"

在您的表单中,只需要在库中调用方法,就不需要foreach循环,因为它位于库方法中:

文档d = myLib.Find(txtTitle.Text);
如果(d!= )
   {
   // 找到了文档!
   richTextBox1.AppendText(d.GetData()+ " );
   ...
   } 

通过这种方式,它可以保持整洁:您的表单无需知道库将如何存储文档:您只需说给我这个文档"即可完成所有工作.这样,图书馆就可以在将来进行更改,而不必更改表格以保持最新状态.

因此,我不会直接从列表中派生库,而是将列表作为私有类变量嵌入到库中.这意味着您必须自己添加"Add"和"Remove"方法,但是它会抽象化数据存储,以便将来在需要时更容易进行更改.添加和删​​除只是调用私有列表的适当方法.


这是我要执行的操作:

A)公司的IT部门正在使用集合来存储包含有关
信息的对象 公司文件.集合中要存储的文档类型为:
-每个文档都有作者的姓氏和名称以及标题

以下是要执行的查询/操作:
-列出所有公司文件,同时显示特定文件的所有详细信息
-按作者姓氏在集合中搜索特定文档

-每个类都必须具有或继承GetData()方法,该方法返回由ALL
B)在C#Windows应用程序中实现类
-类层次结构的最高级祖先的构造函数,除了
结构,初始化其所有数据字段
类Documents的方法GetData()

C)专门用于存储公司文档的所有对象的收集类
根据类层次结构,从任何.NET合适的集合类(例如ArrayList
)继承 类或通用列表).该集合将实现单个方法GetDataOfAll()返回
一个字符串,使用适当的分隔符将所有对象的数据连接在一起. (该方法稍后会
用于在合适的输出占位符中显示数据)

D)通过以下方法扩展集合的功能:
SearchByTitle(字符串aTitle)
它会在集合中搜索匹配的标题,并返回对文档的引用
对象或null(如果找不到).


I am creating a user defined collection.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CompanyDocs
{
    public class Documents
    {
        public string Name;
        public string Surname;
        public string Title;
        //Constructors
        public Documents()
        {}
        public Documents(string iName, string iSurname, string iTitle)
        {
            Name = iName;
            Surname = iSurname;
            Title = iTitle;
        }
        public virtual string GetData()
        {
            return Name + " - " + Surname + " : " + Title;
        }
    }
}



This is my Collection:

namespace CompanyDocs
{
    public class StoreObjects:List<Documents>
    {
        
        public StoreObjects() { } //Constructor

        public void GetDataOfAll()
        {
            foreach (Documents d in this)
                Console.WriteLine(d.GetData());
        }
    }
}



I have 2 forms: 1 is where I am inputting data, where I have 3 text boxes (i.e. Name Surname Title), and have to buttons one is to show all the data in a RichTextBox within the same form. The other button calls another form where I have a button and textbox to be able to search for a Document by Title, and display the result in another RichTextBox within Form2.

I am getting stuck of how to display this data

What I need is that :
1. the method GetDataOfAll() returns a string instead of void
2. how can i implement the search facility.

Thanks

解决方案

Firstly, change the name of your class from "Documents" to "Document": the plural implies it contains more than one document, which it doesn''t. What could you call an array of "Documents"? If you do not use plurals for singular objects you can write code which is immediately obvious:

Document[] documents = myLibrary.GetAll("Hamsters");
foreach (Document document in documents)
   {
   ...
   }

Additionally, consider changing the name of your StoreObjects class: Does it store "Object" items? or "Document" items?

Secondly, to return a string instead of a void:

public string GetDataOfAll()
   {
   StringBuilder sb = new Stringbuilder();
   foreach (Documents d in this)
      {
      sb.Append(d.GetData() + "\n");
      Console.WriteLine(d.GetData());
      }
   return sb.ToString();
   }

You don''t have to use a StringBuilder, you could just concatenate the strings with

myReturnString += d.GetData();

But StringBuilder is a lot more efficient.

Thirdly, to search, provide two routines:
In StoreObjects:

public Document Find(string searchText)
   {
   foreach (Document document in this)
      {
      if (document.Find(searchText))
         {
         return document;
         }
      }
   return null;
   }


In Document:

public bool Find(string searchText)
   {
   bool found = false;
   // Do your document search here, and set "found" to "true" if you find the text
   return found;
   }



I leave the document search to you, as I have no idea of your document content!

"Should I call which method from my Form? the method in Document or the one in myLibrary?"

In your form, you only need to call the method in your library, you don''t need the foreach loop as it is inside the library method:

Document d = myLib.Find(txtTitle.Text);
if (d != null)
   {
   // Found the document!
   richTextBox1.AppendText(d.GetData() + "\n");
   ...
   }

This way it is kept cleaner: your form doesn''t need to know what your library does to store the documents: you just say "Give me this document" and it does all the work. That way, the Library can change in future, without the form having to change to keep up.

For that reason, I wouldn''t derive the Library from the List directly, but would embed the list in the library as a private class variable. It means you have to add the "Add" and "Remove" methods yourself, but it abstracts your data store so it is easier to change in future if you need to. The Add and Remove just call the appropriate methods of the private list.


This is what I was asked to do:

A) The IT section of a company is using a collection to store objects containing information about
company''s documents. The types of Documents to be stored on the collection are:
- Each document has the Surname and Name of the author and Title

The following are queries / operations to be carried out:
- listing of all company’s documents while displaying all details of a particular document
- searching the collection for a particular document by surname of author

- each class must have or inherit the method GetData() returning string composed of ALL
class’s data

B) Implement the classes in a C# Windows application program
- a constructor of an upmost ancestor of the class hierarchy which, apart from the
construction, initializes ALL its data fields
the method GetData() for class Documents

C) a collection class dedicated to store all objects of company documents
as per the class hierarchy, inheriting from any .NET suitable collection class (e.g. the ArrayList
class, or generic List). The collection is to implement a single method GetDataOfAll() returning
a string which concatenates data of all objects with suitable separators. (The method will later
be used to display data in a suitable output placeholder)

D) Extend the functionality of the collection by the following method:
SearchByTitle(string aTitle)
which searches the collection for a matching title, returning the reference to the document
object or null, if not found.


这篇关于用户定义的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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