首页 \ 问答 \ 类文本文件到数据字典(Textfile to class to Data Dictionary)

类文本文件到数据字典(Textfile to class to Data Dictionary)

我正在尝试创建一个进入类的文本文件,并将该对象存储到数据字典中,然后创建一个允许我添加,编辑和删除等的GUI。

我的文本文件使用以下语法:

国家,GDP增长,通货膨胀,贸易平衡,HDI排名,主要贸易伙伴

例子:

  • 美国,1.8,2,-3.1,4,[加拿大,英国,巴西]
  • 加拿大,1.9,2.2,-2,6,[美国;中国]

无论如何,在我创建GUI之前,我试图让它在控制台中首先工作。 国家出现在控制台中但使用调试器中的步骤,我的对象数组和我的数据字典(如果我已正确创建数据字典)似乎是存储但是当它继续到下一个国家时它会覆盖前一个。 我怎样才能让所有国家都存储而不仅仅是存储。 如果这是有道理的,任何帮助将不胜感激。

我的代码:

 class Program
{
    static void Main(string[] args)
    {

        const int MAX_LINES_FILE = 50000;
        string[] AllLines = new string[MAX_LINES_FILE];
        int i = 0;
        //reads from bin/DEBUG subdirectory of project directory
        AllLines = File.ReadAllLines(@"C:\Users\Jack\Documents\countries.csv");
        country[] newCountry = new country[30];

        foreach (string line in AllLines)
        {
            if (line.StartsWith("Country")) //found first line - headers
            {
                headers = line.Split(',');
            }
            else
            {
               string[] columns = line.Split(',');
                      newCountry[i] = new country();
                 newCountry[i].Country=(columns[0]);
                 newCountry[i].GDP=(columns[1]);
                 newCountry[i].Inflation=(columns[2]);
                 newCountry[i].TB =(columns[3]);
                 newCountry[i].HDI =(columns[4]);
                 newCountry[i].TP = (columns[5]);


                Dictionary<object, string> CountryList = new Dictionary<object, string>();
                CountryList.Add(newCountry[i].Country, newCountry[i].GDP + "," + newCountry[i].Inflation + "," + newCountry[i].TB + "," + newCountry[i].HDI + "," + newCountry[i].TP);
                i++;

               foreach (KeyValuePair<object, string> country in CountryList)
                {
                    Console.WriteLine("Country = {0}, GDP = {1}",
                        country.Key, country.Value);
                }

            }
            Console.ReadKey();
        }
    }

    public static string[] headers { get; set; }
}


public class country
{
    public string Country { get; set; }
    public string GDP { get; set; }
    public string Inflation { get; set; }
    public string TB { get; set; }
    public string HDI { get; set; }
    public string TP { get; set; }

}

}

编辑:按照建议移动CountryList仍然得到countryList计数保持1的相同问题。

我把它放在国家级的方法中。

        public static void addD(country a)
    {
        Dictionary<object, string> CountryList = new Dictionary<object, string>();
        CountryList.Add(a.Country, a.GDP);


        foreach (KeyValuePair<object, string> country in CountryList)
        {
            Console.WriteLine("Country = {0}, GDP = {1}", country.Key, country.Value);
        }
    }

并从这里调用它:

            newCountry[i].TB =(columns[3]);
         newCountry[i].HDI =(columns[4]);
         newCountry[i].TP = (columns[5]);
         country.addD(newCountry[i]);

I'm trying to make a textfile that goes into a class and stores the object into a data dictionary and then create a GUI that allows me to add, edit and remove etc.

My text file is in the following syntax:

Country, GDP growth, Inflation, Trade Balance, HDI Ranking, Main Trade Partners

Examples:

  • USA,1.8,2,-3.1,4,[Canada;UK;Brazil]
  • Canada,1.9,2.2,-2,6,[USA;China]

Anyway before I create the GUI, I am trying make it work in the console first. The Countries come up in the console but using the step into in the debugger, my object array and my data dictionary (if I have created the data dictionary correctly) seem to be storing but though when it goes on to the next country it overwrites the previous one. How can I make it so that all countries get stored instead of just the one. If that makes sense any help will be greatly appreciated.

My code:

 class Program
{
    static void Main(string[] args)
    {

        const int MAX_LINES_FILE = 50000;
        string[] AllLines = new string[MAX_LINES_FILE];
        int i = 0;
        //reads from bin/DEBUG subdirectory of project directory
        AllLines = File.ReadAllLines(@"C:\Users\Jack\Documents\countries.csv");
        country[] newCountry = new country[30];

        foreach (string line in AllLines)
        {
            if (line.StartsWith("Country")) //found first line - headers
            {
                headers = line.Split(',');
            }
            else
            {
               string[] columns = line.Split(',');
                      newCountry[i] = new country();
                 newCountry[i].Country=(columns[0]);
                 newCountry[i].GDP=(columns[1]);
                 newCountry[i].Inflation=(columns[2]);
                 newCountry[i].TB =(columns[3]);
                 newCountry[i].HDI =(columns[4]);
                 newCountry[i].TP = (columns[5]);


                Dictionary<object, string> CountryList = new Dictionary<object, string>();
                CountryList.Add(newCountry[i].Country, newCountry[i].GDP + "," + newCountry[i].Inflation + "," + newCountry[i].TB + "," + newCountry[i].HDI + "," + newCountry[i].TP);
                i++;

               foreach (KeyValuePair<object, string> country in CountryList)
                {
                    Console.WriteLine("Country = {0}, GDP = {1}",
                        country.Key, country.Value);
                }

            }
            Console.ReadKey();
        }
    }

    public static string[] headers { get; set; }
}


public class country
{
    public string Country { get; set; }
    public string GDP { get; set; }
    public string Inflation { get; set; }
    public string TB { get; set; }
    public string HDI { get; set; }
    public string TP { get; set; }

}

}

EDIT: Moved the CountryList as suggested still getting the same issue that the countryList count is staying on 1.

I have put it in a method in the country class.

        public static void addD(country a)
    {
        Dictionary<object, string> CountryList = new Dictionary<object, string>();
        CountryList.Add(a.Country, a.GDP);


        foreach (KeyValuePair<object, string> country in CountryList)
        {
            Console.WriteLine("Country = {0}, GDP = {1}", country.Key, country.Value);
        }
    }

and calling it from here:

            newCountry[i].TB =(columns[3]);
         newCountry[i].HDI =(columns[4]);
         newCountry[i].TP = (columns[5]);
         country.addD(newCountry[i]);

原文:https://stackoverflow.com/questions/31565950
更新时间:2022-07-06 15:07

最满意答案

如果你需要一个PlayerPlane作为一个类,那么你可以这样做(因为PlayerPlanePlayerPlane的一个子类,你可以使用它的初始值):

class PlayerPlane: SKSpriteNode {

    static func populatePlayerPlane() -> PlayerPlane {
        let playerPlane = PlayerPlane(imageNamed: "airplane_1_14")
        playerPlane.position = CGPoint(x: 200, y: 200)
        playerPlane.zPosition = 50
        return playerPlane
    }

    func fireEnemies() {
        print("Attack")
    }
}

但是你也可以使用一个协议并为SKSpriteNode编写一个扩展,如果你想让其他对象/结构体采用PlayerPlane功能,这很方便:

protocol PlayerPlane {

    func fireEnemies()
}

extension SKSpriteNode : PlayerPlane {

    func fireEnemies() {
        print("Attack")
    }

}

extension SKSpriteNode {

    static func populatePlayerPlane() -> SKSpriteNode {
        let playerPlane = SKSpriteNode(imageNamed: "airplane_1_14")
        playerPlane.position = CGPoint(x: 200, y: 200)
        playerPlane.zPosition = 50
        return playerPlane
    }

}

最后,当你想以其他方式将fireEnemies功能附加到你的PlayerPlane你还可以为协议写一个扩展名:

class PlayerPlane: SKSpriteNode {

    static func populatePlayerPlane() -> PlayerPlane {
        let playerPlane = PlayerPlane(imageNamed: "airplane_1_14")
        playerPlane.position = CGPoint(x: 200, y: 200)
        playerPlane.zPosition = 50
        return playerPlane
    }
}


protocol FireEnemies {
    func fireEnemies()
}

extension PlayerPlane : FireEnemies {
    func fireEnemies() {
        print("Attack")
    }
}

If you need a PlayerPlane as a class then you can make it this way (as PlayerPlane is a subclass of SKSpriteNode you can use it's initializers):

class PlayerPlane: SKSpriteNode {

    static func populatePlayerPlane() -> PlayerPlane {
        let playerPlane = PlayerPlane(imageNamed: "airplane_1_14")
        playerPlane.position = CGPoint(x: 200, y: 200)
        playerPlane.zPosition = 50
        return playerPlane
    }

    func fireEnemies() {
        print("Attack")
    }
}

But you can also use a protocol and write an extension for SKSpriteNode which is convenient if you want to make other objects/structs adopt PlayerPlane functionality:

protocol PlayerPlane {

    func fireEnemies()
}

extension SKSpriteNode : PlayerPlane {

    func fireEnemies() {
        print("Attack")
    }

}

extension SKSpriteNode {

    static func populatePlayerPlane() -> SKSpriteNode {
        let playerPlane = SKSpriteNode(imageNamed: "airplane_1_14")
        playerPlane.position = CGPoint(x: 200, y: 200)
        playerPlane.zPosition = 50
        return playerPlane
    }

}

Finally, when you want "other" way to attach fireEnemies functionality to your PlayerPlane you can also write an extension for a protocol:

class PlayerPlane: SKSpriteNode {

    static func populatePlayerPlane() -> PlayerPlane {
        let playerPlane = PlayerPlane(imageNamed: "airplane_1_14")
        playerPlane.position = CGPoint(x: 200, y: 200)
        playerPlane.zPosition = 50
        return playerPlane
    }
}


protocol FireEnemies {
    func fireEnemies()
}

extension PlayerPlane : FireEnemies {
    func fireEnemies() {
        print("Attack")
    }
}

相关问答

更多

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)