Question

I am new to C# and trying to make a volume cal that allows you to choose the shape you want to find the volume of I am trying to do that with read line. I am getting Cannot convert type 'String' to 'AreaCal.VolumeSphere' at the read line in static void main

using System;

namespace AreaCal
{

    class VolumeSphere
    {
        double pi = 3.14159265359;
        double ft = 1.333333333333333333333333333333333333;
        double r;
        public void Details()
        {
            Console.WriteLine("Volume of a Circle");
            Console.WriteLine ("Type Radius");
            r = Convert.ToInt32 (Console.ReadLine ());

        }
        public double GetVolume()
        {
            return ft * pi * r * r * r;
        }
        public void Display()
        {
            Console.WriteLine ("Volume: {0}", GetVolume());
        }

    }
    class MainClass
    {
        public static void Main (string[] args)
        {
            VolumeSphere Sphere = new VolumeSphere ();
            Sphere = (Console.ReadLine ());
            if (Sphere != null)
                Console.WriteLine ("awwww");

        }
    }
}
Was it helpful?

Solution

In reading your question you mention that you are trying to select multiple calculators, in your example you are trying to assign your keyboard input to your VolumeSphere Object. Here is a quick and dirty implementation of a menu for your calculator, you can play with it and expand it to meet your needs.

class MainClass
{
    static void Main(string[] args)
    {
        int value;
        bool run;

        run = true;
        while(run)
        {
            Console.WriteLine("Calculator Menu");
            Console.WriteLine("1: Compute Volume of a Sphere");
            Console.WriteLine("2: Compute Something else");
            Console.WriteLine("etc,etc,etc");

            if (int.TryParse(Console.ReadLine(), out value))
            {
                switch (value)
                {
                    case 1:
                        VolumeSphere Sphere = new VolumeSphere();
                        Sphere.Details();
                        Console.WriteLine(Sphere.GetVolume());
                        Sphere.Display();
                        break;
                    case 2:
                        break;
                    case 3:
                        break;

                    default:
                        Console.WriteLine("GoodBye");
                        run = false;
                        break;

                }

            }
        }
        Console.WriteLine("Press Any Key to Exit");
        Console.ReadLine();
    }
}

OTHER TIPS

Sphere is an object of class VolumeSphere but you are assigning a string Console.ReadLine here that is wrong. Probably you wanted to do something like:

VolumeSphere Sphere = new VolumeSphere();
Sphere.Details();
Sphere.Display();

But your requirement is not clear. So please provide more details on it. And also consider using constructor to pass radius value to your VolumeSphere class.

VolumeSphere Sphere = new VolumeSphere();
Sphere = (Console.ReadLine ());

This is throwing the error. Console.Readline() returns a string read in from standard input. You are trying to assign this to the variable Sphere, which you declared to be of type VolumeSphere. I am not sure what you are reading from standard input, but I assume it is radius.

You need to do this instead;

string radius = Console.ReadLine();
VolumeSphere Sphere = new VolumeSphere(Convert.ToDouble(radius));

This passes the string parameter converted to a double to the VolumeSphere constructor, which you are going to define like this inside of class VolumeSphere:

public VolumeSphere(double r)
{
   this.r = r;
}

You should also remove the Details method and put its writelines in the main before you read your input because it is better to encapsulate your models and user interface.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top