Click here to Skip to main content
15,900,973 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am creating a user defined collection.

C#
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:

C#
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
Posted

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:
C#
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:
C#
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
C#
myReturnString += d.GetData();
But StringBuilder is a lot more efficient.

Thirdly, to search, provide two routines:
In StoreObjects:
C#
public Document Find(string searchText)
   {
   foreach (Document document in this)
      {
      if (document.Find(searchText))
         {
         return document;
         }
      }
   return null;
   }

In Document:
C#
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:
C#
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.
 
Share this answer
 
v2
Comments
datt265 23-Jan-11 4:03am    
I have changed class names as suggested, thanks very much for the info.

the GetAllOfData is working as it should

the only problem have is that I am getting the last inputted document when i do search

public bool Find(string aTitle)
{
bool foundTitle = false;

//if ((Title != null) && (Title == aTitle))

if (Title == aTitle)
{
foundTitle = true;
}

return foundTitle;
}
OriginalGriff 23-Jan-11 4:14am    
Assuming that code in is your Document class, it should work. Check your Store class Find method, and make sure it returns when it found a document match. You may want to put a breakpoint on the first instruction in the method and single step through.
datt265 23-Jan-11 5:20am    
Should I call which method from my Form? the method in Document or the one in myLibrary?

as i have managed to get the required data but noticed that I was only accesing the Document class

These are my methods as i have renamed them

-----------------in myLibrary-------------------
public Document SearchByTitle(string searchText)
{
foreach (Document d in this)
{
if (d.SearchByTitle(searchText))
{
//Console.WriteLine(d);
return d;
}
}
return null;
}
-----------------in Document-------------------

public bool SearchByTitle(string aTitle)
{
bool foundTitle = false;

if ((Title != null) && (Title == aTitle))

{
foundTitle = true;

}

return foundTitle;
}

-----------------in Form -------------------
private void cmdSearch_Click(object sender, EventArgs e)
{



foreach (Document d in myLib)
{
d.SearchByTitle(txtTitle.Text);
if ((d != null) && (d.Title == txtTitle.Text))
{
richTextBox1.AppendText(d.GetData() + "\n");

}//end if

if (d == null)
{
richTextBox1.AppendText("Title " + txtTitle.Text + "was not found");
}//end else
}
}
OriginalGriff 23-Jan-11 5:30am    
Answer updated
datt265 23-Jan-11 5:43am    
ok working now, thanks very much... I will now try to modify the code so that if I have 2 or more docs with the same title i get both as know i am only getting one
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900