none
Dictionary as class member? RRS feed

  • Question

  • 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?


    • Edited by Tom Ruby Monday, May 6, 2019 3:10 PM
    Monday, May 6, 2019 3:09 PM

Answers

  • Hello,

    In C# a field cannot be declare by var, you must explicit the type, only add the type instead of var.

    eg:

    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;
    
            public Dictionary<string, Component> 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}}
                };
    
    
        }
    }


    Monday, May 6, 2019 4:09 PM

All replies

  • Hello,

    In C# a field cannot be declare by var, you must explicit the type, only add the type instead of var.

    eg:

    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;
    
            public Dictionary<string, Component> 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}}
                };
    
    
        }
    }


    Monday, May 6, 2019 4:09 PM
  • Ok. My googlefu needs work.

    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;
    
    	public Dictionary<string, Component> 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}}
    			};
    	}
    I sure hope nobody is trying to google these hydrocarbons and keeps hitting my silly questions.

    Monday, May 6, 2019 4:11 PM
  • Hello,

    First off  Danilo Pereshas provided the proper syntax. I would suggest if you were considering using this data in more than one solution (container for projects) then create a standalone solution with a class project that is home for your code that may be used in other projects in a different solution.

    Now if you only have one Visual Studio solution with many projects you can a) place the code in a shared class project or even cooler is in say a new project, right click on the project and select "add existing item", select the item but don't click "add" button, instead click the down arrow on the add button and add the class as a link.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, May 6, 2019 8:19 PM
    Moderator
  • Greetings Tom.

    Danilo's solution is correct, but in case it's not clear why you need to do it that way, here's a quick explanation.

    The keyword 'var' resolves type at run time, not compile time, not matter how obvious it is. You can do that in a method, but a class definition can't contain members of indeterminate type, so you need to state the type of class members explicitly.

    Tuesday, May 7, 2019 1:33 AM
  • Consider a static class and a read-only dictionary too:

    public static class ComponentsHelp
    {
        public class Component
        {
            public string Name;
            public string id;
            public string TestValue;
            public string OrigValue;
            public bool Found;
        };
    
        public const int Useless = 42;
    
        public static readonly ReadOnlyDictionary<string, Component> ComponentList =
            new ReadOnlyDictionary<string, Component>( 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}},
                . . .
            }
    }
    
    

    Tuesday, May 7, 2019 5:12 AM
  • The keyword 'var' resolves type at run time, not compile time,

    Sorry, Ante Meridian. This is wrong. The keyword "var" is resolved at compile time. You can easily verify it by examining the MSIL code generated by the compiler using ildasm.exe. You will see that the correct type has been compiled. You can also hover the mouse over "var" in Visual Studio and you will see how it displays the real type of the variable (even though the program is not yet running). So the type of var is always known at compile time; otherwise you will get a compilation error.
    Wednesday, May 8, 2019 6:03 AM
    Moderator
  • Hi

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, May 13, 2019 8:59 AM
  • Yes. Danilo's answer was correct. I'll throw "var" away as I didn't like it any how.
    Wednesday, May 15, 2019 7:46 PM
  • Quite right, Alberto. My apologies.

    I misunderstood explanations like this one, from Microsoft's documentation;

    "Implicit typing with the var keyword can only be applied to variables at local method scope. Implicit typing is not available for class fields as the C# compiler would encounter a logical paradox as it processed the code: the compiler needs to know the type of the field, but it cannot determine the type until the assignment expression is analyzed, and the expression cannot be evaluated without knowing the type."

    Not that it really matters to me, because I never use 'var' if I can avoid it. Can't stand the stupid thing.

    Thursday, May 16, 2019 12:12 AM
  • Not that it really matters to me, because I never use 'var' if I can avoid it. Can't stand the stupid thing.

    Agreed. "var" is frequently overused. The one case where you have to use it is when you are using anonymous types. Another case when it is reasonable to use "var" is when you have MyClass c = new MyClass(). This can be abbreviated to var c = new MyClass() to avoid the repetition of MyClass. But then some people do var c = MyMethod() and this hurts the readability of the code because you don't know what type is this var unless you go and find the definition of MyMethod. I recommend avoinding var in such cases.
    Thursday, May 16, 2019 6:59 AM
    Moderator