473,725 Members | 1,980 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(typ eof(Telegram1)) ;

TlgList.Add(typ eof(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.Telegr amNo;

but if I try the same with:

tlgList[i].TelegramNo I can't compile my code any more, because of
"System.Typ e 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 5678
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.Creat eInstance(teleg ram1,{tlgByteSt ream});

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.Creat eInstance(teleg ram1,{tlgByteSt ream});

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.Creat eInstance (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.Creat eInstance (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******** ******@TK2MSFTN GP12.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(typ eof(Telegram1)) ;

TlgList.Add(typ eof(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.Telegr amNo;

but if I try the same with:

tlgList[i].TelegramNo I can't compile my code any more, because of
"System.Typ e 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
6661
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 give me this capability. So I want std::list<T*>::iterator. However, the container is of type std::list<T*>. How to get std::list<const T*>::iterator?
4
52987
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>. However, I need to cast that list to a List<BaseClass> and it is not working. The code is below. I get the following exception: "Unable to cast object of type 'System.Collections.Generic.List`1' to type 'System.Collections.Generic.List`1'." ...
1
2208
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 partial string "Dog" Thanks
1
2978
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 IEnumerable<typeand storing it in memory first? This question is really about optimizing large collections of data returned by web services. If it is possible to loop through the IEnumerable<type> with a "foreach" and somehow start formatting the...
9
7901
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 List<as a property. Consider the example below, the class "ListTest" contains a private "List<>" called "strings" - it also provides a public method to add to that list,
10
6467
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 MyObject<T: ChangeObject { ... public MyObject(string name, T value)
2
4965
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 List<ImplInterfaceto IEnumerable<Interface>? Thanks, BH
6
14571
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
18642
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: List<int> Pkey = new List<int>(); Dictionary<List<int>, Polynomial> A = new Dictionary<List<int>, Polynomial>(); // initialize A with last column. for (int r = 0; r < n; ++r)
0
8752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9257
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9111
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4517
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4782
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2634
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.