PDA

View Full Version : the "this" reference & indexers


BobLewiston
01-15-2009, 10:36 PM
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 [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?

Millenia
01-15-2009, 10:54 PM
Please wrap your code in [code] tags, it makes us easier to understand and make sense of it.

BobLewiston
01-15-2009, 11:05 PM
Please pardon my ignorance. I will be happy to comply, if you will explain what you mean. I don't know what you're talking about.

TheShaner
01-15-2009, 11:08 PM
I think you're looking to do this:

Citizen[] cit = new Citizen[5];

cit[0].Name = "Shane";
cit[0].Age = 27;

Also, I'm not sure what language you're writing in, so my syntax and semantics may be a bit off. Make sure you always specify the language you're programming in.

And as Millenia pointed out, highlight all your code next time and click the Pound Sign icon in the editor to wrap them in the CODE tags. This will keep your indentation and wrap your code in a nice, easy-to-read code box like mine above.

-Shane

BobLewiston
01-15-2009, 11:47 PM
My bad. I'm writing in C#.

Quote:
Citizen[] cit = new Citizen[5];

cit[0].Name = "Shane";
cit[0].Age = 27;

Thanks, but I tried that. No error is caused in Main by your code, but in class Citizen,

public string this [int i].Name

generates an error right at the ".", so I don't know how to access both .Name and .Age.

oesxyl
01-15-2009, 11:59 PM
Please pardon my ignorance. I will be happy to comply, if you will explain what you mean. I don't know what you're talking about.
this mean to put your code inside tags [ code] and [ /code] without spaces. You can edit your post to do that. Thank you.

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 [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?
this is a internal reference to the object and have access to object components no mater if are private, protected or public but can be used only inside of your class.
This is a general explanation and how you can access a array in your code depends on how is implemented a array container in the language you used.
I'm sorry but I don't use c#, .net or other ms stuff so I can help you further.

I guess that reading some basic things about OOP and how is implemented in the language you use will help you( search or post a thread to ask for some documentation).

regards

Fou-Lu
01-16-2009, 02:19 AM
The Shaner's code is what you want, but its a skeleton. You'll need to actually create instances of Citizen inside of you're array before trying to access it. As is, its mearly allocating memory of 5 * sizeof(Citizen) in memory in order to store the Citizen objects. this is never used outside of an internal object context, and C# uses variable masking so you're only required to use this if a method has redefined the value of a variable with the same name as a property.