C
C#2y ago
XD

❔ I tried to substract two TimeSpan objects but nothing happened

I have been trying to add to add a goal feature in my application (a coding tracker), but I haven't been able to update the progress of the goal of the user What I'm trying to do here is: 1. Store in a variable each coding session which has been started after the first goal was added by the user (example, user added his goal 1 hour ago, so then every coding session after the user added his goal, will be stored) 2. Add to GoalProgress all the durations of those sessions 3. Return GoalProgress to use it later
public static TimeSpan DisplayGoal()
{
TimeSpan GoalProgress = TimeSpan.Zero;

var csSinceGoalWasAdded = CodingSessions.Where(cs => cs.StartTime >= (Goals.First().AddedDate)).ToList();

foreach (var j in csSinceGoalWasAdded)
{
GoalProgress += j.Duration;
}

return GoalProgress;
}
public static TimeSpan DisplayGoal()
{
TimeSpan GoalProgress = TimeSpan.Zero;

var csSinceGoalWasAdded = CodingSessions.Where(cs => cs.StartTime >= (Goals.First().AddedDate)).ToList();

foreach (var j in csSinceGoalWasAdded)
{
GoalProgress += j.Duration;
}

return GoalProgress;
}
Then here comes the problem:
Console.WriteLine($"\nWelcome to my Coding Tracker (...)")
if (CodingController.Goals.Count != 0)
{
Console.WriteLine($"Reminder: You are {CodingController.Goals.First().GoalValue - CodingController.DisplayGoal()} hours away from completing your last goal!");
}
Console.WriteLine($"\nWelcome to my Coding Tracker (...)")
if (CodingController.Goals.Count != 0)
{
Console.WriteLine($"Reminder: You are {CodingController.Goals.First().GoalValue - CodingController.DisplayGoal()} hours away from completing your last goal!");
}
I tried to calculate the progress by substracting GoalProgress to GoalValue, but the value of that operation will always be equal to GoalValue (the original goal the user inserted) This is the void in which the values of my Goal model are inserted into my SQLite database:
8 Replies
XD
XDOP2y ago
Pastebin
public static void SetGoal() { var timeUnit = Use...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
XD
XDOP2y ago
This is my repo in case you'd rather check my code there: https://github.com/JsPeanut/CodeReviews.Console.CodingTracker
GitHub
GitHub - JsPeanut/CodeReviews.Console.CodingTracker
Contribute to JsPeanut/CodeReviews.Console.CodingTracker development by creating an account on GitHub.
Anu6is
Anu6is2y ago
Did you debug your code? Does DisplayGoal actually return a non zero value?
XD
XDOP2y ago
XD
XDOP2y ago
Set a breakpoint right here:
Console.WriteLine($"Reminder: You are {CodingController.Goals.First().GoalValue - CodingController.DisplayGoal()} hours away from completing your last goal (15 hours of coding time)!");
Console.WriteLine($"Reminder: You are {CodingController.Goals.First().GoalValue - CodingController.DisplayGoal()} hours away from completing your last goal (15 hours of coding time)!");
It doesn't even appear lol
Anu6is
Anu6is2y ago
If you set a breakpoint on the WriteLine it's not going to show...you need to step into the method and see what it returns
DaVinki
DaVinki2y ago
We won't debug for you but after skimming the code, there are some possible places things could've went wrong even before your DisplayGoal method In your GetAllRecords method, do your rows actually have a valid duration value? In your ParseGoalIntoTimeSpan, do your goals have a valid time too? Do you actually have any coding sessions beginning after the goal is added? Do your coding sessions actually have a duration greater than 0?
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.

Did you find this page helpful?