Hi Everyone,
I have this. But get strange output.
THX!!
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
VulpesRank #55 · 29.2K reputationPosted Nov 7, 2011, 11:39 PM EST
Console.Write("{0}: {1}", currentShape.ToString(), shape);
shape is a Shape[] and if you use this in a Console.WriteLine() the latter automatically calls the array's ToString() method which returns the full name of the type.
Dipal ChoksiRank #199 · 12K reputationPosted Nov 7, 2011, 12:43 PM EST
could you post some details about your code and the strange output that you get?
albert albertRank #2,683 · 519 reputationPosted Nov 7, 2011, 1:14 PM EST
but why u dont download it?
Because it has so: 8 classes. Then u can see what I mean
this is program:
[code]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ShapeInHeritanceApp
{
class Program
{
static void Main(string[] args)
{
Shape[] shape = new Shape[4];
shape[0] = new Circle(12, 13, 4);
shape[1] = new Cube(4);
shape[2] = new Triangle(44, 5);
shape[3] = new Square(4);
foreach (Shape currentShape in shape)
{
Console.Write("{0}: {1}", currentShape.ToString(), shape);
if (currentShape is TwoDimensionalShape)
{
TwoDimensionalShape twodimensionShape = (TwoDimensionalShape)currentShape;
Console.Write("{0}'s area is {1}\n", twodimensionShape.getName(), twodimensionShape.Area());
}
if (currentShape is ThreeDimensionalShape)
{
ThreeDimensionalShape threeDimensional = (ThreeDimensionalShape)currentShape;
Console.Write("{0}'s area is {1}\n ", threeDimensional.getName(), threeDimensional.getArea());
Console.Write("{0}'s volume is:{1}\n", threeDimensional.getName(), threeDimensional.getVolume());
}
}
Console.ReadKey();
}//end method
}
}
[/code]
Dipal ChoksiRank #199 · 12K reputationPosted Nov 7, 2011, 1:42 PM EST
albert albertRank #2,683 · 519 reputationPosted Nov 7, 2011, 9:56 PM EST
shapeInheritanceApp.Shape[]Circle'area is ....
Satish BhatRank #2,496 · 563 reputationPosted Nov 7, 2011, 11:29 PM EST
If you are talking about getting ZERO value for area/volume for Square/Cube, you are using local class variables for the calculation which is not assigned. If you want to use local variables, in the constructor of both Square and Cube classes, assign the value for the local variable(s).
albert albertRank #2,683 · 519 reputationPosted Nov 8, 2011, 12:08 AM EST