Reputation:
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!
Upvotes: 0
Views: 1085
Reputation: 8699
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.
Upvotes: 0
Reputation: 1577
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;
}
}
Upvotes: 0
Reputation: 19212
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();
}
}
}
Upvotes: 0
Reputation: 688
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
Upvotes: 2