简体   繁体   English

C#中通过ref传递的参数

[英]parameters passing by ref in C#

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 ?我是编程新手,现在我正在做一些练习,但是,我无法完成一项任务(或者我不明白),我被困在数字 (3) 上,你能帮我吗? Here's exercise and my code:这是练习和我的代码:

(1)Should have a separate method for conversion (1)应有单独的转换方法

(2)Should have a separate method called ConvertSecondsToHoursMinutesSeconds (2)应该有一个单独的方法叫做ConvertSecondsToHoursMinutesSeconds

(3)Should have one int parameter passed by value and three int parameters passed by ref (3)应该有1个值传递的int参数和3个ref传递的int参数

(4)Should correctly convert seconds to hours, minutes and seconds (4)应正确将秒转换为小时、分钟和秒

  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}";


    }
}

You need to pass the ref parameters received to the other method you are calling for them to receive correct values.您需要将收到的 ref 参数传递给您正在调用的其他方法,以便它们接收正确的值。 Local variables usually fail to exist once a method returns.一旦方法返回,局部变量通常不存在。

private void ConvertSecondsToHoursMinutesSecondsMethod(long totalSeconds, ref long secs, ref long mins) 
{
    hours = totalSeconds / 3600;
    long v = totalSeconds % 3600;
    mins = v / 60;
    secs = v % 60;
}

private void conversion()
{
    long hours, secs, mins;
    long seconds = Convert.ToInt64(userInputLabel.Text);
    ConvertSecondsToHoursMinutesSecondsMethod(seconds, ref hours, ref secs, ref mins);

    outputLabel.Content = $"{hours} {mins} {secs}";
}

Try this尝试这个

Also have a look at https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/ref for the correct way to use ref另请查看https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/ref以了解使用 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}";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM