C # list of const structure

I want to create a lookup table that can be referenced from other classes, so I'm trying to create a list of const structures as follows:

public struct DataRouting1 { public struct tParameters { private readonly bool prvFieldbus; private readonly int prvAddressMax; private readonly string prvTypeLabel; private readonly byte prvXTPtype; private readonly string prvPointLabel; private readonly int prvPointMin; private readonly int prvPointMax; private readonly int prvPointValue; private readonly int prvQuantityMax; public tParameters(bool Fieldbus, int AddressMax, string TypeLabel, byte XTPtype, string PointLabel, int PointMin, int PointMax, int PointValue, int QuantityMax) { this.prvFieldbus = Fieldbus; this.prvAddressMax = AddressMax; this.prvTypeLabel = TypeLabel; this.prvXTPtype = XTPtype; this.prvPointLabel = PointLabel; this.prvPointMin = PointMin; this.prvPointMax = PointMax; this.prvPointValue = PointValue; this.prvQuantityMax = QuantityMax; } public bool Fieldbus { get { return Fieldbus; } } public int AddressMax { get { return AddressMax; } } public string TypeLabel { get { return TypeLabel; } } public byte XTPtype { get { return XTPtype; } } public string PointLabel { get { return PointLabel; } } public int PointMin { get { return PointMin; } } public int PointMax { get { return PointMax; } } public int PointValue { get { return PointValue; } } public int QuantityMax { get { return QuantityMax; } } } public static readonly List<tParameters> Parameter = new List<tParameters> { new tParameters (true, 250, "Fieldbus Digital Input", 0x80, "Number", 1, 65535, 0, 255), new tParameters (true, 250, "Fieldbus Digital Output", 0x81, "Number", 1, 65535, 0, 255), new tParameters (true, 250, "Fieldbus Input Register", 0x82, "Number", 1, 65535, 0, 255), . . . }; } 

I use it as follows:

  if (DataRouting1.Parameter[Index].Fieldbus == false) . . 

This compiles correctly, but when I ran it, the program crashed, saying that it had an overflow of memory. I went through the program and found that when I entered the line above, the application called the line

  public bool Fieldbus { get { return Fieldbus; } } 

and stayed there endlessly. The steps switch between the '{' after 'get' and 'return Fieldbus' selections, so it seems to be entering an infinite loop.

Any ideas what I'm doing wrong?

+4
source share
4 answers

Instead, you need to return your private backup variable this.prvFieldbus . This is also true for other properties.

+5
source
 public bool Fieldbus { get { return prvFieldbus; } } 
+2
source
 public bool Fieldbus { get { return Fieldbus; } } 

creates infinite recursion. Fieldbus returns Fieldbus returns Fieldbus , etc ... until the call stack passes.

0
source

If the purpose of the structure is to encapsulate a fixed set of independent variables, simply expanding the fields directly will allow you to more clearly define the structure, which will allow you to do everything that could be done if it displayed the properties in exactly the same way. In addition, the storage locations of the resulting structure type will change in the same circumstances as if they wrapped their fields in read-only properties; the primary semantic difference is that the structure of the open field will allow you to do something very efficiently and safely in a streaming mode in some way, so that the so-called immutable structure will only allow you to do it in an inefficient and non-thread safe way. The fact that structure methods that allow mutation through methods are poorly processed has led to the proclamation that β€œmutable structures are evil,” although problems associated with structural mutation methods do not occur with open structure fields.

Others noted that the problem in your case is due to using the name of the structure property when the name of the support field is required. Of course, a fix that will allow your code to work. I would suggest, however, that the easiest way to avoid such problems is for the structure to use public fields to encapsulate its state. This will halve the definition of a structure, allowing you to see its semantics at a glance and eliminate the possibility that a structure that at first glance looks like it follows your intended template actually does something else.

0
source

Source: https://habr.com/ru/post/1496752/


All Articles