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

accessing static members through Type?

Hi NG,

I have a generic list of Type where I hold some class types like:

List<Type> classList = new List<Type>();
classList.Add(typeof(Telegram1);
classList.Add(typeof(Telegram2);
......

Each of my Telegram<x> classes has a static Member "TlgNo";
With out building a instance of the class, I can get the TlgNo like
int tlgNo = Telegram1.TlgNo;

Is there a way to get this static member of the class from my list?
What I would like to do is:

for(int i=0; i<classList.Count; i++)
{
aTlgNo = classList[i].TlgNo;
......
}

Thanks for hints,

Rainer Queck
May 8 '06 #1
5 1212
"Rainer Queck" <Ra****@noemail.noemail> a écrit dans le message de news:
uu**************@TK2MSFTNGP05.phx.gbl...

| I have a generic list of Type where I hold some class types like:
|
| List<Type> classList = new List<Type>();
| classList.Add(typeof(Telegram1);
| classList.Add(typeof(Telegram2);
| .....
|
| Each of my Telegram<x> classes has a static Member "TlgNo";
| With out building a instance of the class, I can get the TlgNo like
| int tlgNo = Telegram1.TlgNo;
|
| Is there a way to get this static member of the class from my list?
| What I would like to do is:
|
| for(int i=0; i<classList.Count; i++)
| {
| aTlgNo = classList[i].TlgNo;
| ......
| }

Assuming TlgNo is a static property, you need to use reflection like this :

foreach (object o in classList)
{
Type itemType = o.GetType();

PropertyInfo tlgNoProperty = itemType.GetProperty("TlgNo");
if (tlgNoProperty != null)
{
int aTlgNo = (int) tlgNoProperty.GetValue(null, null);
...
}
...
}

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
May 8 '06 #2
Hi Joanna,

thanks a lot for you help. Works perfect.

Rainer
May 8 '06 #3
A more object oriented solution could be to add an abstract property in
the base class and implement it in each class:

class Telegram {
abstract int TelegramNo { get; }
}

class Telegram1 : Telegram {
static int TlgNo = 1;
public virtual int TelegramNo { get { return TlgNo; } }
}

class Telegram2 : Telegram {
static int TlgNo = 2;
public virtual int TelegramNo { get { return TlgNo; } }
}

(It's my personal opinion that reflection generally is bad for program
structure. ;) )

Rainer Queck wrote:
Hi NG,

I have a generic list of Type where I hold some class types like:

List<Type> classList = new List<Type>();
classList.Add(typeof(Telegram1);
classList.Add(typeof(Telegram2);
.....

Each of my Telegram<x> classes has a static Member "TlgNo";
With out building a instance of the class, I can get the TlgNo like
int tlgNo = Telegram1.TlgNo;

Is there a way to get this static member of the class from my list?
What I would like to do is:

for(int i=0; i<classList.Count; i++)
{
aTlgNo = classList[i].TlgNo;
......
}

Thanks for hints,

Rainer Queck

May 8 '06 #4
"Göran Andersson" <gu***@guffa.com> a écrit dans le message de news:
%2****************@TK2MSFTNGP05.phx.gbl...

|A more object oriented solution could be to add an abstract property in
| the base class and implement it in each class:

....

| (It's my personal opinion that reflection generally is bad for program
| structure. ;) )

Unfortunately Rainer wanted to access the TlgNo value without having to
instantiate anything. Therefore, virtual properties/methods are not a viable
option.

Therefore, in the absence of virtual static members, the simplest choice is
reflection.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
May 8 '06 #5
Aha, I see. When I look at the code again, I see that the List is only
containing the type of the objects, not any objects. Odd construction...
but in that case there is not much to do but to use reflection.

I didn't pay that much attention to the code as it isn't the actual code
that is used, as it won't even compile...

Joanna Carter [TeamB] wrote:
"Göran Andersson" <gu***@guffa.com> a écrit dans le message de news:
%2****************@TK2MSFTNGP05.phx.gbl...

|A more object oriented solution could be to add an abstract property in
| the base class and implement it in each class:

...

| (It's my personal opinion that reflection generally is bad for program
| structure. ;) )

Unfortunately Rainer wanted to access the TlgNo value without having to
instantiate anything. Therefore, virtual properties/methods are not a viable
option.

Therefore, in the absence of virtual static members, the simplest choice is
reflection.

Joanna

May 8 '06 #6

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

Similar topics

3
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming...
4
by: ambar.shome | last post by:
Hi, Please take a look at the code snippet given below:#include<iostream> using namespace std; class A { public: virtual void test(){cout<<"Default implementation called"<<endl;}
6
by: lovecreatesbeauty | last post by:
Hello Experts, Why static data members can be declared as the type of class which it belongs to? Inside a class, non-static data members such as pointers and references can be declared as...
11
by: dee | last post by:
OleDbCommand class like many .NET classes has the following description in its help file: "Thread Safety Any public static (Shared in Visual Basic) members of this type are safe for...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
2
by: Rafe Culpin | last post by:
Does anyone please know of a way to access static methods of a class, when the name of that class is held in a variable? I have several classes (PHP5) which all have identically named methods and...
8
by: crjjrc | last post by:
Hi, I've got a base class and some derived classes that look something like this: class Base { public: int getType() { return type; } private: static const int type = 0; };
2
by: agendum97 | last post by:
Is there a way of constructing a "class" in JavaScript to access a private member of a different scope of the same type? It is important that the variable remain private, and not accessible in...
6
by: Bhawna | last post by:
I am into c++ code maintenance for last 3-4 years but recently I am put into design phase of a new project. Being a small comapany I dont have enough guidance from seniors. Currently I am into a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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
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...
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...

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.