如何从其他结构引用中调用C#中的Structures [英] How to call Structures in C# from other structure reference
问题描述
我正在为一些电子设备开发c#固件。该电气设备具有不同的节点。对于节点的配置,我正在创建一个结构,但事情是这个设备有100个节点,所以它意味着配置我必须创建100个结构或我还能做的其他事情。?
我的结构是这样的
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
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;
}
谢谢
Azeem
thanks
Azeem
推荐答案
你只需要创建100
(可能)相同struct $ c的不同实例 $ c>。
如果您担心内存使用情况并且许多节点共享部分状态,那么您可以查看自由设计模式 [ ^ ]。
You just have to create100
(possibly) different instances of the samestruct
.
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[^].
作为Carlo解决方案的替代方案,您可以创建一个结构数组:
As an alternative to Carlo's solution, you could just create an array of the structs:
MyNode[] myNodes = new MyNode[100];
由于struct是一个值类型,它将在一个语句中创建你需要的所有实例。
如果你有一个变量节点数,那么你可能想要一个List< MyNode>相反,但在这种情况下,你可能根本不需要一个结构,你想要一个类,因为结构必须被装箱到堆上。实际上,根据指南,你的结构应该真的是一个类 - 它超过了MS推荐的16字节限制。
看看这里:< a href =http://www.codeproject.com/Articles/728836/Using-struct-and-class-whats-that-all-about>使用struct和class - 这是怎么回事? [ ^ ]它会解释更多。
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.
我有100个节点要配置吗意思是我必须在c#中创建100个结构。例如,在c中,我们只能传递结构的引用,并且能够在c中轻松复制值。在c#中有类似的东西吗?
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#?
对于第一个,我想使用类而不是结构 - 在C ++和C#中它是可能的(与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
List<Nodes>
或者其他什么否则)
这篇关于如何从其他结构引用中调用C#中的Structures的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!