473,382 Members | 1,445 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,382 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 2454
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.