Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Computer Forums > Programmers Lounge > Programming Discussions » the "this" reference & indexers
Closed Thread
Old 01-15-2009, 04:48 PM   #1 (permalink)
 
Junior Techie

Join Date: Nov 2008

Posts: 79

BobLewiston is on a distinguished road

Default the "this" reference & indexers

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?

Last edited by Mak213; 01-24-2009 at 02:40 PM. Reason: added code tags
BobLewiston is offline  
Old 01-15-2009, 05:05 PM   #2 (permalink)
Baez's Avatar
 

Join Date: Sep 2005

Location: Toronto, Canada

Posts: 5,465

Baez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of light

Default Re: the "this" reference & indexers

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.
__________________

Baez is offline  
Old 01-15-2009, 05:17 PM   #3 (permalink)
 
Junior Techie

Join Date: Nov 2008

Posts: 79

BobLewiston is on a distinguished road

Default 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?
BobLewiston is offline  
Old 01-15-2009, 05:43 PM   #4 (permalink)
 

Join Date: Jul 2005

Location: England

Posts: 2,159

kmote has a spectacular aura aboutkmote has a spectacular aura about

Default Re: the "this" reference & indexers

yes, yes it does [.code][./code] (without dots)
__________________
MSI P43 Neo|Enermax Pro82+ 425W|E5200|silent 8500GT|250GB Samsung spinpoint F1|Samsung SATA DVD RW|4GB Corsair|Antec SOLO|openSUSE11


There are in order of increasing severity: lies, darn lies, statistics, and computer benchmarks. - diskinfo man page

Last edited by kmote; 01-15-2009 at 05:46 PM.
kmote is offline  
Old 01-16-2009, 12:42 PM   #5 (permalink)
 
Software Developer

Join Date: Mar 2006

Location: Columbus, OH

Posts: 569

jaeusm is on a distinguished road

Default Re: the "this" reference & indexers

Quote:
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".
jaeusm is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Foriegn key reference table in oracle vishnunekkanti Programming Discussions 2 07-15-2009 10:25 AM
Quick Reference Website CommonCents Hardware Troubleshooting 6 01-14-2009 04:13 PM
Got you a fresh one Trotter HijackThis Logs (finished) 16 10-20-2008 09:27 PM
Seeminly Unsolveable Problem! Reply ASAP! yoshi1476 Windows Operating Systems and Software 44 06-13-2008 12:48 AM