473,491 Members | 2,636 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

is this possible? an indexed property to have sub-properties?

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
also has some properties to it. So in the end i would be able to access my
items like this:

classA xyz = new classA();
xyz.prop1=1;
xyz.prop2=2;
xyz.prop3=3;
xyz.prop4=4;

xyz.prop5[0].prop1=1;
xyz.prop5[0].prop2=2;

xyz.prop5[1].prop1=1;
xyz.prop5[1].prop2=2;

I remember doing stuff like this a while ago in VB6 but i'm pretty new to C#
and i'm just learning out to do this sort of stuff. I hope that explain
helps a little bit more.
Oct 19 '06 #1
3 1494

Michael Matteson wrote:
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
also has some properties to it. So in the end i would be able to access my
items like this:

classA xyz = new classA();
xyz.prop1=1;
xyz.prop2=2;
xyz.prop3=3;
xyz.prop4=4;

xyz.prop5[0].prop1=1;
xyz.prop5[0].prop2=2;

xyz.prop5[1].prop1=1;
xyz.prop5[1].prop2=2;

I remember doing stuff like this a while ago in VB6 but i'm pretty new to C#
and i'm just learning out to do this sort of stuff. I hope that explain
helps a little bit more.
I'm assuming from your examples that ClassB is some sort of aggregate
for which indexing makes sense.

Just implement an indexer for ClassB (within class B) and you'll be
able to do what you indicated.

http://msdn2.microsoft.com/en-us/library/2549tw02.aspx

Oct 19 '06 #2
yea my real world situation is this. i'm receiving netflow packets from a
cisco device and storing them in a class that represents the packet layout.
i call this p.

p.header property is an instantiantion on my header class that has
properties so i can store my packet header data
p.header.version
p.header.count
p.header.sysuptime
etc.

that part is fine. the next part is to store template flows.
p.templaterecord.id=1
p.templaterecord.fsid=255
p.templaterecord.length=182
but there can be many different templates included in a packet and since i
don't know how many i just wanted to create an array (indexed property) to
store my templaterecord data. so later on i can just for/each through it and
write the data to a database. i'll give what you said a try. thanks bruce!

"Bruce Wood" <br*******@canada.comwrote in message
news:11**********************@m7g2000cwm.googlegro ups.com...
>
Michael Matteson wrote:
>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
also has some properties to it. So in the end i would be able to access
my
items like this:

classA xyz = new classA();
xyz.prop1=1;
xyz.prop2=2;
xyz.prop3=3;
xyz.prop4=4;

xyz.prop5[0].prop1=1;
xyz.prop5[0].prop2=2;

xyz.prop5[1].prop1=1;
xyz.prop5[1].prop2=2;

I remember doing stuff like this a while ago in VB6 but i'm pretty new to
C#
and i'm just learning out to do this sort of stuff. I hope that explain
helps a little bit more.

I'm assuming from your examples that ClassB is some sort of aggregate
for which indexing makes sense.

Just implement an indexer for ClassB (within class B) and you'll be
able to do what you indicated.

http://msdn2.microsoft.com/en-us/library/2549tw02.aspx

Oct 19 '06 #3
A word of caution about returning aggregates as properties.

There are two simple-minded ways to return aggregates as properties,
both of which can result in problems. The "right" way to do it is more
complex. I put "right" in quotation marks because, of course, nothing
is right in all situations.

Most people start out making aggregate properties doing something like
this:

public class ClassA
{
private string[] _stuff;

public string[] Stuff { get { return this._stuff; } }
}

Seems simple enough. The only problem is that because arrays are passed
(and returned) by reference, this code just gave its callers the power
to modify the object's internal state without any controls. So, for
example, a caller could do this:

ClassA a = new ClassA();
a.Stuff[15] = "Hello world";

While the caller cannot replace the entire array (because Stuff has no
setter), a caller _can_ modify the _contents_ of the array, and perhaps
that's not what the class intended (many classes want to export a
read-only aggregate).

So, the programmer gets trickier:

public class ClassA
{
private string[] _stuff;

public string[] Stuff { get { return (string[])this._stuff.Clone();
} }
}

Now the caller just gets a copy and can't change the ClassA object's
internal state without ClassA knowing about it. This, however, can lead
to efficiency problems when callers (perfectly reasonably) do things
like this:

ClassA a = new ClassA();
for (int i = 0; i < a.Stuff.Length; i++)
{
string s = a.Stuff[i];
...
}

because here the Stuff array is cloned at least once (and perhaps
twice) for each time around the loop. Nasty.

The "correct" solution is along the lines of what you posted: create a
new class to represent the aggregate structure, and have _that class_
decide what the caller may or may not do with the aggregate.

Look, for example, at how ListViewItemCollection is implemented as a
model.

Michael Matteson wrote:
yea my real world situation is this. i'm receiving netflow packets from a
cisco device and storing them in a class that represents the packet layout.
i call this p.

p.header property is an instantiantion on my header class that has
properties so i can store my packet header data
p.header.version
p.header.count
p.header.sysuptime
etc.

that part is fine. the next part is to store template flows.
p.templaterecord.id=1
p.templaterecord.fsid=255
p.templaterecord.length=182
but there can be many different templates included in a packet and since i
don't know how many i just wanted to create an array (indexed property) to
store my templaterecord data. so later on i can just for/each through it and
write the data to a database. i'll give what you said a try. thanks bruce!

"Bruce Wood" <br*******@canada.comwrote in message
news:11**********************@m7g2000cwm.googlegro ups.com...

Michael Matteson wrote:
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
also has some properties to it. So in the end i would be able to access
my
items like this:

classA xyz = new classA();
xyz.prop1=1;
xyz.prop2=2;
xyz.prop3=3;
xyz.prop4=4;

xyz.prop5[0].prop1=1;
xyz.prop5[0].prop2=2;

xyz.prop5[1].prop1=1;
xyz.prop5[1].prop2=2;

I remember doing stuff like this a while ago in VB6 but i'm pretty new to
C#
and i'm just learning out to do this sort of stuff. I hope that explain
helps a little bit more.
I'm assuming from your examples that ClassB is some sort of aggregate
for which indexing makes sense.

Just implement an indexer for ClassB (within class B) and you'll be
able to do what you indicated.

http://msdn2.microsoft.com/en-us/library/2549tw02.aspx
Oct 19 '06 #4

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

Similar topics

11
2516
by: Craig Keightley | last post by:
I have a mysql database with a list of companies who supply specific products tblSuppliers (simplified) sID | sName | goodsRefs 1 | comp name | 1,2,3,4,5 2 | company 2 | 2,4
4
2580
by: dixie | last post by:
I have a table called "tblParticipants" with a field called "ID", which must be indexed with No duplicates allowed. I am looking for a way of doing in vba a small if ..then ..else .. routine that...
7
2002
by: Ioannis Vranos | last post by:
Fellows there is probably a serious implementation bug of C++/CLI indexed property in VC++ 2005, it looks like it is implemented the opposite way than the C++/CLI draft says! At first the...
1
2509
by: Bob of the West | last post by:
Hi, I'm trying to something quite straightforward, populate a datagrid with certain fields after selecting from a dropdownlist. The books I have don't seem to give an example of how to do this (...
4
1657
by: TJ | last post by:
Hi, There is one aspx web page that contains usercontrol. In aspx page and usercontrol , there is each submit button... Here is what I want... I want to process something depending on each...
2
976
by: mattie | last post by:
hey all, can you code against the event log, for instance if an entry for a specific user shows up can i popup a message box notifying me that? thanks, mj
1
1114
by: Nick C | last post by:
Hi I am trying to sort my gridview ? What is the best way to do this? th Nick <%@ Page Language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
1
1791
by: KJ | last post by:
Hello all, I have several objects which use indexers, such as (in C#): public string this { get { return BaseEntity; }
25
3886
by: Rick Collard | last post by:
Using DAO 3.6 on an Access 2002 database, I'm getting unexpected results with the FindFirst method. Here's the simple code to test: Public Sub FindIt() Dim db As Database, rs As Recordset...
9
2162
by: magickarle | last post by:
Hi, I have a database in access with the following columns: Day AgentID ManagerID Grade They got information about agents and some grades. I would like to have ONE form with several...
0
7112
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
6974
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
7183
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...
1
6852
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
7356
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
4573
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...
0
3084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
628
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
277
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...

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.