Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

the "this" reference & indexers


  • Please log in to reply
1 reply to this topic

#1 BobLewiston

BobLewiston

  • Members
  • 69 posts
  • OFFLINE
  •  
  • Local time:07:45 PM

Posted 15 January 2009 - 04:31 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?

BC AdBot (Login to Remove)

 


#2 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:05:45 PM

Posted 15 January 2009 - 06:31 PM

I am not sure what you mean by "put these two techniques together". I am not exactly sure what you are trying to do.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users