I'm new to programming and right now I'm doing some exercises, however, I couldn't complete one task (or I didn't understand), I'm stuck at number (3), can you help me ? Here's exercise and my code:
(1)Should have a separate method for conversion
(2)Should have a separate method called ConvertSecondsToHoursMinutesSeconds
(3)Should have one int parameter passed by value and three int parameters passed by ref
(4)Should correctly convert seconds to hours, minutes and seconds
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
conversion();
}
private void ConvertSecondsToHoursMinutesSecondsMethod(long totalSeconds)
{
long hours, mins, secs, v;
hours = totalSeconds / 3600;
v = totalSeconds % 3600;
mins = v / 60;
secs = v % 60;
}
private void conversion(ref long hours, ref long secs, ref long mins)
{
long seconds = Convert.ToInt64(userInputLabel.Text);
ConvertSecondsToHoursMinutesSecondsMethod(seconds);
outputLabel.Content = $"{hours} {mins} {secs}";
}
}
Try this
Also have a look at https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/ref for the correct way to use ref
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Conversion();
}
private void ConvertSecondsToHoursMinutesSecondsMethod(int totalSeconds, ref long hours, ref long secs, ref long min)
{
long v;
hours = totalSeconds / 3600;
v = totalSeconds % 3600;
min = v / 60;
secs = v % 60;
}
private void Conversion()
{
long hours = 0;
long secs = 0;
long mins = 0;
int seconds = Convert.ToInt32(userInputLabel.Text);
ConvertSecondsToHoursMinutesSecondsMethod(seconds, ref hours, ref mins, ref secs);
outputLabel.Content = $"{hours} {mins} {secs}";
}