Dictionary as class member?

  • Thread starter Thread starter Tom Ruby
  • Start date Start date
T

Tom Ruby

Guest
I made this dictionary with 24 rows of data:

public class Component
{
public string Name { get; set; }
public string id { get; set; }
public string TestValue { get; set; }
public string OrigValue { get; set; }
public bool Found;
};

var ComponentList = new Dictionary<string, Component>(StringComparer.OrdinalIgnoreCase)
{
{ "Nitrogen", new Component(){Name="Nitrogen", id="N2", TestValue="60.38", OrigValue="", Found=false} },
{ "Methane", new Component(){Name="Methane", id="C1", TestValue="76.01", OrigValue="", Found=false}},
{ "Carbon Dioxide", new Component(){Name="Carbon Dioxide", id="CO2", TestValue="138.42", OrigValue = "", Found=false}},
{ "Ethylene", new Component(){Name="Ethylene", id="C2=", TestValue="145.32", OrigValue = "", Found=false}},
{ "Ethane", new Component(){Name="Ethane", id="C2", TestValue="125.20", OrigValue="", Found=false}},
{ "Propane", new Component(){Name="Propane", id="C3", TestValue="70.25",OrigValue="", Found=false}},
{ "Isobutane", new Component(){Name="Isobutane", id="IC4", TestValue="107.07", OrigValue="", Found=false}},
{ "Normal Butane", new Component(){Name="Normal Butane", id="NC4", TestValue="120.15", OrigValue="", Found=false}},
{ "Neopentane", new Component(){Name="Neopentane", id="NeoC5", TestValue="154.95", OrigValue="", Found=false}},
{ "Isopentane", new Component(){Name="Isopentane", id="IC5", TestValue="205.80", OrigValue="", Found=false}},
{ "Normal Pentane", new Component(){Name="Normal Pentane", id="NC5", TestValue="15.95", OrigValue="", Found=false}},
{ "Hexane Plus", new Component(){Name="Hexane Plus", id="C6+", TestValue="31.85", OrigValue="", Found=false}},
{ "Hexanes", new Component(){Name="Hexanes", id="C6s", TestValue="117.75", OrigValue="", Found=false}},
{ "Heptanes", new Component(){Name="Heptanes", id="C7s", TestValue="69.35", OrigValue="", Found=false}},
{ "Octanes", new Component(){Name="Octanes", id="C8s", TestValue="129.45", OrigValue="", Found=false}},
{ "Nonanes", new Component(){Name="Nonanes", id="C9s", TestValue="73.51", OrigValue="", Found=false}},
{ "Decanes", new Component(){Name="Decanes", id="C10s", TestValue="49.57", OrigValue="", Found=false}},
{ "Undecanes", new Component(){Name="Undecanes", id="C11s", TestValue="98.26", OrigValue="", Found=false}},
{ "Ethylene Minus", new Component(){Name="Ethylene Minus", id="C2-", TestValue="132.25", OrigValue="", Found=false}},
{ "Propane Plus", new Component(){Name="Propane Plus", id="C3+", TestValue="86.75", OrigValue="", Found=false}},
{ "Hydrogen Sulfide", new Component(){Name="Hydrogen Sulfide", id="H2S", TestValue="73.62", OrigValue="", Found=false}},
{ "Water", new Component(){Name="Water", id="H2O", TestValue="26.12", OrigValue="", Found=false}},
{ "Helium", new Component(){Name="Helium", id="He", TestValue="19.56", OrigValue="", Found=false}},
{ "Hydrogen", new Component(){Name="Hydrogen", id="H2", TestValue="38.67", OrigValue="", Found=false}}
};



Now this block of data seems pretty useful in other places. I could, like the C# noob I am, just copy/paste it all over the place, but as an antique computer science practitioner, I know there must be a better way.

"Perhaps I'll make a class!"

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Generic;

namespace CommonPages
{
public class ComponentsHelp
{
public class Component
{
public string Name { get; set; }
public string id { get; set; }
public string TestValue { get; set; }
public string OrigValue { get; set; }
public bool Found;
};

public int Useless = 42;

var ComponentList = new Dictionary<string, Component>(StringComparer.OrdinalIgnoreCase)
{
{ "Nitrogen", new Component(){Name="Nitrogen", id="N2", TestValue="60.38", OrigValue="", Found=false} },
{ "Methane", new Component(){Name="Methane", id="C1", TestValue="76.01", OrigValue="", Found=false}},
{ "Carbon Dioxide", new Component(){Name="Carbon Dioxide", id="CO2", TestValue="138.42", OrigValue = "", Found=false}},
{ "Ethylene", new Component(){Name="Ethylene", id="C2=", TestValue="145.32", OrigValue = "", Found=false}},
{ "Ethane", new Component(){Name="Ethane", id="C2", TestValue="125.20", OrigValue="", Found=false}},
{ "Propane", new Component(){Name="Propane", id="C3", TestValue="70.25",OrigValue="", Found=false}},
{ "Isobutane", new Component(){Name="Isobutane", id="IC4", TestValue="107.07", OrigValue="", Found=false}},
{ "Normal Butane", new Component(){Name="Normal Butane", id="NC4", TestValue="120.15", OrigValue="", Found=false}},
{ "Neopentane", new Component(){Name="Neopentane", id="NeoC5", TestValue="154.95", OrigValue="", Found=false}},
{ "Isopentane", new Component(){Name="Isopentane", id="IC5", TestValue="205.80", OrigValue="", Found=false}},
{ "Normal Pentane", new Component(){Name="Normal Pentane", id="NC5", TestValue="15.95", OrigValue="", Found=false}},
{ "Hexane Plus", new Component(){Name="Hexane Plus", id="C6+", TestValue="31.85", OrigValue="", Found=false}},
{ "Hexanes", new Component(){Name="Hexanes", id="C6s", TestValue="117.75", OrigValue="", Found=false}},
{ "Heptanes", new Component(){Name="Heptanes", id="C7s", TestValue="69.35", OrigValue="", Found=false}},
{ "Octanes", new Component(){Name="Octanes", id="C8s", TestValue="129.45", OrigValue="", Found=false}},
{ "Nonanes", new Component(){Name="Nonanes", id="C9s", TestValue="73.51", OrigValue="", Found=false}},
{ "Decanes", new Component(){Name="Decanes", id="C10s", TestValue="49.57", OrigValue="", Found=false}},
{ "Undecanes", new Component(){Name="Undecanes", id="C11s", TestValue="98.26", OrigValue="", Found=false}},
{ "Ethylene Minus", new Component(){Name="Ethylene Minus", id="C2-", TestValue="132.25", OrigValue="", Found=false}},
{ "Propane Plus", new Component(){Name="Propane Plus", id="C3+", TestValue="86.75", OrigValue="", Found=false}},
{ "Hydrogen Sulfide", new Component(){Name="Hydrogen Sulfide", id="H2S", TestValue="73.62", OrigValue="", Found=false}},
{ "Water", new Component(){Name="Water", id="H2O", TestValue="26.12", OrigValue="", Found=false}},
{ "Helium", new Component(){Name="Helium", id="He", TestValue="19.56", OrigValue="", Found=false}},
{ "Hydrogen", new Component(){Name="Hydrogen", id="H2", TestValue="38.67", OrigValue="", Found=false}}
};


}
}


But C# complains about the var "The contextual keyword var may only appear within a local variable declaration or in script code."

Thinking, "this is where members go, isn't it?" I added a useless integer, and C# accepts that.

"Ok," the partly dressed brat concludes, "there must be something (else) I don't know about C#." How can a class put a dictionary as a public member, or is there a better idea?

Continue reading...
 
Back
Top