Reputation: 61
I want to create a look-up table that can be referenced from other classes, so I am a attempting 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 it had a memory overflow. I stepped through the program and found that when I stepped into to above line the application called the line
public bool Fieldbus { get { return Fieldbus; } }
and remained there indefinitely. Stepping toggles between highlighting the '{' after 'get' and 'return Fieldbus', so it apparently enters an infinite loop.
Any ideas what I am doing wrong?
Upvotes: 4
Views: 1400
Reputation: 81189
If the purpose of a structure is to encapsulate a fixed collection of independent variables, simply exposing the fields directly will allow one to define much more concisely a structure which will allow one could do everything one could do if it exposed properties, in exactly the same way. Further, storage locations of the resulting structure's type will be mutable in exactly the same circumstances as they would be if one wrapped its fields in read-only properties; the primary semantic difference is that an exposed-field struct will allow some things to be done efficiently and in thread-safe fashion that a so-called immutable struct would only allow to be done in an inefficient and non-thread-safe fashion. The fact that struct methods which allow mutation via methods are poorly handled has led to a proclamation that "mutable structs are evil", even though the problems associated with struct-mutating methods do not occur with exposed struct fields.
Others have noted that the problem in your case is due to the use of a struct property name when the backing field name was required. Certainly correcting that would allow your code to work. I would suggest, however, that the simplest way to avoid such problems is to have the struct use public fields to encapsulate its state. Doing so would halve the length of your struct definition, allow its semantics to be seen at a glance, and eliminate the possibility that a struct which at a glance looks like it follows your intended pattern actually does something else.
Upvotes: 0
Reputation: 12215
public bool Fieldbus { get { return Fieldbus; } }
creates infinite recursion. Fieldbus
returns Fieldbus
returns Fieldbus
etc..., until the call stack flows over.
Upvotes: 0
Reputation: 2167
You need to return your private backup variable this.prvFieldbus
instead. This is also true for your other properties.
Upvotes: 5