Using a Dictionary for multiple class instances - see if entry of class exists
当我使用字典"动态"生成一个类的多个实例时-如何引用多个语句
我的上下文是下面一个扩展的更具体的粗体版本的问题-对不起,如果它需要澄清,我正在努力的边缘我的理解这一点。这也是我第一个问题,如果有太多或太少的细节很抱歉-谢谢
我正在处理和分析数据。
为了有效地处理这个问题,我希望将它按项目进行拆分,并添加一些其他细节-因此,我选择了一个类来存储这些内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public class Project { public bool Academic { get; set; } public string ProjectName { get; set; } public string Equipment { get; set; } public List<string> User = new List<string>(); public List<int> Hours = new List<int>(); public Project(bool academic, string projectName, string equipment, List<string> user, List<int> hours) { Academic = academic; ProjectName = projectName; Equipment = equipment; User = user; hours = Hours; } } |
我计划的方法是为每个新项目创建一个实例(我不知道一个给定条目将有多少个实例)
所以我使用Dictionary类来创建项目类的新实例。当我访问每个数据条目时,我想看看数据中的设备变量是否与我现有的任何实例中的设备变量匹配。我有一个循环,在这里我试图测试这个——但是看不到如何从一个给定的项目实例定义一个设备变量。
我在下面添加了更多的代码片段字典初始化
1 |
下面的代码是我使用字典创建第一个实例的地方,然后希望能够检查设备是否存在。
它检查头是否已从文件中读取,然后存储重要头的位置以备将来使用。(注意,它一次传递到行上)。然后,如果头已被读取,但没有头类的实例,则创建一个实例。否则,我希望它单步通过每个实例来查看上面的问题
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | internal void writeEntry(int length, int pos, string input, Processing formObject, int k) { Processing processing = formObject; if (processing.headingsRead == false) { findHeadingPostitons(processing.CurrentLine); processing.headingsRead = true; string x = processing.CurrentLine[headingPostitions.ProjectPosition]; } else { if(processing.projectNames.Count==0) { Type projectType = processing.projectNames[k.ToString()]; object myInstance = Activator.CreateInstance(projectType); Project myproject = (Project)myInstance; myproject.ProjectName = processing.CurrentLine[headingPostitions.ProjectPosition]; myproject.Equipment = processing.CurrentLine[headingPostitions.EquipmentPosition]; myproject.Commercial = Convert.ToBoolean(Convert.ToInt32(processing.CurrentLine[headingPostitions.CommercialPosition])); myproject.User.Add(processing.CurrentLine[headingPostitions.LaserUserPosition]); float hours = hoursCalc(processing); myproject.Hours.Add(hours); } else { for(int i =0;i< processing.projectNames.Count;i++) { if(processing.projectNames[i.ToString()]==????) { } } } } } |
编辑对于那些试图用字典来创建动态变量的人来说——这不是为C做这件事的方法——一个类实例列表可以完成这项工作!(见下面的解决方案)
下面是对我混乱的评论的一些扩展。这里的目标是通过使用泛型来查询项目对象。为了构建您的列表,它看起来就像使用了一个表单参数,因此出现了".net designer builder pattern"。希望这有帮助!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | using System; using System.Collections.Generic; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace GamerInMvc.Tests { [TestClass] public class UnitTest1 { [TestMethod] public void TestMethod1() { // Create a list of parts. List<Project> parts = new List<Project>(); // Add parts to the list. parts.Add(new Project() { ProjectName ="crank arm", ProjectId = 1234 }); parts.Add(new Project() { ProjectName ="chain ring", ProjectId = 1334 }); parts.Add(new Project() { ProjectName ="regular seat", ProjectId = 1434 }); parts.Add(new Project() { ProjectName ="banana seat", ProjectId = 1444 }); parts.Add(new Project() { ProjectName ="cassette", ProjectId = 1534 }); parts.Add(new Project() { ProjectName ="shift lever", ProjectId = 1634 }); ; // Write out the parts in the list. This will call the overridden ToString method // in the Part class. Console.WriteLine(); foreach (Project aPart in parts) { Console.WriteLine(aPart); } // Check the list for part #1734. This calls the IEquatable.Equals method // of the Part class, which checks the PartId for equality. Console.WriteLine(" Contains: Project with Id=1734: {0}", parts.Contains(new Project { ProjectId = 1734, ProjectName ="" })); // Find items where name contains"seat". Console.WriteLine(" Find: Project where name contains "seat": {0}", parts.Find(x => x.ProjectName.Contains("seat"))); // Check if an item with Id 1444 exists. Console.WriteLine(" Exists: Project with Id=1444: {0}", parts.Exists(x => x.ProjectId == 1444)); } } public class Project : IEquatable<Project> { public int ProjectId { get; set; } public bool Academic { get; set; } public string ProjectName { get; set; } public string Equipment { get; set; } public bool Commercial { get; set; } public List<string> User = new List<string>(); public List<int> Hours = new List<int>(); public Project() { //parameterless ctor for demo } public Project(bool academic, string projectName, string equipment, List<string> user, List<int> hours) { Academic = academic; ProjectName = projectName; Equipment = equipment; User = user; hours = Hours; } public override string ToString() { return"ID:" + ProjectId +" Name:" + ProjectName; } public override bool Equals(object obj) { if (obj == null) return false; Project objAsPart = obj as Project; if (objAsPart == null) return false; else return Equals(objAsPart); } public override int GetHashCode() { return ProjectId; } public bool Equals(Project other) { if (other == null) return false; return (this.ProjectId.Equals(other.ProjectId)); } } } |