使用类和构造函数 [英] Using classes and constructors

查看:98
本文介绍了使用类和构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为OCRChar的类

I have a class named OCRChar

class OCRChar
    {
        public string fileName;
        public string binVal;       

        public OCRChar(string iFileName, string iBinVal)
        {
            this.fileName = iFileName;
            this.binVal = iBinVal;          
        }
    }



我的主程序包含一个按钮,该按钮加载一些.png文件,将其转换为二进制文件,我需要将它们存储在内存中,以便以后可以使用二进制值来训练神经网络.



My Main Program consists of a button that loads some .png files, converts them to binary and I need to store them in memory so that later I can train a neural network with the binary values.

private void btnLoadTrainingData_Click(object sender, EventArgs e)
        {
            string[] filenames = Directory.GetFiles(@"C:\temp\test", "*.png", SearchOption.AllDirectories);

            foreach (string f in filenames)
            {
                Console.WriteLine(getFirstChar(Path.GetFileName(f)));
                img = (Bitmap)Bitmap.FromFile(f);                
                c = new OCRChar(f,convertToBinary(img));               
            }
            //img.Dispose();
        }



如何列出我已存储的所有项目?也许使用一个foreach循环?这不起作用



How can I list all the items I have stored? maybe using a foreach loop? this is not working

foreach (OCRChar c )
            {
            Console.WriteLine(c.binVal);
            }

推荐答案

我认为您需要返回一步,并查看列表等内容.

"c"不是列表.我可以告诉您,因为您要为其分配一个对象的单个实例:
I think you need to go back a step, and review what a list and so forth is.

"c" is not a list. I can tell this, because you are assigning a single instance of an object to it:
c = new OCRChar(f,convertToBinary(img));


因此,它不能在foreach循环中用作可枚举的值:


Therefore, it can''t be used as an enumerable value in a foreach loop:

foreach (OCRChar c )
            {
            Console.WriteLine(c.binVal);
            }

(忽略foreach的语法看起来不是这样).

退后一步,看看您要做什么.
在您的主窗体中添加一个可枚举的对象,因此保留所有OCRChar实例-我建议OCRChar对象的列表是一个不错的选择:

(Ignoring that the syntax for foreach doesn''t look like that).

Step back, and look at what you are trying to do.
Add an emumerable object to your main form, so hold all the OCRChar instances - I would suggest that a List of OCRChar objects would be a good choice:

private List<OCRChar> ocrFiles = new List<OCRChar>();


现在,当您创建一个新实例时,将其添加到列表中:


Now, when you create a new instance, add it to the list:

foreach (string f in filenames)
{
    Console.WriteLine(getFirstChar(Path.GetFileName(f)));
    img = (Bitmap)Bitmap.FromFile(f);
    ocrFiles.Add(new OCRChar(f,convertToBinary(img)));
}


然后,您可以稍后遍历它们:


Then you can iterate through them later:

foreach (OCRChar c in ocrFiles)
{
    Console.WriteLine(c.binVal);
}



目前,您似乎正在通过猜测并希望它起作用来尝试进行编码-这不是可行的策略!我强烈建议您回到一两个阶段,并研究基础知识-在进一步研究之前,请先对它们有一个扎实的了解.您正步入一个越来越艰苦的领域,除非您对简单的东西比现在了解的多,否则我看不到您会做更多的事情来使自己感到困惑和沮丧.



At the moment, it looks like you are trying to code by guessing and hoping it works - that isn''t a viable strategy! I strongly recommend that you go back a stage or two and look at the basics - get a good solid grasp of them first before you go much further. You are venturing into harder and harder territory and I can''t see that you are going to do more than confuse and frustrate yourself unless you understand the simple stuff a lot better than you appear to now.


这篇关于使用类和构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆