Click here to Skip to main content
15,914,111 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,

I am developing a firmware in c# for some electrical device. This electrical device have different Nodes. For the configuration of the nodes I am creating a structure but the thing is There are 100 Nodes of this device so it means for the configuration do I have to create 100 structures or there is something else I can do.?

my structure is like this

C#
public struct {
            private UInt16 NodeAddress;
            private UInt16 NodeType;
            private UInt16 NumberOfSupportedSensors;
            private UInt16 NodeStatus;
            private UInt16 BaudRate;
            private UInt16 TimeOutValue;
            private UInt16 NumberOfPolls;
            private UInt16 ScanningPriority;
            private UInt16 CH1Status;
}


thanks
Azeem
Posted
Comments
Leo Chapiro 30-Sep-15 4:47am    
I have not understand what is exactly the problem, sorry! Can you explain it more detailed, please?
Muhammad Azym 30-Sep-15 5:04am    
I have 100 nodes to configured does it mean I have to create 100 structures in c#. for example in c we can only pass the reference of the structure and able to copy the values easily in c. is there anything like that in c#?
BillWoodruff 30-Sep-15 16:20pm    
"the issue is I have to send the whole structure over the TCP/IP into another device which is programmed in c and then configure the nodes."

If transferring the data requires the data to be highly compressed, you might take a look at WCF's Binary Serialization/Deserialization mode, or Marc Gravell's protobuf-net, a WCF compatible serializer that supports binary output ... with, Marc asserts, reduced file-sizes.

http://stackoverflow.com/a/1282825/133321

You just have to create 100 (possibly) different instances of the same struct.
If you are concerned about memory usage and many nodes share part of their state, then you might have a look at the Flyweight design pattern[^].
 
Share this answer
 
As an alternative to Carlo's solution, you could just create an array of the structs:
C#
MyNode[] myNodes = new MyNode[100];
Since a struct is a value type, that will create all the instances you need in one statement.

If you have a variable number of nodes, then you probably want a List<MyNode> instead, but in that case, you probably don't want a struct at all, you want a class as the struct will have to be boxed onto the heap anyway. As it happens, your struct should really be a class anyway, according to the guidelines - it exceeds the 16 byte limit that MS recommend.

Have a look here: Using struct and class - what's that all about?[^] and it will explain more.
 
Share this answer
 
Quote:
I have 100 nodes to configured does it mean I have to create 100 structures in c#. for example in c we can only pass the reference of the structure and able to copy the values easily in c. is there anything like that in c#?


For the first, I would like to use the class instead of structure - in C++ as well as C# it is possible (in contrast to C).

Now (having a class) you can write one or more copy constructor to copy / adjust / create the new instance of class.

(Well, actually you can make it having structure as well, but ... the class is simple cooler ;) )

If you need to create and manage several instances of the class / structure, see the answers above ( a
C#
List<Nodes>
or something else )
 
Share this answer
 
v3
Comments
Muhammad Azym 30-Sep-15 5:34am    
I know what you meant but the issue is I have to send the whole structure over the TCP/IP into another device which is programmed in c and then configure the nodes. :(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900