Need Help to understand this code...

jimgreener

New member
Joined
Nov 22, 2017
Messages
1
Programming Experience
Beginner
I am new to programming...I came across this code...I am not clear what it does - Can someone help :
Sorry if I posted the wrong question to wrong forum -
public class TimeSlot
{

public ushort Time { get; set; }

public TimeSlot(ushort val)
{
  Time = val;
}
public TimeSlot(string time)
{
  ushort uTime;
  if (ushort.TryParse(time, out uTime))
  {
    Time = ushort.Parse(removeAllWhiteSpaces(time));
  }
  else throw new ArgumentException($"Invalid Format of Time slot; Use  hh:mm am| hh:mm pm");
}

public static TimeSlot CreateTimeSlot(string time)
{
return new TimeSlot(time);
}
private string removeAllWhiteSpaces(string str)
{
str = str.Trim();
str = str.Replace(" ", "");
str = str.Replace(":", "");
str = str.Replace("AM", "");
str = str.Replace("am", "");
str = str.Replace("Am", "");
str = str.Replace("aM", "");
str = str.Replace("PM", "");
str = str.Replace("pm", "");
str = str.Replace("Pm", "");\
str = str.Replace("pM", "");
return str;
}
}
 
Last edited by a moderator:
Not much to it, you call "CreateTimeSlot" and pass it a time in "hhmm" format and it strips out the invalid characters (could be done in a shorter/cleaner way too) and it outputs a "TimeSlot" object.
 
One has to wonder why you came across that code in the first place if you have no idea what it does. What are you trying to achieve and why do you think that code is relevant?
 

Latest posts

Back
Top Bottom