473,388 Members | 1,326 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,388 software developers and data experts.

Uncommon ways of object access

Dear Group,

I'm curious, whether is possible such trick in C#:

Basically speaking, in PHP what I do, is I assign an object variables
using simple loop:

class MyObject {
private $myObjVar;
private $anotherVar;
public function MyObject($a, $b) { $this->myObjVar = $a; $this-
>anotherVar = $b; };
}

$myObject = new MyObject(123, "hello world!");

foreach($myObject as $oField =$oValue)
{
printf("%s = %s\n", $oField, $oValue);
/* That gives me an output of MyObject like:
myObjVar = 123
anotherVar = hello world!
*/
}

Is such loop possible in C#, anyhow? (Loop, or rather an object access
mode)

The second question is, directly connected with first one: Is it
possible to access an C# Object such way (PHP sample):

$varName = "myObjVar";

echo $myObject->{$varName};
// That will print out "123"

Anyway to do it in C#?

Thank you in advanced for any suggestions.

All the best,
Przemek M. Zawada
Nov 15 '07 #1
5 1309
On 2007-11-14 23:17:00 -0800, "Przemek M. Zawada"
<pr************@gmail.comsaid:
[...]
foreach($myObject as $oField =$oValue)
{
printf("%s = %s\n", $oField, $oValue);
/* That gives me an output of MyObject like:
myObjVar = 123
anotherVar = hello world!
*/
}

Is such loop possible in C#, anyhow? (Loop, or rather an object access
mode)

The second question is, directly connected with first one: Is it
possible to access an C# Object such way (PHP sample):

$varName = "myObjVar";

echo $myObject->{$varName};
// That will print out "123"

Anyway to do it in C#?
Both of your questions can be answered in C# using reflection. You'll
want to look at things like the Type class, Type.GetFields() or
Type.GetProperties() (depending on what exactly you're trying to
extract from the type), and the FieldInfo and/or PropertyInfo classes
(again, depending).

Pete

Nov 15 '07 #2
On 15 Lis, 00:42, Peter Duniho <NpOeStPe...@NnOwSlPiAnMk.comwrote:
On 2007-11-14 23:17:00 -0800, "Przemek M. Zawada"
<przemek.zaw...@gmail.comsaid:
[...]
foreach($myObject as $oField =$oValue)
{
printf("%s = %s\n", $oField, $oValue);
/* That gives me an output of MyObject like:
myObjVar = 123
anotherVar = hello world!
*/
}
Is such loop possible in C#, anyhow? (Loop, or rather an object access
mode)
The second question is, directly connected with first one: Is it
possible to access an C# Object such way (PHP sample):
$varName = "myObjVar";
echo $myObject->{$varName};
// That will print out "123"
Anyway to do it in C#?

Both of your questions can be answered in C# using reflection. You'll
want to look at things like the Type class, Type.GetFields() or
Type.GetProperties() (depending on what exactly you're trying to
extract from the type), and the FieldInfo and/or PropertyInfo classes
(again, depending).

Pete
Thank you Pete for such fast answer.
That's correct, I receive through GetProperties() PropertyInfo object,
which gives me object details. Another case selecting data from object
I've fetched properties?

CustomerObject c = new CustomerObject();
foreach(PropertyInfo pi in c.GetType().GetProperties())
{
string varName = pi.Name;
c.{varName} = "my newValue"; // <<<
Console.WriteLine(c.{varName}.ToString()); // <<<
}

How shall I write those '<<<' lines correctly? If it's possible of
course..

All the best,
Przemek
Nov 15 '07 #3
On Nov 15, 8:23 am, "Przemek M. Zawada" <przemek.zaw...@gmail.com>
wrote:
Thank you Pete for such fast answer.
That's correct, I receive through GetProperties() PropertyInfo object,
which gives me object details. Another case selecting data from object
I've fetched properties?

CustomerObject c = new CustomerObject();
foreach(PropertyInfo pi in c.GetType().GetProperties())
{
string varName = pi.Name;
c.{varName} = "my newValue"; // <<<
Console.WriteLine(c.{varName}.ToString()); // <<<

}

How shall I write those '<<<' lines correctly? If it's possible of
course..
pi.SetValue(c, "my newValue", null);
Console.WriteLine (pi.GetValue(c, null));

Jon
Nov 15 '07 #4
On 15 Lis, 09:27, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Nov 15, 8:23 am, "Przemek M. Zawada" <przemek.zaw...@gmail.com>
wrote:
Thank you Pete for such fast answer.
That's correct, I receive through GetProperties() PropertyInfo object,
which gives me object details. Another case selecting data from object
I've fetched properties?
CustomerObject c = new CustomerObject();
foreach(PropertyInfo pi in c.GetType().GetProperties())
{
string varName = pi.Name;
c.{varName} = "my newValue"; // <<<
Console.WriteLine(c.{varName}.ToString()); // <<<
}
How shall I write those '<<<' lines correctly? If it's possible of
course..

pi.SetValue(c, "my newValue", null);
Console.WriteLine (pi.GetValue(c, null));

Jon
THANK YOU, that's the way I like it!

All the best,
Przemek
Nov 15 '07 #5
On 2007-11-15 01:02:05 -0800, "Przemek M. Zawada"
<pr************@gmail.comsaid:
THANK YOU, that's the way I like it!
Uh-huh, uh-huh.

(with apologies to KC and the Sunshine Band :) )

Glad we could help. :)

Nov 15 '07 #6

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

Similar topics

22
by: Peter Ammon | last post by:
A friend was asked "How many ways are there to create an object in C++?" in an interview. Apparently, the "right" answer was eleven. Does anyone know how the interviewer arrived at that number? ...
2
by: bobsled | last post by:
What're good ways to pass a pointer or reference of "client" though pureAbstractBaseClass() so that both derive1 and derive2 can access client::clientMethod()? Thanks for your comments! class...
29
by: Lauren Wilson | last post by:
Does anyone know how the following info is extracted from the user's computer by a Front Page form? HTTP User Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041107...
7
by: J-T | last post by:
I can instantiate my object in my *ASP.NET* application in two ways: A) public sealed class RSSingleton { private static ReportingServiceProxy m_RsProxy=null; static RSSingleton() {...
13
by: frk.won | last post by:
I am interested in learning how to use the VS 2005 code snippets. However, I wish to know what are the best ways to source control the code snippets? Are there any source safe/subversion...
2
by: Vadim Tropashko | last post by:
http://vadimtropashko.wordpress.com/why-relational-division-is-so-uncommon/
2
by: ksamir2004 | last post by:
How to compare two files in java & uncommon text should print in text file.. thanks Samir
2
by: mani85 | last post by:
HI, I have two tables.I want to retrieve the uncommon records from two tables. I m using the queries : select col1,col2 from table 1 where rtrim(col1+'-'+col2) not in (select ...
1
by: Just D | last post by:
All, Returning to WinForms - what is the current situation with the MS SQL Server database access? Did anybody see any good review or can just give us his own impression about that? I'd like to...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.