Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Cannot use Variable in Method

I have a problem to use my radius im the Method GetCircleArea(). Please help. I am sitting here and dont know what to do anymore.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Programmieraufgabe_5
{
    class Program
    {
        static void Main(string[] args)
        {

            Console.WriteLine("Gebe einen Radius ein: ");
            double rad = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("Die Fläche eines Kreises mit dem Radius {0}cm beträgt {1}cm²", rad, GetCircleArea());

            Console.ReadKey();

        }

        static double GetCircleArea()
        {
            double area = Math.PI * radius * radius;
            return area;
        }
    }
}

Thank you. For the answers!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

the answer to your question is that you cannot access the variables of other Methods in any Method you want that simple. The easiest way to do this is to add a parameterList to the GetCircleArea Method.

so write
static double GetCircleArea(double radius) instead of static double GetCircleArea().

also when you use the method write what for a parameter you want to give to the Method. In your case it would be the rad variable from Main. So you can write

Console.WriteLine("Die Fläche eines Kreises mit dem Radius {0}cm beträgt {1}cm²", rad, GetCircleArea(rad));

P.S. if you want that you can put the radius in the same Line with the Text Gebe einen Radius ein: you can use Console.Write() instead of Console.WriteLine(), because with Console.Write you can proceed in the same Line. Then you use Console.ReadLine() and switch the Line nevertheless.

I hope i could help you 🙂

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading