[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CustomCollections
Here I will explain how to write, implement and use a custom collection.
First thing you will need is to create a new class within your project.
Name the Class Population.
Add using System.Collections; To the using header (For ease. You could just type out the whole thing)
Before you do this you have to think about the class you wish to collect. Is it an existing class? Where will it be located? What additional functionality will an single instance of your collected “object” need to function as you need it?
Remember: You can’t just jump in with guns blazing… C# is very structured and the more structured your approach is to your problem, the more C# works for you.
Let’s begin.
We collecting Humans, right.
We want Name, Surname and TelephoneNumber (You can add any property to this class you want. I am just using an example.)
Now we have our Human class. Let’s collect it now.
Try this out. Open note pad and past this example into it. Save the file (in a place you will find it again). Call it Population.cs Open the Visual Studio .NET Command prompt. (Please note that this needs to be the Visual Studio .NET command prompt. You will have to set the paths to the compiler directory if you don’t have Visual Studio.)
Navigate to the directory you have saved the code file and type “csc Population.cs” Afterwards you should see an exe file called Population.exe. Run Population.exe from the command Prompt. You will see Pieter. Thus is a very simple example of one of the ways to implement a custom collection.
The custom collection can be bound to a data grid!!! Try it.
If you feel that I have left something important out or that you would like to add, don’t hesitate.
Plan your work and work your plan.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
CustomCollections
Custom Collections
Here I will explain how to write, implement and use a custom collection.
First thing you will need is to create a new class within your project.
Name the Class Population.
Add using System.Collections; To the using header (For ease. You could just type out the whole thing)
using System;
using System.Collections;
using System.Text;
public Class Population : CollectionBase
{
}
When you inherit from Collection base there are some methods that can be overridden. Nl.
· Add()
· Remove()
· The indexer.Before you do this you have to think about the class you wish to collect. Is it an existing class? Where will it be located? What additional functionality will an single instance of your collected “object” need to function as you need it?
Remember: You can’t just jump in with guns blazing… C# is very structured and the more structured your approach is to your problem, the more C# works for you.
Let’s begin.
We collecting Humans, right.
We want Name, Surname and TelephoneNumber (You can add any property to this class you want. I am just using an example.)
public class Human
{
private string name;
public string Name
{
get{return name;}
set{name = value;}
}
}
Encapsulation states that we use methods to manipulate the classes properties and hade the properties them self’s. Therefore the Get and Set methods appear here.
Remember you can use the property directly within the human class, but it will be protected from the outside.
Here the warning: If you add extra functionality to the Get and Set methods, be selective about how you wish to update the values of the properties.Now we have our Human class. Let’s collect it now.
using System;
using System.Collections;
using System.Text;
public class Population : CollectionBase
{
/// This here is the indexer.
///You will notice that we use this to access a particular object in the ///collection with the indexer.
public Human this[int HumanIndex]
{
get{return List[HumanIndex] as Human;}
}
public void Add(Human aPerson)
{
List.Add(aPerson);
}
}
public class Human
{
public Human(string HumanName)
{
this.name = HumanName;
}
private string name;
public string Name
{
get{return name;}
set{name = value;}
}
}
public class GeneratePopulation
{
public static void Main(string[] args)
{
Population myPopulation = new Population();
myPopulation.Add(new Human("Pieter"));
Console.Write(myPopulation[0].Name);
}
}
Try this out. Open note pad and past this example into it. Save the file (in a place you will find it again). Call it Population.cs Open the Visual Studio .NET Command prompt. (Please note that this needs to be the Visual Studio .NET command prompt. You will have to set the paths to the compiler directory if you don’t have Visual Studio.)
Navigate to the directory you have saved the code file and type “csc Population.cs” Afterwards you should see an exe file called Population.exe. Run Population.exe from the command Prompt. You will see Pieter. Thus is a very simple example of one of the ways to implement a custom collection.
The custom collection can be bound to a data grid!!! Try it.
If you feel that I have left something important out or that you would like to add, don’t hesitate.
Plan your work and work your plan.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
