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

List<Type>

Hi NG,

I have a base class "Telegram". This class , as well as all descants have a
static public field "public static int TelegramNo = <a tlg no>;"

Now I added all descants of Telegram to a generic List<Type>:

List<Type> tlgList = new List<Type>();

TlgList.Add(typeof(Telegram1));

TlgList.Add(typeof(Telegram2));

The idea now is, that if I receive a tlg stream I look for a telegram
nummber in it, and then scan my tlgList for the right class to instanciat.

My problem now is, how can I do that?

A static field of a class I can access by :

int i = Telegram.TelegramNo;

but if I try the same with:

tlgList[i].TelegramNo I can't compile my code any more, because of
"System.Type has no definition for TelegramNo".

How can I get access to the static field, with out creating an instance of
Telegram?

Thanks for hints and help!

Regards

Rainer


Jan 17 '06 #1
8 5658
Rainer Queck wrote:

<snip>
How can I get access to the static field, with out creating an instance of
Telegram?


You'd need to use reflection. You're basically trying to add
polymorphism over static members, which doesn't exist. An alternative
would be to have a class TelegramType which had the Type it was about,
and the number.

Jon

Jan 17 '06 #2
Hi Jon,

thans for you answer.
A little background Info...
I am comming from Delphi, where there is a class "TClassList".
There I was able to store the Class in the ist and use its static (class)
members like
myClassList[i].<static Field>.
I was now looking for a simular mechanism in C#.
You'd need to use reflection. You're basically trying to add
polymorphism over static members, which doesn't exist. An alternative
would be to have a class TelegramType which had the Type it was about,
and the number.

But then I would have to build a instance of TelegramType, before I can
access it's number, right?
My thought was not to build any instance, before I know which one to build.

I already thought about a hash table where the key could be the telegram
number....
Do you think that would be a solution?

Regards
Rainer
Jan 17 '06 #3
Rainer Queck wrote:
A little background Info...
I am comming from Delphi, where there is a class "TClassList".
There I was able to store the Class in the ist and use its static (class)
members like
myClassList[i].<static Field>.
I was now looking for a simular mechanism in C#.
Yes - I believe Delphi has polymorphism over types as well as
instances. .NET doesn't.
You'd need to use reflection. You're basically trying to add
polymorphism over static members, which doesn't exist. An alternative
would be to have a class TelegramType which had the Type it was about,
and the number.

But then I would have to build a instance of TelegramType, before I can
access it's number, right?
My thought was not to build any instance, before I know which one to build.
These would be instances of type descriptors though - not the telegrams
themselves. Each telegram type would have an instance of TelegramType
which knew its number and how to build a Telegram instance.
I already thought about a hash table where the key could be the telegram
number....
Do you think that would be a solution?


Yes, that would work too. It depends whether you want to put more
useful information in, such as how to build a Telegram instance given
the type number.

Jon

Jan 17 '06 #4
Hi Jon,
Yes, that would work too. It depends whether you want to put more
useful information in, such as how to build a Telegram instance given
the type number.

In my case I think that should be enough. A telegram class would know how
build its instance by a given byte stream.
Would building the instance with

Activator.CreateInstance(telegram1,{tlgByteStream} );

where tlgByteStream is a byte[] and ther is a constructor to telegram1 like
"telegram1(byte[] msg)" work?

Regards
Rainer
Jan 17 '06 #5
Rainer Queck wrote:
Yes, that would work too. It depends whether you want to put more
useful information in, such as how to build a Telegram instance given
the type number.

In my case I think that should be enough. A telegram class would know how
build its instance by a given byte stream.
Would building the instance with

Activator.CreateInstance(telegram1,{tlgByteStream} );

where tlgByteStream is a byte[] and ther is a constructor to telegram1 like
"telegram1(byte[] msg)" work?


Nearly - you'd need to change it to

Activator.CreateInstance (telegram1, new object[]{tlgByteStream});

But other than that, it should work.

Jon

Jan 17 '06 #6
Hi Jon,
Nearly - you'd need to change it to

Activator.CreateInstance (telegram1, new object[]{tlgByteStream});

But other than that, it should work.

Great! Then this seems to be the way, how I will do it.

Thanks for your Help!
Rainer
Jan 17 '06 #7
Use a dictionary to map TelegramNo to Type

"Rainer Queck" <Ra****@noemail.noemail> wrote in message
news:eL**************@TK2MSFTNGP12.phx.gbl...
Hi NG,

I have a base class "Telegram". This class , as well as all descants have
a static public field "public static int TelegramNo = <a tlg no>;"

Now I added all descants of Telegram to a generic List<Type>:

List<Type> tlgList = new List<Type>();

TlgList.Add(typeof(Telegram1));

TlgList.Add(typeof(Telegram2));

The idea now is, that if I receive a tlg stream I look for a telegram
nummber in it, and then scan my tlgList for the right class to instanciat.

My problem now is, how can I do that?

A static field of a class I can access by :

int i = Telegram.TelegramNo;

but if I try the same with:

tlgList[i].TelegramNo I can't compile my code any more, because of
"System.Type has no definition for TelegramNo".

How can I get access to the static field, with out creating an instance of
Telegram?

Thanks for hints and help!

Regards

Rainer


Jan 18 '06 #8
Hi Nick,

thanks for this hint!
Dictionary looks good for my purpose.

Regards
Rainer
Jan 18 '06 #9

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

Similar topics

6
by: PengYu.UT | last post by:
Hi, Suppose I have a list which contains pointers. I want the pointer got by dereferencing the iterator be a pointer pointing to a const object. But std::list<const T*>::const_iterator doens't...
4
by: matty.hall | last post by:
I have two classes: a base class (BaseClass) and a class deriving from it (DerivedClass). I have a List<DerivedClass> that for various reasons needs to be of that type, and not a List<BaseClass>....
1
by: Robert Bravery | last post by:
Hi all, How can I use the contains method to find a partial value in a list eg if I have a list that contains "I love my Dog" "I hate the mouse" and I want to know if the list contains the...
1
by: =?Utf-8?B?RnJhbmNvaXNWaWxqb2Vu?= | last post by:
Hi there, Does anybody know how to return the results of an IEnumerable<typeas an array of the same type i.e type in a web service call without first having to collect all elements in the...
9
by: Paul | last post by:
Hi, I feel I'm going around circles on this one and would appreciate some other points of view. From a design / encapsulation point of view, what's the best practise for returning a private...
10
by: lpinho | last post by:
Hi all, I have a class (named for the example myObject) that can be of several types (int, string, float, etc), instead of using a object to define it's type I used a generic. public class...
2
by: Berryl Hesh | last post by:
Converting in the other direction , IEnumerable<Interfaceto List<ImplInterface>, I can just create a new List<ImplInterface(data) where data is IEnumerable<Interface>. How can I convert the...
6
by: A.Rocha | last post by:
Hi, I need save to text file a List<type>, but i dont wont serialize. // List<mytypemyList = new List<mytype>(); anyone can help me? --
15
by: hsachdevah | last post by:
Hello, I am trying to create a dictionary item with its key as list type and value as custom object type. It gives me a error "An item with the same key has already been added." Here is my code:...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...

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.