将词典用于多个类实例 - 查看类的条目是否存在

时间:2018-04-21 22:25:29

标签: c# .net class dictionary

当我使用字典“动态”制作类的多个实例时 - 如何引用多个语句

我的上下文是以粗体显示的问题的扩展更具体的版本 - 抱歉,如果需要澄清,我正在努力理解这一点。这也是我发布的第一个问题,如果有太多或太少的细节,那就很抱歉 - 谢谢

我正在处理和分析数据。

要有效地处理这个问题,我想通过项目拆分它,并添加一些其他细节 - 因此我选择了一个类来存储这些

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;
    }
}

我计划的方法是为每个新项目创建一个实例(我不知道给定条目会有多少个)

所以我使用字典类来创建Project类的新实例。当我到达每个数据条目时,我想查看数据中的设备变量是否与我现有实例的设备变量匹配。
我有一个循环,我试图测试这个 - 但无法看到如何从给定的项目实例定义设备变量

我在下面添加了更多代码段 字典初始化

public Dictionary<string, Type> projectNames = new Dictionary<string, Type>();

下面的代码是我使用字典创建第一个实例然后希望能够检查设备是否存在的地方。

它检查是否已从文件中读取标头,然后存储未来行的重要标头的位置。 (请注意,它一次传递给行) 然后,如果已经读取了标题但是没有标题类的实例则它会创建一个实例。否则我希望它逐步浏览每个实例以查看我的上述问题

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#的方法 - 类实例列表将完成这项工作! (见下面的解决方案)

1 个答案:

答案 0 :(得分:0)

这是我的混乱评论的一些扩展。这里的目标是通过使用泛型来查询您的Project对象。为了构建列表,看起来好像使用了表单参数,因此“.net designer builder pattern”。我希望这有帮助!

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("\nContains: Project with Id=1734: {0}",
                parts.Contains(new Project { ProjectId = 1734, ProjectName = "" }));

            // Find items where name contains "seat".
            Console.WriteLine("\nFind: Project where name contains \"seat\": {0}",
                parts.Find(x => x.ProjectName.Contains("seat")));

            // Check if an item with Id 1444 exists.
            Console.WriteLine("\nExists: 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));
        }
    }

}