473,406 Members | 2,843 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,406 software developers and data experts.

Indexed Properties (indexers) and reflection (long book follows)

I have an Invoice class that must know, directly or indirectly, how to
do anything associated with creating, reading, or otherwise processing
an invoice to a customer. It is an uber-DAO object that combines the
business logic and DAO layers. The invoice it handles can have one to
many lines. The most logical implementation is an Invoice object with
an array of InvoiceLine objects. We of course want to prevent Joe
Programmer from directly manipulating the array. In our case Joe should
be able to get a reference to an object in the array to access *its*
members (item ID, quantity, unit price), but should not be able to set
an invoice line index to a new object reference. So, we need to hide it
behind a property accessor.
>From what I understand, doing the following in C# will not work:
//within Invoice class declaration

private InvoiceLine[] _lines;

public InvoiceLine Line[int index]
{
get{
//TODO: create a new object and append it to the array if the
index is beyond the upper bound
return _lines[index];
}
}

Instead, as I understand it, the array will be wrapped in an indexer
object. I probably won't be dealing with interfaces; for one,
implementing IList AFAIK requires me to implement the Add() function,
which I don't want Joe Programmer to have access to. So the above code
would instead be replaced with something like:

private class LineIndexer
{
private InvoiceLine[] _lines;

public LineIndexer this[int index]
{
get{
//TODO: create a new object instance and append if the index
is out-of-bounds
return _lines[index];
}
}
}

//within Invoice class declaration

private LineIndexer _lines;

public LineIndexer Line
{
get{
return _lines;
}
}

Joe Programmer could then code a static line to set a property, similar
to:

myInvoice.Line[1].ItemID.Value = "My New Item 001";

(footnote: ItemID is itself a read-only property accessor for a nested
object, allowing us to encapsulate field information used for
validation and display, like type, length and whether the field is
required)

So, having figured out all that, the question is, how does this affect
being able to dynamically parse through a property identifier like the
one above when using reflection? To work with a property four or five
levels down, I start with a string identifying the "fully qualified"
property in dot notation. I then parse this string dot by dot to get
each member, which is always a property, and get the value of that
property which gives me the next class reference, and repeat. However,
to the best of my knowledge when I get to "Line[1]", I will not get an
InvoiceLine object by setting up a PropertyInfo object for Line and
passing an index as the parameter.

Or will I?

I can't test this right now, but I am thinking that, if I parse out an
index (the code already looks for [x] in the identifier string and
returns x as int), I'll need to know that there's an extra step: get
the LineIndexer reference, *then* get the actual line object from
LineIndexer by using the index I parsed out.

This method needs to be as elegant and fast as possible, it needs to
work flawlessly with reflection techniques (the algorithm doing all the
parsing can know in general how our business objects are put together,
but it cannot know at compile time which business object type it will
be working with), it needs to be able to deal with multiple indexed
properties in a chain (an InvoiceLine would for instance implement an
array of InvoiceLineJob objects that show which project a contractor
spent the listed time and materiel on), and future generations need to
understand what the flying flip I'm doing (assume I'm commenting the
code like a tutorial). It doesn't, at this time, have to work with
multi-dimensional arrays. I heard that the concept of indexed
properties is more transparently implemented in VB.Net, but we're
trying to avoid unnecessary complexity, and programming different
objects in different languages does just that.

Aug 21 '06 #1
0 2013

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

Similar topics

2
by: Kor | last post by:
Hi, Am I right that indexed properties are implemented in VB ..NET, but NOT in C#. It seems that for C# you have to wrap the property in a separate class, for emulating index properties. In...
10
by: Sunny | last post by:
Hi, I have an old problem which I couldn't solve so far. Now I have found a post in that group that gave me an idea, but I can not fully understand it. The problem is: I'm trying to use a...
8
by: Russell Bearden | last post by:
Why are there no indexed properties in C#? For example: class blah { public Node Child { get {
2
by: Daniel Miller | last post by:
What is the difference between indexers and indexed properties (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vcwlkindexedpropertiestutorial.asp)? I don't see what...
3
by: Fin | last post by:
Index properties in C++ class libraries (.NET) apper as set_ and get_ methods when used in C# To test this out, I changed the example from section "13.2 Indexed Properties" in MSDN, and placed the...
4
by: tg.foobar | last post by:
i'd like to do the following, but i don't think it's possible. can you help me find a way to do this, or maybe a better way to write the code? I have a list of items that need to be modified...
6
by: | last post by:
I have made some user controls with custom properties. I can set those properties on instances of my user controls, and I have programmed my user control to do useful visual things in response to...
7
by: Tom Dacon | last post by:
I'm using Reflection to iterate over the properties and fields of an arbitrary object. For non-indexed properties it's pi.GetValue(theObject, Nothing) for VB, or pi.GetValue(theObject, null) for...
0
by: saudamini | last post by:
i am trying to deserlize XML against the class which has this an indexed property, the deserializer just skips this indexed property, it's not setting it. I remember reading somewhere XML...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
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
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,...
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
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...
0
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,...

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.