Can anybody give me a hint about how to put these two techniques together? I can use the "this" reference to control access to the private fields of an individual object:

using System;
namespace MyNamespace
{
class Program
{
static void Main ()
{
Citizen citizen = new Citizen ();


citizen.Name = "Larry Fine";
citizen.Age = 89;


Console.WriteLine ("{0}, {1}\n\n\n", citizen.Name, citizen.Age);
}
}


class Citizen
{
private string name;
private int age;


public string Name
{
get { return name; }
set { name = value; }
}


public int Age
{
get { return age; }
set { age = value; }
}
}
}

And I can use indexers to make an array of objects:

using System;
namespace MyNamespace
{
class Program
{
static void Main ()
{
Citizen citizen = new Citizen ();


citizen [0] = "Larry Fine";
citizen [1] = "Buster Douglas";
citizen [2] = "Mortimer Snerd";
citizen [3] = "Horace Greeley";
citizen [4] = "Ornette Coleman";


Console.WriteLine ("{0}\n{1}\n{2}\n{3}\n{4}\n", citizen [0],
citizen [1], citizen [2], citizen [3], citizen [4]);
}
}


class Citizen
{
private string [] name = new string [5];


public string this [int i]
{
get { return name ; }
set { name = value; }
}
}
}

But I can't figure out how to put the two techniques together to control access to the private fields of an array of objects. The solution is probably staring me right in the face, but I don't see it. Any help?

P.S. This is a console application, obviously.

P.P.S. BTW, what happened to all my indentation?

Dani AI

Generated

Short version: keep the per-object encapsulation (use this inside each Citizen) and expose a collection wrapper (or List<Citizen>) that provides an indexer which returns a Citizen. That lets client code use collection[0].Name and still keep fields private. was on the right track with indexing into an item, and ’s suggestion to use a list is the usual way to hold multiple objects.

A compact pattern: a small wrapper that holds a List<Citizen> and exposes a Citizen indexer plus basic Add/Count. Client code then uses the indexer to reach properties on each object.

// example wrapper (assumes a Citizen type exists)
class Citizens
{
    private List<Citizen> items = new List<Citizen>();

    public Citizen this[int i]
    {
        get { return items[i]; }
        set { items[i] = value; }
    }

    public void Add(Citizen c) => items.Add(c);
    public int Count => items.Count;
}

// usage
var pool = new Citizens();
pool.Add(new Citizen("Larry Fine", 89));
Console.WriteLine(pool[0].Name + ", " + pool[0].Age);

Notes and cautions: do bounds checking or surface a safer API (e.g., TryGet or IReadOnlyList<Citizen>). If you want enumeration, implement IEnumerable<Citizen>. Use this in constructors only to disambiguate parameter names from fields. For authoritative details see the C# docs on indexers and the this keyword.

Recommended Answers

All 3 Replies

If you post your code with code tags, the whitespace doesn't go away.

When posting c# code, please use c# code tags
[code=c#] // Your code here

[/code]

About the private members and array topic, I don't think I understand what you're asking.

You can do things like:

Console.WriteLine ("{0}, {1}\n\n\n", citizen[3].Name, citizen[3].Age);

Is that what you were trying to ask about?

Great Murtan :)

First off, you are doing it wrong, read about arrays and list.

You have this class { notice i added a constructor }

class Citizen
{
private string name;
private int age;

public string Name
{
get { return name; }
set { name = value; }
}

public int Age
{
get { return age; }
set { age = value; }
}

public Citizen(int name,int age)
{
 // "this.name" refers to the private field while "name" refers to the 
 // parameter of the constructor.
  this.name = name;
  this.name = age;
}
}
}

then you have your main

using System;
namespace MyNamespace
{
class Program
{
static void Main ()
{
Citizen citizen = new Citizen ();

citizen.Name = "Larry Fine";
citizen.Age = 89;

Console.WriteLine ("{0}, {1}\n\n\n", citizen.Name, citizen.Age);
}
}

then you started the next class that's when you got it wrong. read about List and Array's.

not try this main :

using System;
using System.Collections.Generic;

namespace MyNamespace
{
class Program
{
static void Main ()
{
List<Citizen> citizens = new List<Citizen>();

citizens.add(new Citizen("Name-1",13));
citizens.add(new Citizen("Name-2",35));
citizens.add(new Citizen("Name-3",38));


Console.WriteLine ("Name : '{0}', Age : '{1}'\n", citizens[0].Name, citizens[0].Age);
Console.WriteLine ("Name : '{0}', Age : '{1}'\n", citizens[1].Name, citizens[1].Age);
Console.WriteLine ("Name : '{0}', Age : '{1}'\n", citizens[2].Name, citizens[2].Age);

// So now you have this output :
// 
//  Name : 'Name-1' , Age : '13' 
//  Name : 'Name-2' , Age : '35 
//  Name : 'Name-3 , Age : '38'

}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.