0

I have a lot of txt file, that contains a lot of datas. For example:

1.txt
parameter1_1="[1 3 21 12 42]"
parameter2_1="[4 2 28 72 46]"
parameter2_2="[2 6 46 75 76]"
58.txt
parameter1_1="[9 8 98 87 89]"
parameter1_2="[2 4 11 42 62]"
parameter2_1="[9 4 25 67 56]"
parameter2_2="[7 6 87 79 75]"

In the first txt file all of the parameter's first value is a coil datas. The second value is the second coil datas. I want to put all of the value into one class/object what is contains all of the value`like you can see on the picture.

enter image description here

When I upload the object, the index cannot be slip, so if I choose the index 1 from teh collection, then I get the correct datas.

I created a class as you can see, and I cannot get the nested class property by string.

I have the next class

    public class Coil
{
    public string ID { get; set; }
}

public class FailStation : Coil
{
    public Station_1 Station_1 = new Station_1();
    public Station_2 Station_2 = new Station_2();
}

public class Station_1
{
    public string parameter1_1{ get; set; }
    public string parameter1_2 { get; set; }
}
public class Station_2
{
    public string parameter2_1{ get; set; }
    public string parameter2_2 { get; set; }
}

I would like to set and get the value like this or something simiar:

FailStationcoil coil = new FailStation();
List<FailStation> AllCoil = new List<FailStation>();
coil["parameter1_1"] = "value"; //or
coil.SetPropValue("parameter1_1", "value");
coil["ID"]="1";
AllCoil.Add(coil);
Alex
  • 49
  • 1
  • 5
  • 3
    Possible duplicate of [Setting a property by reflection with a string value](https://stackoverflow.com/questions/1089123/setting-a-property-by-reflection-with-a-string-value) – obl Nov 06 '18 at 14:57
  • 2
    Have you considered using a simple `Dictionary` instead of nested classes with properties? – Fildor Nov 06 '18 at 14:59
  • @obl That one is not a nested class. – Alex Nov 06 '18 at 15:11
  • @Fildor Now the program work with Dictionary, but it's not really dynamic. – Alex Nov 06 '18 at 15:18
  • 1
    What do you mean by "dynamic"? Could you explain what your actual goal behind all this is? – Fildor Nov 06 '18 at 15:20
  • @Alex Please explain why it matters that these are nested classes. What is `coildata`? Is it an object of type `Coil`? `Station_101`? Are you trying to set a property of type `string` or `object`? – obl Nov 06 '18 at 15:25
  • @obl I exlained better. Please read it. – Alex Nov 07 '18 at 12:32
  • @Fildor I want to set value by string and not for example coil.Station_1.parameter2_1=value, because I have more then 100 parameter. – Alex Nov 07 '18 at 12:50

1 Answers1

0

It looks like your parameter names already tell you what Station it is, unless I'm misreading it.

parameter1_1 => the first number tells you Station_{n}, the second number tells you cam_term_{n}_val

So why not ditch the Station_1 and Station_2 classes and just use a Dictionary<string, int> or Dictionary<string, object> (if you need to store the keys with no value) as @Fildor suggested?

Reflection is expensive and should only be used if absolutely necessary. Your situation, as you have explained so far, doesn't look like it requires it.

obl
  • 1,799
  • 12
  • 38
  • Yes in the example the name is similar, but in the real life, where I want to you I don't know the station name from the parameter name, because it's different. Please check the picture. If the reflection is expensive, then how can I link the parameter name to the station? Could you suggest a good method? – Alex Nov 07 '18 at 16:24
  • @Alex please update your code to show how you are reading in the text files. I don't understand what you mean by the station name and parameter name being different. How then do you know which properties go to which station? – obl Nov 07 '18 at 17:02