Click here to Skip to main content
15,891,657 members
Home / Discussions / C#
   

C#

 
QuestionRe: Storing File Path on Database Pin
Eddy Vluggen25-Aug-12 23:34
professionalEddy Vluggen25-Aug-12 23:34 
AnswerRe: Storing File Path on Database Pin
ASPnoob26-Aug-12 0:27
ASPnoob26-Aug-12 0:27 
GeneralRe: Storing File Path on Database Pin
Eddy Vluggen26-Aug-12 0:40
professionalEddy Vluggen26-Aug-12 0:40 
QuestionChange Combobox Look in WPF Pin
a.fatemeh25-Aug-12 9:24
a.fatemeh25-Aug-12 9:24 
AnswerRe: Change Combobox Look in WPF Pin
Abhinav S25-Aug-12 18:31
Abhinav S25-Aug-12 18:31 
QuestionIndexers Pin
ripples24-Aug-12 23:59
ripples24-Aug-12 23:59 
AnswerRe: Indexers Pin
Wes Aday25-Aug-12 0:12
professionalWes Aday25-Aug-12 0:12 
AnswerRe: Indexers Pin
DaveyM6925-Aug-12 0:37
professionalDaveyM6925-Aug-12 0:37 
The easiest way to have indexers available to a class is to derive from an object that already has them, such as List<T>.
A simple example:
C#
public class EmployeeData
{
    private string name;
    private string department;

    public EmployeeData(string name, string department)
    {
        // Validation goes here
        this.name = name;
        this.department = department;
    }

    public string Name
    {
        get { return name; }
    }
    public string Department
    {
        get { return department; }
    }

    public override string ToString()
    {
        return string.Format("{0}, {1}", name, department);
    }
}
public class Employee : List<EmployeeData>
{
    public Employee(EmployeeData data)
        : base(new EmployeeData[] { data })
    { }
}

C#
Employee employee = new Employee(new EmployeeData("Fred", "Reception"));
employee.Add(new EmployeeData("George", "IT"));
List<Employee> employees = new List<Employee>();
employees.Add(employee);
foreach (Employee employeeItem in employees)
{
    foreach (EmployeeData employeeData in employeeItem)
    {
        Console.WriteLine(employeeData);
    }
}

If you don't want to expose your class as a List then you can wrap it and expose the parts you want using your own indexers:
C#
public class Employee : IEnumerable<EmployeeData>
{
    private List<EmployeeData> list;

    public Employee(EmployeeData data)
    {
        list = new List<EmployeeData>();
        list.Add(data);
    }

    public EmployeeData this[int index]
    {
        get { return list[index]; }
        set { list[index] = value; }
    }
    public int Count
    {
        get { return list.Count; }
    }

    public void Add(EmployeeData data)
    {
        list.Add(data);
    }
    public IEnumerator<EmployeeData> GetEnumerator()
    {
        return list.GetEnumerator();
    }
    IEnumerator IEnumerable.GetEnumerator()
    {
        return list.GetEnumerator();
    }
}

Dave

Binging is like googling, it just feels dirtier.
Please take your VB.NET out of our nice case sensitive forum.
Astonish us. Be exceptional. (Pete O'Hanlon)

BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)



AnswerRe: Indexers Pin
PIEBALDconsult25-Aug-12 4:40
mvePIEBALDconsult25-Aug-12 4:40 
AnswerRe: Indexers Pin
Richard MacCutchan25-Aug-12 7:07
mveRichard MacCutchan25-Aug-12 7:07 
GeneralRe: Indexers Pin
ripples25-Aug-12 8:52
ripples25-Aug-12 8:52 
Questioncan this be done without using 'dynamic: build a list of KeyValuePairs, where each item's Type varies ? Pin
BillWoodruff24-Aug-12 20:31
professionalBillWoodruff24-Aug-12 20:31 
AnswerRe: can this be done without using 'dynamic: build a list of KeyValuePairs, where each item's Type varies ? Pin
DaveyM6925-Aug-12 0:13
professionalDaveyM6925-Aug-12 0:13 
GeneralRe: can this be done without using 'dynamic: build a list of KeyValuePairs, where each item's Type varies ? Pin
BillWoodruff25-Aug-12 11:25
professionalBillWoodruff25-Aug-12 11:25 
AnswerRe: could this be done without dynamic Pin
Martijn Kok25-Aug-12 0:59
Martijn Kok25-Aug-12 0:59 
GeneralRe: could this be done without dynamic Pin
BillWoodruff25-Aug-12 11:33
professionalBillWoodruff25-Aug-12 11:33 
Questionasp.net Pin
Abubakar Shaikh .24-Aug-12 20:05
Abubakar Shaikh .24-Aug-12 20:05 
AnswerRe: asp.net Pin
Richard MacCutchan24-Aug-12 21:57
mveRichard MacCutchan24-Aug-12 21:57 
AnswerRe: asp.net Pin
Abhinav S25-Aug-12 2:38
Abhinav S25-Aug-12 2:38 
AnswerRe: asp.net Pin
Dave Kreskowiak25-Aug-12 4:23
mveDave Kreskowiak25-Aug-12 4:23 
QuestionProblem in datagridviewDeleteColumn Pin
Member 925960624-Aug-12 19:44
Member 925960624-Aug-12 19:44 
QuestionHow To Binding A List Of Frame? Pin
Bardiamarzbani24-Aug-12 9:42
Bardiamarzbani24-Aug-12 9:42 
AnswerRe: How To Binding A List Of Frame? Pin
Wes Aday24-Aug-12 10:46
professionalWes Aday24-Aug-12 10:46 
AnswerRe: How To Binding A List Of Frame? Pin
Dave Kreskowiak25-Aug-12 4:21
mveDave Kreskowiak25-Aug-12 4:21 
QuestionLearning C# for a beginner Pin
Diego Carrion24-Aug-12 8:16
Diego Carrion24-Aug-12 8:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.