Question Created my first class library - looking for advice

ConsKa

Well-known member
Joined
Dec 11, 2020
Messages
140
Programming Experience
Beginner
I hope it is ok to ask this sort of question, if it is not appropriate please delete the post and I will know what is appropriate.

I am learning C# and I am trying to 'refactor' my code (which is one huge 1,000 line Win Form. So I created my first class library and I just wanted to run it by people that know a LOT more than I do to see if there are improvements, or if I have misunderstood the way to implement it - which is entirely possible.

I created a Class called FileName.cs to handle all of the filenames that I use throughout the Win Form.

C#:
public class FileName
    {
        public string Path { get; set; }
        public string Date { get; set; }
        public string Name { get; set; }

        public string CombinedName(string name)
        {
            Path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\";
            Date = DateTime.Now.ToString("yyyyMMdd");
            this.Name = name;
            return Path + Name;
        }

    }

I know that Date has become superfluous in the above code - it wasn't always, but I am testing a different naming convention to improve functionality.

At the very top of my Form1.cs I refer to the objects that I need to create:

C#:
FileName DataSetOne = new FileName();
        FileName DataSetTwo = new FileName();
        FileName DataSetFull = new FileName();
        FileName DataSetExcel = new FileName();

Then whenever I need to refer to a file I use a single line of code - for each file:

C#:
string DS1 = DataSetOne.CombinedName("DataSet1.txt");
            string DS2 = DataSetTwo.CombinedName("DataSet2.txt");
            string DF1 = DataSetFull.CombinedName("fulldataset.txt");

While technically, I was using a single line of code anyway (just setting the variable to a string of "Path + Name" - this has helped me enormously when it came to changing the naming convention - which I just did....I just changed the class method and all of the names were changed throughout, so there is real benefit to doing it this way and I would like to take another step in refactoring the code into classes, but before I go ahead and do that I wanted to seek some advice to make sure I wasn't completely misunderstanding the basics.

Self teaching, easy to teach yourself mistakes.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,386
Location
Chesapeake, VA
Programming Experience
10+
While teaching yourself, take time to learn about the .NET Framework naming conventions.

 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,386
Location
Chesapeake, VA
Programming Experience
10+
As for that class design, I personally would recommend something like:
Immutable type:
public class FileName
{
    public string Path { get; }
    public string Date { get; }
    public string Name { get; }
    public string FullName { get; }

    public FileName(string name)
    {
        Path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\";
        Date = DateTime.Now.ToString("yyyyMMdd");
        Name = name;
        FullName = Path.Combine(Path, Name);
    }
}

The above would be even easier with the C# 9.0 record type.
 

ConsKa

Well-known member
Joined
Dec 11, 2020
Messages
140
Programming Experience
Beginner
Thanks for that, I was here earlier and saw it but work was busy.

I was aware of camel and Pascal, just not really 100% on it so cheers for the guide.

Looking at your example, I would still need to

C#:
string dataSet1 = DataSetOne.FullName("DataSet1.txt")

To assign the value to the string for the rest of the code?
 
Top Bottom