the "this" reference & indexers

Status
Not open for further replies.

BobLewiston

In Runtime
Messages
182
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:
Code:
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 [i]; }
            set { name[i] = 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?
 
To keep the indentation you need to put it inside a code function.

Use
Code:
 [/code ] to create one. Take the space out between /code and ] though.
 
attention, Baez:

Does that mean that I type the word "code" in square brackets just before my code, and the word "/code" in square brackets just after my code?
 
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.
You cannot access private members of an object outside of its class. That's the purpose of the "private" access modifier. You can only use the "this" keyword within an instance method or instance property of some class. When you do use it, it allows access only to any member defined on the class in which you are using it. Technically, you don't have to use it at all, since any method or property you call without specifying an object will implicitly use "this".

Think of it this way: Any instance method or property you call has to be executed on some object. If you want to call the "Name" property on an instance of your Citizen class, you do it like so -- citizenInstance.Name. You seem to understand that concept. However, if you want to call a method from a different method on the same object, you can use the "this" keyword or just specify the name of the target method without using "this".
 
Status
Not open for further replies.
Back
Top Bottom