Reputation: 17
I've created a program which should calculate the surface area of an irregular shaped object, such as a lake. I read in a file, which contained the values for the x and y values, and the depth.
I'm new to C#, and so I don't fully understand everything yet, but I think my code should work, however, it doesn't seem to be writing the value for the area onto the screen.
I know that Console.WriteLine(_surface);
SHOULD work , but it I can't seem to get it to do anything, and it's probably in the wrong place!
Can someone please tell me where I'm going wrong?
My code is as follows.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using NUnit.Framework;
namespace ConsoleApplication1
{
public class ValueXyz
{
public double X { get; set; }
public double Y { get; set; }
public int Z { get; set; }
}
public class SurfaceCalculator
{
private ValueXyz[] _valuesXyz;
private double _surface;
private readonly string _textWithValues;
public SurfaceCalculator(string textWithValues)
{
_textWithValues = textWithValues;
SetValuesToCalculate();
}
public double Surface
{
get { return _surface; }
}
public void CalculateSurface()
{
for (var i = 0; i < _valuesXyz.Length; i++)
{
if (_valuesXyz[i].Z == 0)
_surface = (_valuesXyz[i].X * _valuesXyz[i + 1].Y) - (_valuesXyz[i + 1].X * _valuesXyz[i].Y);
Console.WriteLine(_surface);
}
}
private void SetValuesToCalculate()
{
var valuesXyz = _textWithValues.Split(' ');
_valuesXyz = valuesXyz.Select(item => new ValueXyz
{
X = Convert.ToDouble(item.Split(',')[0]),
Y = Convert.ToDouble(item.Split(',')[1]),
Z = Convert.ToInt32(item.Split(',')[2])
}).ToArray();
}
public void TestSurfaceCalculatorGetsAValue()
{
var textWithValues = File.ReadAllLines(@"C:\Users\user\Documents\Visual Studio 2010\Projects\Lake_Take_Toooooo\Lake_Take_Toooooo\bin\Debug\Lake_Test.csv");
var calculator = new SurfaceCalculator(_textWithValues);
calculator.CalculateSurface();
Assert.IsNotNull(calculator.Surface);
}
static void Main()
{
Console.ReadKey();
}
}
}
This is my first time using classes, so apologies if there's an obvious answer.
Thanks for your help!
Upvotes: 0
Views: 2474
Reputation: 6595
Are you trying to run this as console application or as unit test? (It looks like you're trying to run it as unit test since you're using NUnit.Framework and there is a Test-method with an Assert...)
If you want to run it as console application, you have to call the code that should get executed in the Main method.
If you want to run it as unit test, you have to add some "attributes" to your test class and test method. The class should have the [TestFixture] attribute, and the method should have the [Test] attribute, like:
[TestFixture]
public class SurfaceCalculator {
...
[Test]
public void TestSurfaceCalculatorGetsAValue() {
...
}
}
Upvotes: 0
Reputation: 62439
You need to actually call the method inside the Main
method, which is the program entry point. Like:
static void Main()
{
CalculateSurface();
Console.ReadKey();
}
When you run your program, only the code inside the Main
method is actually executed. If you do not call anything from there then no code is executed.
Upvotes: 5
Reputation: 3867
No function is being called in the Main event... I should imagine read key will wait for key input then close, correct?
Upvotes: 1