我对C#相对较新,在VB6中完成了我以前的大部分编程工作.我知道C#比VB更明确地键入,但我希望有一个解决我的问题的方法.
我正在开发一个项目,旨在打开,解析,验证并最终编辑5个不同的CSV文件,这些文件用作我们使用的应用程序的输入.手动操作CSV文件是现在所做的,但由于缺乏原始开发人员的文档和支持,大多数用户都很难.我的目标是构建一个GUI,允许用户直接编辑字段并创建一个新的CSV文件用作导入.这是现在的基本结构:
public class Dataset
{
public Dictionary<string,File1> file1 = new Dictionary<string,File1>();
public Dictionary<string,File2> file2 = new Dictionary<string,File2>();
public Dictionary<string,File3> file3 = new Dictionary<string,File3>();
public Dictionary<string,File4> file4 = new Dictionary<string,File4>();
public Dictionary<string,File5> file5 = new Dictionary<string,File5>();
public void SelectFiles()
{
//User specifies which file(s) are to be opened (default is all 5 files)
}
public class File1
{
public string Field_1 {get ; set}
public string Field_2 {get ; set}
.
.
.
public string Field_10 {get ; set}
}
public class File2
{
public string Field_1 {get ; set}
public string Field_2 {get ; set}
.
.
.
public string Field_31 {get ; set}
}
public class File3
{
public string Field_1 {get ; set}
public string Field_2 {get ; set}
.
.
.
public string Field_57 {get ; set}
}
public class File4
{
public string Field_1 {get ; set}
public string Field_2 {get ; set}
.
.
.
public string Field_68 {get ; set}
}
public class File5
{
public string Field_1 {get ; set}
public string Field_2 {get ; set}
.
.
.
public string Field_161 {get ; set}
}
}
Run Code Online (Sandbox Code Playgroud)
挑战在于将CSV中的数据读入每个词典.现在用5种不同的功能完成(实际上一个功能重载5次)
public Dictionary<string,File1>ReadFile(string file)
{
//Open and Parse File #1, and store in File1 class and accessed by file1 dictionary
}
public Dictionary<string,File2>ReadFile(string file)
{
//Open and Parse File #2, and store in File2 class and accessed by file2 dictionary
}
public Dictionary<string,File3>ReadFile(string file)
{
//Open and Parse File #3, and store in File3 class and accessed by file3 dictionary
}
public Dictionary<string,File4>ReadFile(string file)
{
//Open and Parse File #4, and store in File4 class and accessed by file4 dictionary
}
public Dictionary<string,File5>ReadFile(string file)
{
//Open and Parse File #5, and store in File5 class and accessed by file5 dictionary
}
Run Code Online (Sandbox Code Playgroud)
打开和解析CSV文件的代码几乎完全相同,只是字典的类型不同.因此,当我对此函数进行更改时,我必须确保对其他4个函数进行相同的更改,并且我担心它将在未来维护代码更多的问题.有没有可能的方法我可以创建一个没有重载的单个函数,我可以将类型作为参数传递给函数?
public Dictionary<string,[UnknownType]>ReadFile(string file, [UnknownType] typ)
{
//Open and Parse File and read into class specified by typ
}
Run Code Online (Sandbox Code Playgroud)
面向对象我的朋友.来自VB6的观点,可能不是您的用途.但是没有File1 - > File5,你为什么不拥有一个"CVSFile"对象,如果你真的必须从中得到它.哪个会在很多方面帮助你.
多态性
来自MSDN的片段:
public class BaseClass
{
public void DoWork() { }
public int WorkField;
public int WorkProperty
{
get { return 0; }
}
}
public class DerivedClass : BaseClass
{
public new void DoWork() { }
public new int WorkField;
public new int WorkProperty
{
get { return 0; }
}
}
DerivedClass B = new DerivedClass();
B.DoWork(); // Calls the new method.
BaseClass A = (BaseClass)B;
A.DoWork(); // Calls the old method.
Run Code Online (Sandbox Code Playgroud)
编辑
只是为了清楚一点
使用new关键字时,将调用新的类成员而不是已替换的基类成员.这些基类成员称为隐藏成员.如果将派生类的实例强制转换为基类的实例,则仍可以调用隐藏类成员.
如果您想使用虚拟方法:
为了使派生类的实例完全从基类接管类成员,基类必须将该成员声明为虚拟成员.这是通过在成员的返回类型之前添加virtual关键字来实现的.然后,派生类可以选择使用override关键字而不是new来用自己的替换基类实现.
public class BaseClass
{
public virtual void DoWork() { }
public virtual int WorkProperty
{
get { return 0; }
}
}
public class DerivedClass : BaseClass
{
public override void DoWork() { }
public override int WorkProperty
{
get { return 0; }
}
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,结果将是这样
DerivedClass B = new DerivedClass();
B.DoWork(); // Calls the new method.
BaseClass A = (BaseClass)B;
A.DoWork(); // Also calls the new method.
Run Code Online (Sandbox Code Playgroud)
所有示例均来自MSDN
如何在您的示例中应用此功能
假设您有一个LoadCSV方法,而不是有5个不同的方法返回每个对象,您可以轻松地说"嘿我会返回一个CVSFile,你不关心其他任何事情!".
那里有很多很好的装饰,而"为孩子们编程"在基础知识上有最好的插图.看看这个:孩子的面向对象(没有冒犯.)
| 归档时间: |
|
| 查看次数: |
7637 次 |
| 最近记录: |