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

How can I create -- CustomClass[propertyName].CustomMethod ?

I can't figure out how to approach this problem.
I would like to have a custom object that will contain some predefined
attributes.

The simple solution that I know is to create my own class -- something like:

public class MyClass
{
private string _property01 = string.Empty;

public string property01
{
get { return _property01; }
set { this._property01 = value; }
}
}

And use all hard-coded assignments like:

MyClass someRecord = new MyClass();
someRecord.Property01 = "blah blah";
string anotherBlah = someRecord.Property01;

What I am looking for is to generalize properties of my custom class as well
as generalize future population of those properties.

I think that I need a construct like this:
{
....
propertyName = "property01"

someRecord[propertyName].Value = "blah blah"
string anotherBlah = someRecord[propertyName].Value;
....
}
and perhaps extending that class with more methods like:

someRecord[propertyName].Remove;
someRecord[propertyName].Add;
someRecord[propertyName].Whatever;

It is seems to me that I will need to inherit from CollectionBase and use
some sort of constructor like:

public MyClass this[int index]
{
get { return ((MyClass)(List[index])); }
set { List[index] = value; }
}

Should I create some sort of a "special" constructor for my "MyClass" class?

With the one I've got I can return value by integer "index" which is a
sollectionBase IList method, I gathering... Which i could not make to work
anyway...

How can I supply my own "search" by name of the attribute, and not by its
index?
Should I extend constructor? Should I inherit from something else? Or
perhaps I am completely off-track with this idea.

Please advice.

Thanks in advance.
Oct 14 '06 #1
2 1269
Well, in terms of implementation, you would be better of with a
hashtable. In 2.0, you could use

privatate readonly Dictionary<string, objectpropertyBag = new
Dictionary<string, object>();

public object this[string propertyName] {
get {// returns null if not defined
object value;
propertyBag.TryGetValue(propertyName, out value);
return value;
}
set {
propertyBag[propertyName] = value;
}
}

However, this isn't type safe, doesn't (as is) support notifications,
and doesn't show in the component model (e.g. for use in tables).

Doing this /properly/ is not a trivial task, but can be done. Lots of
code, though, and you need a pretty good undertanding of the component
model. I would recommend keeping it simple (as above) unless you know
you need the extra functionality (e.g. for binding the virtual
properties directly to winforms in the standard way, or supporting a
complex diffgram model).

I have an architecture that does all this (broadly comparable to a
DataSet, but entirely "normal class" based), but it is much, much code.
I might post it somewhere when complete... but way too long for an
ng...

Marc

Oct 14 '06 #2
Note: to support methods on each, the item in the dictionary would be a
class with a property for the value; at the simplest (using fields for
brevity; use properties IRL):

public class PropertyBagValue {
public object Value;
public void Whatever() {}
}

I think the Remove and Add belong naturally to the parent; this
approach also allows you to start looking at change notifications (with
a reference to the parent), but not completely.

Search: depends how you want it to behave! Since we are talking about a
single instance (not a collection of instances) I'm not sure what you'd
want to search *on*, but if you did have e.g. a List<Ta predicate
would be trivial:

list.FindAll(delegate (MyClass item) {
return item["SomeProperty"] == 5; // using original code, not .Value
});

Finally - CollectionBase is wrong for the *outer* class here; it is a
single item, not a collection of those items. It can encapsulate a
collection of its properties (as a private implementation detail), but
that doesn't /make/ it a collection in the normal sense.

Marc

Oct 14 '06 #3

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

Similar topics

2
by: §iD` | last post by:
Hi! I would like to create a virtual folder (which I want to mount) and populate managing his content by a DLL or something like that in VB.NET (2.0). How can I acomplish this? Thanks to...
13
by: andrea | last post by:
Sorry for the stupid question, I know, but sometimes is necessary starts from the basic. I don't know how to pass the result of a method generated from a DAL class to a BL class returning the...
5
by: hfk0 | last post by:
Hi, I'm new to ASP.net, SQL Server and visual studio.net, and I'm having problem inserting and storing data from a web form to a SQL database. I created a simple ASP.NET web form, a simple SQL...
2
by: Luis Arvayo | last post by:
Hi, In c#, I need to dynamically create types at runtime that will consist of the following: - inherits from a given interface - will have a constructor with an int argument
5
by: none | last post by:
I'd like to create a new static property in a class "hiding" the property present in a base class. Since this needs to happen at runtime I tried doing this via DynamicMethod. But obviously the...
2
by: Ralph | last post by:
Hi I don't understand why it's not working: function schedule(imTop){ this.tdImagesTop = imTop; } schedule.prototype.selectEl = function() { alert(this.tdImagesTop);
11
by: Andrus | last post by:
I'm implementing entity object which should populate its properties from database when property is first referenced. In RDL reports I use object properties like MyObject.MyProperty MyObject...
0
by: Brian Roisentul | last post by:
Hi All, I want to programmatically(c# fmw 2.0) create a folder under my IIS Web Site. I figured that I needed to create an entry in the IIS Metabase. So, after investigating a while and i...
17
by: nickvans | last post by:
Hi everyone, Thanks in advance for trying to help me out. I have a SQLDataSource which is running a dynamically generated SQL2005 stored procedure. (That is, the stored procedure creates a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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:
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...

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.