I new to Visual C# and just wanted to create a tiny CLI-based application. With the written codes below, I am receiving this error: "Object reference not set to an instance of an object.". Obviously, since I'm a noob, I have no idea how fix the problem.
Here's the code I have in Program.cs:
class Program
{
static void Main(string[] args)
{
GetAverage(args);
}
static void GetAverage(string[] s)
{
var ave = new Average();
ave.arg = s;
ave.FindAverage();
ave.DisplayResult();
}
}
Here's the code I have in the Average.cs:
public class Average
{
public Average()
{
Console.Write("\n" + "Given numbers: ");
foreach (string s in this.arg)
{
Console.Write(this.arg + " ");
num += Double.Parse(s);
}
}
public double num = 0;
public string[] arg;
public double result;
public void FindAverage()
{
this.result = this.num / this.arg.Length;
}
public void DisplayResult()
{
Console.WriteLine("\n" + "Average: " + this.result);
}
}
What I wanna do is to access the arguments from Main method of the Program class so that I can work with it from the Average class.
Please help me with this one. Thanks!