Subject: [Q] How do you dynamically reference the filed in
Posted By: CyanBlue Post Date: 9/1/2006 6:51:04 PM
Howdy...  :)

How do you dynamically reference the field in C#???

Let's say that I have an object, _obj, that has name, address, phone fields...

If I have a function that gets the field that is given as a parameter, how do I reference it in C#???  I normally do that by using a bracket in ActionScript but I have no idea how to do that in C#...

public string getField(string _field)
{
//   return _obje.name; // This would be how I access the name field...
   return _obj[_field];
}

Thank you...

CyanBlue

CyanBlue / Jason Je / Flash Developer
http://CyanBlue.FlashVacuum.com
http://www.FlashVacuum.com
Reply By: dparsons Reply Date: 9/1/2006 7:23:42 PM
Cyan,
   Well that all depends.

If you wanted to be able to access the information as _obj.name you would have to create a custom class that had get[] set[] properties defined and then you could call _obj.name, which would return you the value of name.

If you are trying to access n parameter of x object then you are thinking of _obj as a collection and would need to implement the IEnumerator (Or IEnumerable depeneding on your need) into a custom class; this will allow you to access n value of _obj using [ ].  

The datarow object implements IEnumerator and you can access the values in a datarow object by either:

dr["columnName"] or dr[0]

Just assume that if 'columnName' is column 0 then those 2 statements point at the same data element.

hth.

"The one language all programmers understand is profanity."
Reply By: CyanBlue Reply Date: 9/1/2006 7:45:20 PM
Thanks for the quick reply, dparsons...  :)

It sounds like a very easy question, yet the answers are too hard for my ActionScript brain to understand...

I think getter/setter method makes much more sense if I were to design the whole class but I am just accessing whatever I was given... (and this is my first C# project)
So, in that sense IEnumerator makes better sense cuz I won't be able to add getter/setter methods...

I was merely thinking that there is going to be easier way to handle this but it might take whole lot more time if I were to pursue that route since there are only four elements that I can access anyways...  So, I'll just create four methods like getName, getAddress and so on just to finish this one...  But I sure will keep what you have suggested in mind for next time...

Appreciate your help...   ;)

CyanBlue / Jason Je / Flash Developer
http://CyanBlue.FlashVacuum.com
http://www.FlashVacuum.com
Reply By: dparsons Reply Date: 9/1/2006 10:17:47 PM
No problem.  It may be worthwhile to see if the base class implements the interface though =]

"The one language all programmers understand is profanity."
Reply By: CyanBlue Reply Date: 9/1/2006 10:28:41 PM
Um...  What do you exactly mean by the interface???

CyanBlue / Jason Je / Flash Developer
http://CyanBlue.FlashVacuum.com
http://www.FlashVacuum.com
Reply By: dparsons Reply Date: 9/2/2006 8:47:27 AM
I could sit here and explain what an interface is for the next hour! ;]  Basically, think of an interface as a definition of methods, functions, and get and set statements (no program logic, no variables) and when another case implements X interface it MUST implement the functions and methods defined in the interface.

If my interface had

interface MyInterface{
interger foo();
void fooBar();
}
and then


public class Something: IMyInterface

my class Something must have function called foo and a method in it called fooBar or I will get a compiler error saying that the interface is not implemented correctly.

So what I mean is that if the object you are working with has implemented the IEnumerator interface it will have certain methods and functions you should be able to call (such as MoveNext)

hth

"The one language all programmers understand is profanity."
Reply By: CyanBlue Reply Date: 9/2/2006 11:24:07 AM
I'm glad that you have not spent one hour to explain that...  Your explanation reminded me of something that I have read long time ago and I think I know what you mean by the interface...

Much appreciate your help...



CyanBlue / Jason Je / Flash Developer
http://CyanBlue.FlashVacuum.com
http://www.FlashVacuum.com

Go to topic 49110

Return to index page 186
Return to index page 185
Return to index page 184
Return to index page 183
Return to index page 182
Return to index page 181
Return to index page 180
Return to index page 179
Return to index page 178
Return to index page 177