0

I am trying to write a basic program, with two classes. I can manage fine with one class:

    public static void Main (string[] args)
    {
        double radius;
        double height;
        double volume;
        {
            System.Console.WriteLine ("Enter radius");
            radius = double.Parse(System.Console.ReadLine());
            System.Console.WriteLine (radius);
            System.Console.WriteLine ("Enter height");
            height = double.Parse(System.Console.ReadLine());
            System.Console.WriteLine (height);

            volume = Math.PI * radius * radius * height;
            System.Console.WriteLine (volume);

But I am unable to use ref from one class to another, for example, I tried removing the final volume calculation and making this seperate class:

class Calculation
{
    double radius2 = 0.00;
    double height2 = 0.00;
    double volume2
    radius2(ref radius);
    height2(ref height);
    volume2 = Math.PI * radius2 * radius2 * height2;
}

But it doesn't work at all. I'm quite new to C#, thanks for any help you can offer!

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
  • what exactly is the purpose of the code here? and why doesn't it work? – jgon Dec 08 '13 at 07:21
  • The first part of the posted code does work, but I am trying to make it work across two seperate classes. I want to have one class, where the user enters their data, and one class where the program runs the calculation. It's just meant to be a simple volume of a cylinder calculation to get me to grips with ref. –  Dec 08 '13 at 07:23
  • 1
    You generally don't use `ref` unless you have a specific reason to. – BoltClock Dec 08 '13 at 07:24
  • 1
    ref is not what you think it is, it isn't a reference to radius or height anytime you want to call upon/use those values. – Brian Ogden Dec 08 '13 at 07:25
  • The class you modified won't compile because the fields `radius2` and `height2` have the same names with the methods `radius2` and `height2`. – King King Dec 08 '13 at 07:25
  • not to mention the methods have no return types and the `double volume2` line has no ; – jgon Dec 08 '13 at 07:26
  • 2
    I have added the answer you need, I hope this isn't homework because it is got to be about the last week of school and you are really not understanding a myriad of basic computer programming concepts. Study the difference between value types and reference types and also study scope. – Brian Ogden Dec 08 '13 at 07:34

4 Answers4

2

Okay. So.

How I would write this calculation class:

public class Calculation
{
    public readonly double Result;
    public Calculation(double radius, double height)
    {
        Result = Math.PI * radius * radius * height;
    }
}

Then you would use it like this:

Calculation myCalc = new Calculation(myRadius, myHeight);
double volume = myCalc.Result;

As for what ref is, take a look at the comment on the question here. I'm bad with explanations

Community
  • 1
  • 1
jgon
  • 698
  • 4
  • 11
0
using System;

namespace ConsoleApplication1
{
    class Calculation
    {
        public double Radius { get; set; }
        public double Height { get; set; }

        public double GetVolume()
        {
           return Math.PI * Radius * Radius * Height;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            double radius;//not used but left for your understanding, you didn't need radius2 in your Calculation class, you could have used the name radius, this is why you need to study variable scope
            double height;//not used but left for your understanding, same reason as radius

            Calculation calc = new Calculation();

            Console.WriteLine("Enter radius");
            calc.Radius = double.Parse(Console.ReadLine());
            Console.WriteLine(calc.Radius);
            Console.WriteLine("Enter height");
            calc.Height = double.Parse(Console.ReadLine());
            Console.WriteLine(calc.Height);


            Console.WriteLine(calc.GetVolume());
            Console.ReadLine();
        }
    }
}
Brian Ogden
  • 18,439
  • 10
  • 97
  • 176
0

I would suggest you to get basic knowledge about C#. however you can do this in this way ?

class Program
{
    public static void Main(string[] args)
    {
        double radius = 0.00;
        double height = 0.00;
        double volume = 0.00;
        Calculator calObject = new Calculator();

        System.Console.WriteLine("Enter radius");
        radius = double.Parse(System.Console.ReadLine());
        System.Console.WriteLine(radius);
        System.Console.WriteLine("Enter height");
        height = double.Parse(System.Console.ReadLine());
        System.Console.WriteLine(height);


        volume = calObject.FindVolumne(radius, height);
        System.Console.WriteLine(volume);
        Console.ReadLine();
    }
 }

public class Calculator
{

    public double FindVolumne(double radius, double height)
    {
        double volume = 0.00;
        volume = Math.PI * radius * radius * height;
        return volume;
    }
}
shujaat siddiqui
  • 1,527
  • 1
  • 20
  • 41
0

Although you are using Calculation as a class, you ultimately are attempting to define an operation on a cylinder. A cylinder can be expressed by a height and radius (properties), and is a useful construct for exposing methods (like "Get the volume of the cylinder.").

Let's begin there and define the Cylinder class:

class Cylinder
{
    public double Radius;
    public double Height;
    public double GetVolume()
    {
         return Math.PI * Radius * Radius * Height;
    }
    // Constructor
    public Cylinder(double radius, double height)
    {
         this.Radius = radius;
         this.Height = height;
    }
}

Note that this is a simple class with two fields (radius, height), a method (GetArea), and a constructor. It encapsulates the data needed to construct and perform operations on a cylinder. All of these members are public, which means that they can be accessed by programs that use the class. (Private members, by contrast, can only be used inside the class.)

Now we can build a program that makes use of the class:

class Program
{
    static Cylinder GetCylinderFromUser()
    {
        double radius, height;
        Console.Write("Enter radius: ");
        radius = double.Parse(Console.ReadLine());

        Console.Write("Enter height: ");
        height = double.Parse(Console.ReadLine());

        return new Cylinder(radius, height); 
    }

    static void Main()
    {

        Cylinder c = GetCylinderFromUser();

        Console.WriteLine("Created cylinder with height={0} and radius={1}",
                          c.Height, // replaces {0} with height of c
                          c.Radius  // replaces {1} with radius of c
                       );

        double volume = c.GetVolume();
        Console.WriteLine("The volume of the cylinder is {0}.", volume);
        Console.ReadLine();

    }
}

This is a very basic example, but note that the program is broken into small pieces that are then coupled together. The classes separate out the user interface (getting the user-provided input) from the calculation logic. The Cylinder class can be reused in other contexts, and internal logic can be adjusted without affecting other parts of the code.

drf
  • 8,461
  • 32
  • 50