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 8 5672
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
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
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
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
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
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
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
Hi Nick,
thanks for this hint!
Dictionary looks good for my purpose.
Regards
Rainer This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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?
|
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'."
...
|
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
|
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...
|
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,
| |
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)
|
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
|
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?
--
|
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)
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |