Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 1st, 2006, 06:51 PM
Registered User
 
Join Date: Apr 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default [Q] How do you dynamically reference the filed in

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
 
Old September 1st, 2006, 07:23 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

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."
 
Old September 1st, 2006, 07:45 PM
Registered User
 
Join Date: Apr 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
 
Old September 1st, 2006, 10:17 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

No problem. It may be worthwhile to see if the base class implements the interface though =]

"The one language all programmers understand is profanity."
 
Old September 1st, 2006, 10:28 PM
Registered User
 
Join Date: Apr 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Um... What do you exactly mean by the interface???

CyanBlue / Jason Je / Flash Developer
http://CyanBlue.FlashVacuum.com
http://www.FlashVacuum.com
 
Old September 2nd, 2006, 08:47 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

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."
 
Old September 2nd, 2006, 11:24 AM
Registered User
 
Join Date: Apr 2006
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
HTML supported text filed. dpkbahuguna ASP.NET 3.5 Basics 1 November 12th, 2008 04:31 PM
boolean filed in crystal report saeed_sp BOOK: Professional Crystal Reports for VS.NET 0 August 2nd, 2008 11:58 PM
By value, By reference watashi C++ Programming 1 October 10th, 2007 11:04 PM
Gridview - Place cursor inside filed Sindbad Visual Studio 2005 0 September 21st, 2007 09:24 AM
in need of reference! alialibidad SQL Language 1 June 4th, 2006 05:14 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.