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

member-name as string

Hi!

I am looking for a way to get the name of a class-member as string, as
example:

class A
{
public string a;
}

[...]

string MemberName = GiveMeTheNameOf(A.a);

What I want is: MemberName == "A.a"

Is there any way to do that?

Thank you!
Ruben

Nov 7 '06 #1
8 2087
I am looking for a way to get the name of a class-member as string, as
example:

class A
{
public string a;
}
[...]

string MemberName = GiveMeTheNameOf(A.a);

What I want is: MemberName == "A.a"

Is there any way to do that?
What exactly are you trying to achieve with this? If you are able to pass
the member into a method, you obviously know its name. Could you explain
the circumstances a little more clearly?

Best Regards,
Dustin Campbell
Developer Express Inc.
Nov 7 '06 #2
The name of the class member is pointless in the way you are describing it.

If you want to be able to access the member, based on string name, that is
different.

Such as Get(A, "a"). That requires reflection.

"Ruben" wrote:
Hi!

I am looking for a way to get the name of a class-member as string, as
example:

class A
{
public string a;
}

[...]

string MemberName = GiveMeTheNameOf(A.a);

What I want is: MemberName == "A.a"

Is there any way to do that?

Thank you!
Ruben

Nov 7 '06 #3
I need the name of a member in a string to use this string for a
SQL-database-query.
It would be possible to hardcode this string but this would make the
code very unflexible.

I hope this explains my post...

Thank you!
Ruben

Nov 7 '06 #4
Ruben,

What you're looking for are te "Reflection" classes in System.Reflection. I
adapted the sample from the MSDN library with your "A" class:

using System;
using System.Reflection;

public class FieldInfoClass
{
// the class you're interested in:
public class TestClass
{
public string a;
}

// the sample from MSDN, modified to use the test class
public static void Main()
{
FieldInfo[] myFieldInfo;
TestClass A = new TestClass();

Type myType = A.GetType();

// Get the type and fields of TestClass
myFieldInfo = myType.GetFields(BindingFlags.NonPublic |
BindingFlags.Instance
| BindingFlags.Public);

Console.WriteLine("\nThe fields of " +
"FieldInfoClass are \n");
// Display the field information of FieldInfoClass.
for(int i = 0; i < myFieldInfo.Length; i++)
{
Console.WriteLine("\nName : {0}",
myFieldInfo[i].Name);
Console.WriteLine("Declaring Type : {0}",
myFieldInfo[i].DeclaringType);
Console.WriteLine("IsPublic : {0}",
myFieldInfo[i].IsPublic);
Console.WriteLine("MemberType : {0}",
myFieldInfo[i].MemberType);
Console.WriteLine("FieldType : {0}",
myFieldInfo[i].FieldType);
Console.WriteLine("IsFamily : {0}",
myFieldInfo[i].IsFamily);
}
}
}
"Ruben" <Mr****@gmx.dewrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
Hi!

I am looking for a way to get the name of a class-member as string, as
example:

class A
{
public string a;
}

[...]

string MemberName = GiveMeTheNameOf(A.a);

What I want is: MemberName == "A.a"

Is there any way to do that?

Thank you!
Ruben

Nov 7 '06 #5
Ruben wrote:
I need the name of a member in a string to use this string for a
SQL-database-query.
It would be possible to hardcode this string but this would make the
code very unflexible.

I hope this explains my post...

Thank you!
Ruben
Ruben,

Are you not hard-coding it when you're passing in A.a?

But, by the by, no you can't do it like this. You can either enumerate
across the fields in a class, or you can request the field info, given a
string representation of the name. You cannot get a string representation
of the name, giving a fully qualified reference to the field, since a
fully-qualified reference evaluates to the value of that field.

--
Hope this helps,
Tom Spink

Google first, ask later.
Nov 7 '06 #6
andrew queisser schrieb:
What you're looking for are te "Reflection" classes in System.Reflection. I
adapted the sample from the MSDN library with your "A" class:
[...]

Ok, I see it is possible to traverse threw all members of a class.
But this is not enough for my purpose, is there a way to get explicitly
the name of a certain member?

But since extensive googling did not bring up something nice, I fear
there is no solution for my problem...

Thank you all!
Ruben

Nov 7 '06 #7
"Ruben" <Mr****@gmx.dewrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
andrew queisser schrieb:
>What you're looking for are te "Reflection" classes in System.Reflection.
I
adapted the sample from the MSDN library with your "A" class:
[...]

Ok, I see it is possible to traverse threw all members of a class.
But this is not enough for my purpose, is there a way to get explicitly
the name of a certain member?

But since extensive googling did not bring up something nice, I fear
there is no solution for my problem...

Thank you all!
Ruben
Ah, I see. One bizarre und unsavory way of achieving that would be to stash
the old value of your member, set it to some recognizable test value, then
iterate through all fields in the reflection info of the class, call
"GetValue" and then see which one it is. At that point you know which member
it is and extract the string. To be safe you'd probably have to try at least
two test values to avoid finding a field that happens to already have your
test value.

Andrew


Nov 7 '06 #8

Ruben wrote:
andrew queisser schrieb:
What you're looking for are te "Reflection" classes in System.Reflection. I
adapted the sample from the MSDN library with your "A" class:
[...]

Ok, I see it is possible to traverse threw all members of a class.
But this is not enough for my purpose, is there a way to get explicitly
the name of a certain member?

But since extensive googling did not bring up something nice, I fear
there is no solution for my problem...
It seems that what you're trying to write is something that I believe
is called an OPF, an Object Persistence Framework, or an ORM, an
object-relational mapping. In simpler terms, a standard way to store
object information into a relational database and get it back again
later.

You should probably read up on OPFs and ORMs. I'm sure that there are
lots of clever people out there who have written up how to build these
things, and their designs are better than anything your or I could
produce. Perhaps you should take a step back and see if there is an
established solution to the larger problem you're facing.

Nov 7 '06 #9

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

Similar topics

2
by: Wenjie | last post by:
Hello, I read someone posted assertions that even the (public) member function is not static, there are probably only one copy of the code in the executable. Then except the...
5
by: Newsgroup - Ann | last post by:
Gurus, I have the following implementation of a member function: class A { // ... virtual double func(double v); void caller(int i, int j, double (* callee)(double)); void foo() {caller(1,...
4
by: Jian H. Li | last post by:
Hello, What's the essential differences between the two ways of "class::member" & "object.member"(or object_pointer->member)? class C{ public: void f() {} int i; };
8
by: Scott J. McCaughrin | last post by:
The following program compiles fine but elicits this message from the linker: "undefined reference to VarArray::funct" and thus fails. It seems to behave as if the static data-member:...
6
by: Dan Huantes | last post by:
I was presented a problem today where a class had member variable that was an object of a templated class. The class wanted to instantiate the object as a private member variable and call a...
40
by: Steve Rencontre | last post by:
I can't for the life of me see how to do pointer-to-member when the member is actually part of an embedded structure. That is, if I have: struct S1 { int a; }; struct S2 { S1 s; int b; }; ...
1
by: mangalalei | last post by:
A static data member can be of the same class type as that of which it is a member. A nonstatic data member is restricted to being declared as a pointer or a reference to an object of its class. ...
7
by: Valeriu Catina | last post by:
Hi, consider the Shape class from the FAQ: class Shape{ public: Shape(); virtual ~Shape(); virtual void draw() = 0;
7
by: WaterWalk | last post by:
Hello. I thought I understood member function pointers, but in fact I don't. Consider the following example: class Base { public: virtual ~Base() {} }; class Derived : public Base {
7
by: Immortal Nephi | last post by:
My project grows large when I put too many member functions into one class. The header file and source code file will have approximately 50,000 lines when one class contains thousand member...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...

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.