Link to home
Start Free TrialLog in
Avatar of pclarke7
pclarke7

asked on

retrieving Class object element by index

Hi,
if I have a class such as Test below and I create a new instance Test T = new Test();  How can I now reference object T by index. For instance if I want to place the value of T into a comma delimited string I could do the following

string Str = T.fld1+","+T.fld2+","+T.fld3+","+T.fld4


However I would like to be able to do something like this

for(int x=0;x<=4;x++)
{
Str=T[ x ]+","
}


Is there a way to do this ?

regards
Pat

    public class Test
    {
        public string fld1 { get; set; }
        public string fld2 { get; set; }
        public string fld3 { get; set; }
        public string fld4 { get; set; }
  public Test()
  {
fld1="test1";
fld2="test2";
fld3="test3";
fld4="test4";
  }  
}
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
; )
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pclarke7
pclarke7

ASKER

Thanks to all for your comments. Index operator is exactly what I was looking for and the various implementations of it were great to see. I don't think that there are any design issues here as the data that I am trying to access by index is not related in such a way that i should be placed into an array. I just happen to have a lot of data that can be accessed easier and cleaner by index. I am a big fan of  strongly typed data however there are times when accessing this data is best done by index.

regards
Pat