L
L
lexzus2016-02-20 20:07:43
C++ / C#
lexzus, 2016-02-20 20:07:43

Can't return C# value. Where is the mistake?

using System;

class Test
{

    public int Met(int x) //указываем тип возвращаемого значения и тип параметра
    {

        Console.WriteLine(x); // просто выводим значение параметра

        return x / 2;  // возвращаем x / 2

    }

}

class Test2
{

    static void Main()
    {

        Test obj = new Test(); // создание объекта

        obj.Met(6); // вызываем метод с аргументом 5

        int h = obj.Met(); // присваиваем возвращаемое значение переменной h. Вот здесь
                                   //подсвечивает имя метода. И выдает ошибку.
    }

}

If I just want to return - everything works.
I just want to call a method with an argument - it works too.
But if I want to call a method with an argument + return a value from this method - an error output ...
sorry if everything is obvious. I recently started learning the language.
Thanks in advance to everyone!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Nemiro, 2016-02-20
@lexzus

The parameter was not passed to the method:
Call the method Met, specify the value in the first parameter 123, pass the result to the variable h:

int h = obj.Met(123);
Console.WriteLine("Получен результат: {0}", h);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question