473,732 Members | 2,204 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2485
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.WriteLi ne(obj[1]); //displays "Text"
obj[2] = "SomeText";
Console.WriteLi ne(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.WriteLi ne(obj.ArrayIte m[1]); //will display "Text"
obj.ArrayItem[2] = "SomeText";
Console.WriteLi ne(obj.ArrayIte m[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
2395
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 I'm having problems figuring out how to get VBto read a more complex XML file (people2.xml) and then useadditional text boxes on the same form to add more familymembers. Each family can have from 1 to 5 members. I've pasted the working code below...
11
6598
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 where the job is running, the job runs sucessfully, PDF files got generated, everything is good. If I scheduled the job to run at the time that I am not logged into the server, Access is not able to print to the printer. The error is pretty...
0
256
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 member is returned from the array. How will the property be declared in C#?
1
5061
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 with the ADSI NT Provider and only supported in the LDAP Provider. So given an UserId (UID) how can I read the First Name and Last Name using LDAP Provider. If anybody can help me with a C# sample code it would of great help. Thanks in advance.
6
1872
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 there a way to get this to show up in the properties window?
14
3130
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 very unhappy with the idea and the compiler refuses to play ball with any of the attempts I have made to build such a structure. There is no problem when using a single string but a two dimensional array of strings appears to be a very different...
4
5283
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 The above code was tested and works.
3
1511
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 property and make its type equal to ClassB. but i would also like to index this property too. BTW Class B
17
2784
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 Bad. So maybe we've got over that. I suppose properties could have Bad consequences if a user doesn't know they exist and think that a certain property of an object is just an ordinary attribute. But that applies to
0
8946
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9307
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9235
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9181
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6735
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.