C
C#2y ago
Krimo

❔ class library that opens file and executes calculations?

12 Replies
Krimo
Krimo2y ago
I got an assignment to create a nuget package from a class library. So I got this code that opens a text file and make some calculations of characters from the textfile. My question is possible to create a class library that does that. I'm a beginner so bare with me I want the file path way as the variable
Brady Kelly
Brady Kelly2y ago
It's definitely possible and you should actually factor your logic out of Main anyway. First just add a new class and put the logic in a method on that class, and call that method from Main You will also want to move the WriteLine and ReadLine out of your new method and keep those in Main You can pass the path to your new method as an argument to your new method
Krimo
Krimo2y ago
Alright not sure how I would do that as I trying to get a hold on this coding. Is there any examples?
Brady Kelly
Brady Kelly2y ago
Well you could start by completing the quick tutorials that teach you the very basics of C#. $helloworld You aren't going to get far if you don't know how to create a class and method
Brady Kelly
Brady Kelly2y ago
But I'll give you a clue. It will look something like
public class AcgtCalculator
{
public double GetFileGcCount(string filePath)
{
// Read and process your file here
}
}
public class AcgtCalculator
{
public double GetFileGcCount(string filePath)
{
// Read and process your file here
}
}
Krimo
Krimo2y ago
Okay thanks, 50% chance now that I solve it before going in to extratime Okay it didn't work for me having the method as public double. Had to change it to public static void
Brady Kelly
Brady Kelly2y ago
I meant you are supposed to adjust your code for public double, not just copy and paste it straight from Main
Krimo
Krimo2y ago
Okay should the streamreader be inside those double brackets?
Brady Kelly
Brady Kelly2y ago
Now I'm really spoon-feeding you and you really should learn to figure out basics for yourself by following tutorials and examples. But it's Monday and I'm feeling fresh and kind.
public double GetFileGcCount(string filePath)
{
using(var fastaReader = new StreamReader(filePath))
{
// Loop and if statement
}
gc = ....
return gc;
}
public double GetFileGcCount(string filePath)
{
using(var fastaReader = new StreamReader(filePath))
{
// Loop and if statement
}
gc = ....
return gc;
}
You shouldn't be using WriteLine in your class, it should be agnostic as to how it's results will be used. Then you have something like this:
public static void Main(string[] args)
{
var fastaFilePath = @"C:\....";
var calc = new AcgtCalculator();
var gc = calc.GetFileGcCount(fastaFilePath);
Console.WriteLine($"gc content: {gc}");
}
public static void Main(string[] args)
{
var fastaFilePath = @"C:\....";
var calc = new AcgtCalculator();
var gc = calc.GetFileGcCount(fastaFilePath);
Console.WriteLine($"gc content: {gc}");
}
This way you lose printing all the individual values, but do you really need that? We can work something out for that but it gets more complex. Notice how the logic in your class is independent of how the result is displayed, and how the console app, i.e. the Program class isn't aware of how gc is calculated.
Krimo
Krimo2y ago
Thanks a lot. I will look into it more deeper tomorrow. Big up
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.