HiveBrain v1.2.0
Get Started
← Back to all entries
snippetcsharpMinor

Simple class to create csv

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
csvsimpleclasscreate

Problem

As part of a solution I have the following class createCSV. It is working as expected but I'm very much aware of my lack of experience with c# and OO programming: are there any obvious potential problems or simple refactors?

(note: the use of a guid is a user requirement)

using System.Text;
using System.IO;
using System.Data;
using System;
using System.Collections.Generic;

namespace createCSV
{
  class Csv
  {

    private string guid;

    public Csv(string aGuid)
    {
      guid = aGuid;
    }

    public void CreateCSV(DataTable dt,string aDestPath, string aTitle)
    {

      string filePath = string.Concat(aDestPath, aTitle, @"-", this.guid, @".csv");
      string delimiter = ",";

      StringBuilder sb = new StringBuilder();
      List CsvRow = new List();

      //write headers
      foreach(DataColumn c in dt.Columns)
      {
        CsvRow.Add(c.ColumnName.ToString());
      }
      sb.AppendLine(string.Join(delimiter,CsvRow));

      //write data
      foreach(DataRow r in dt.Rows)
      {
        CsvRow.Clear();

        //go through each column adding to a list of strings
        foreach(DataColumn c in dt.Columns)
        {
          CsvRow.Add(r[c.ColumnName].ToString());
        }

        sb.AppendLine(string.Join(delimiter,CsvRow));
      }

      File.WriteAllText(filePath,sb.ToString());

      Console.WriteLine();
      Console.WriteLine(String.Concat(@"just completed: ",aTitle,@"-",this.guid,@".csv"));
    }

  }
}

Solution

Before you do other things you should separate and encapsulate, this means you should put certain logic into their own methods that specialize in doing only one thing. If you put e.g. the loops in such method methods, you won't need any comments. Here's an example of how it could be done with C# 7. Also notice the changed names. There's no need for the a/an/the prefixes.

This is just a start and there is much more to improve. I left it out intentionally.

public void CreateCsv(DataTable data, string path, string title)
{
    string CreateFileName() => string.Concat(path, title, @"-", guid, @".csv");

    void WriteHeader(StringBuilder csv)
    {
        var header = new List();
        foreach (var column in data.Columns.Cast())
        {
            header.Add(column .ColumnName);
        }
        csv.AppendLine(string.Join(Delimiter, header));
    }

    void WriteData(StringBuilder csv)
    {
        foreach (var row in data.Rows.Cast())
        {
            var values = new List();
            foreach (var column in data.Columns.Cast())
            {
                values.Add(row[column.ColumnName].ToString());
            }
            csv.AppendLine(string.Join(Delimiter, values));
        }
    }

    string WriteCsv() 
    {
        var csv = new StringBuilder();  
        WriteHeader(csv);
        WriteData(csv);
        return csv.ToString();
    }           

    File.WriteAllText(CreateFileName(), WriteCsv());

    Console.WriteLine();
    Console.WriteLine(String.Concat(@"just completed: ", title, @"-", this.guid, @".csv"));
}

Code Snippets

public void CreateCsv(DataTable data, string path, string title)
{
    string CreateFileName() => string.Concat(path, title, @"-", guid, @".csv");

    void WriteHeader(StringBuilder csv)
    {
        var header = new List<string>();
        foreach (var column in data.Columns.Cast<DataColumn>())
        {
            header.Add(column .ColumnName);
        }
        csv.AppendLine(string.Join(Delimiter, header));
    }

    void WriteData(StringBuilder csv)
    {
        foreach (var row in data.Rows.Cast<DataRow>())
        {
            var values = new List<string>();
            foreach (var column in data.Columns.Cast<DataColumn>())
            {
                values.Add(row[column.ColumnName].ToString());
            }
            csv.AppendLine(string.Join(Delimiter, values));
        }
    }

    string WriteCsv() 
    {
        var csv = new StringBuilder();  
        WriteHeader(csv);
        WriteData(csv);
        return csv.ToString();
    }           

    File.WriteAllText(CreateFileName(), WriteCsv());

    Console.WriteLine();
    Console.WriteLine(String.Concat(@"just completed: ", title, @"-", this.guid, @".csv"));
}

Context

StackExchange Code Review Q#154821, answer score: 3

Revisions (0)

No revisions yet.