c# - Use of indexers on properties in a customer class -



c# - Use of indexers on properties in a customer class -

been google.ing time , nil seems match specific issue.

i have created own class properties such as

public class cperson { public int? iid {get;set;} public string sname { get; set; } public bool? bgoodguy { get; set; } }

then create instance of class

cperson myperson = new cperson()

and add together values

myperson.iid=10; myperson.sname="john smith"; myperson.bgoodguy=true;

and if want display person do

writeline("persons id : " + myperson.iid); writeline("persons name : " + myperson.sname); writeline("person : " + myperson.bgoodguy);

but write out based on order property defined in class so

writeline("persons id : " + myperson[0]); writeline("persons name : " + myperson[1]); writeline("person : " + myperson[2]);

and not working.

i assume doable indexer of sort, samples have found indexing several persons, such as:

writeline("person 0's id : " + myperson[0].iid); writeline("person 0's name : " + myperson[0].sname); writeline("person 0's or bad status : " + myperson[0].bgoodguy);

but not im after.

some 1 (c#) sharp plenty give me direction much appreciate it.

regards

a swede

firstly, seems bad idea. if find needing this, should consider design alternatives.

secondly, it's not clear can properties in declaration order - consider more clear-cut ordering, such alphabetical.

if really want this, can add together indexer this:

public object this[int index] { { // alternative: remove hard-coding, , fetch properties // via reflection. switch(index) { // note: property names changed conform .net conventions case 0: homecoming id; case 1: homecoming name; case 2: homecoming goodguy; default: throw new argumentoutofrangeexception("index"); } } }

... say, wouldn't this.

an alternative have properties property or method created ienumerable<object>, perchance via reflection. example:

public ienumerable<object> properties() { homecoming typeof(person).getproperties() .orderby(p => p.name) .select(p => p.getvalue(this, null)); }

you use:

console.writeline("persons id : " + myperson.properties().elementat(0));

further, if really wanted to, create extension method on any object. again, i'd wary of doing of though.

c# properties indexer

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -