473,396 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

using indexed properties to return a object member from an array.

I have a scenario where I am writing a property(using get and set) have to return a member of an object stored in an array of objects! The class user has to pass an index based on which a object i's member is returned from the array.

How will the property be declared in C#?

Nov 16 '05 #1
1 2457
There are two ways to solve this problem
(1) Use Indexed Property as follows:
class A
{
object[] arr = {123, "Text" , 456.55};
public object this[int index]
{
get { return arr[index]; }//add proper checks on index bounds here
set { arr[index] = value; } //add proper checks on index bounds here
}
public static void Main()
{
A obj = new A();
Console.WriteLine(obj[1]); //displays "Text"
obj[2] = "SomeText";
Console.WriteLine(obj[2]); //displays "SomeText"
Console.Read();
}
}

This solution will use object name like an array.
(2) If you want to use a indexed property like
obj.ArrayItem[i] (where ArrayItem is a property using index as a parameter), there is no direct method for it in C#. A workaround is to write a wrapper class around the indexer property. Something like this:
public class A
{
public class ArrayItemClass
{
object[] arr = {123, "Text", 456.55};
public object this[int index]
{
get { return arr[index]; } //add proper checks on index bounds here
set { arr[index] = value; } //add proper checks on index bounds here
}
}

//create an object of class ArrayItem and initialize it in the constructor of class A
ArrayItemClass objArrayItem;
A() { objArrayItem = new ArrayItemClass(); }
//write a readonly property for allowing access to objArrayItem
public ArrayItemClass ArrayItem
{
get { return objArrayItem; }
}
public static void Main()
{
A obj = new A();
Console.WriteLine(obj.ArrayItem[1]); //will display "Text"
obj.ArrayItem[2] = "SomeText";
Console.WriteLine(obj.ArrayItem[2]) ; //will display "SomeText"
Console.Read();
}
}

Hope this helps.
--
Sameeksha,
MCP (.NET)
"Pradeep Kumar" wrote:
I have a scenario where I am writing a property(using get and set) have to return a member of an object stored in an array of objects! The class user has to pass an index based on which a object i's member is returned from the array.

How will the property be declared in C#?

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: kyma via .NET 247 | last post by:
Hi, I haveto use VB to create a form that reads an exisiting XML fileand then allows updates via the VB form. My problem is that I was able to get VB to read a simple XML file(people.XML), but...
11
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on...
0
by: Pradeep Kumar | last post by:
I have a scenario where I am writing a property(using get and set) have to return a member of an object stored in an array of objects! The class user has to pass an index based on which a object i's ...
1
by: Prasad Karunakaran | last post by:
I am using the C# DirectoryEntry class to retrieve the Properties of an user object in the Active Directory. I need to get the First Name and Last Name as properties. I know it is not supported...
6
by: Altman | last post by:
I would like to use an indexed property to access my private array of user controls. If I don't use an indexed property, the property will show up in my Properties Window during development. Is...
14
by: Peter Hallett | last post by:
I would like to set up a string array as a class member, or field, and then populate this array by reading in from a text file, but I cannot find the appropriate syntax. The getter and setter are...
4
by: darrell | last post by:
I recently wrote a program in Access VBA that contains this snippet of code: For i = 1 to TotalBanks With Me("txtBank" & Chr(i + 64)) .BackColor = vbBlue .ForeColor = vbWhite End With Next i...
3
by: Michael Matteson | last post by:
I have two classes. Class A and Class B. I give class A 5 properties int prop1(){} int prop2(){} int prop3(){} int prop4(){} classB prop5(){} what i would like to do is to create a 5th...
17
by: David C. Ullrich | last post by:
Having a hard time phrasing this in the form of a question... The other day I saw a thread where someone asked about overrideable properties and nobody offered the advice that properties are...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.