I am relatively new to C#, having done most of my previous programming in VB6. I know C# is much more explicitly typed than VB, but I’m hoping that there is a solution for my problem.
I am working on a project that is designed to open, parse, validate, and eventually edit 5 different CSV files that are used as input to an application we use. Manually manipulation of the CSV files is what is done now, but it is difficult for most users because of lack of documentation and support from the original developer. My goal is to build a GUI that will permit users to directly edit the fields and create a new CSV file to use as an import. Here’s the basic structure now:
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}
}
}
The challenge is reading the data from the CSV into each of the dictionaries. Right now that is accomplished with 5 different functions (actually one function overloaded 5 times)
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
}
The code for opening and parsing the CSV file is all virtually identical, differing only by the Type for the dictionary. So when I make a change to this function, I have to make sure I make identical changes to the other 4 functions, and I’m worried that it will make maintaining the code in the future more of a problem. Is there any possible way I can create a single function with no overloads where I can pass the type as a parameter to the function?
public Dictionary<string,[UnknownType]>ReadFile(string file, [UnknownType] typ)
{
//Open and Parse File and read into class specified by typ
}
Object Orientation my Friend. Coming from a VB6-perspective that might not be what you are use to. But instead of having File1 -> File5, why don’t you have a “CVSFile”-object which you then, if you really must, derive from. Which would help you in Many ways.
Polymorphism
Check out MSDN on how to use Polymorphism in C#
Snippet from MSDN:
Edit
Just to clearify a bit
And if you like to use virtual methods instead:
In this case the result will be this
All examples are take from MSDN
How to apply this on your example
Let’s say that you have a LoadCSV-method, instead of having 5 different methods that returns each object, you can easily just say “Hey I will return a CVSFile, don’t you care about anything else!”.
There are a lot of good turorials out there, and the “Programming for kids” have the Best illustrations on the fundamentals. Check this out: Object Orientation For Kids ( No offense. )