C#类问题 [英] C# Class Question

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

问题描述

自从我编写c#代码以来已经有一段时间了,所以我有点生疏了。我需要一个关于如何编写和使用课程的快速进修课程。我正在使用的代码工作得很好,但它只是在一个文件中,Program.cs和为了使这个东西符合OOP
我想把它的部分移动到他们自己的类。我需要处理的第一个任务是设置一个字符串数组的值,该数组将包含文件夹中所有* .pdf文件的列表。

It's been a while since I've written c# code so I'm a bit rusty. I need a quick refresher course on how to write and use a class. The code I'm using works great but it's all in a single file, Program.cs and in order to make this thing compliant with OOP I want to move the parts of it to their own class. The first task I need to handle is to set the value for a string array which will hold a list of all the *.pdf files in a folder.

string[] filelist = new GetFiles();

这是我现在的课程......

Here is the class as I have it now...

public class GetFiles { public GetFiles() { DirectoryInfo dirInfo = new DirectoryInfo(@"C:\MyFolder\"); FileInfo[] fileInfos = dirInfo.GetFiles("*.pdf"); ArrayList list = new ArrayList(); foreach (FileInfo info in fileInfos) { // HACK: Just skip the protected samples file... if (info.Name.IndexOf("protected") == -1) list.Add(info.FullName); }

}

我想做的是让班级使用我可以运行的对象通过代码的其他部分,但是我上次用C#编写代码已经很久了。任何建议都将不胜感激。

What I'd like to do is to have the class use an object which I can run through other parts of the code, but it's been so long since I last wrote code in C#. Any advice would be appreciated.

推荐答案

public GetFiles()是构造函数。你应该为此实现一个方法:

public GetFiles() is the constructor. You should implement a method for this:

public class GetFiles { public ArrayList Get(string path) { DirectoryInfo dirInfo = new DirectoryInfo(path); FileInfo[] fileInfos = dirInfo.GetFiles("*.pdf"); ArrayList list = new ArrayList(); foreach (FileInfo info in fileInfos) { // HACK: Just skip the protected samples file... if (info.Name.IndexOf("protected") == -1) list.Add(info.FullName); } return list;

}
}

并像这样使用它:

var getFiles = new GetFiles();
var filelist = getFiles.Get(@"c:\MyFolder\");

问候,克里斯


这篇关于C#类问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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