C
Curious
When I select File->New->Project, and then if I pick "Console
Application", it automatically creates a file, "Program.cs".
After I fill in some code in the Main method, the content of
"Program.cs" is below:
class Program
{
static void Main(string[] args)
{
double d = 45.66778;
d = RoundToTwoDecimalPlaces(d);
}
double RoundToTwoDecimalPlaces(double val)
{
string decimal_adjusted = val.ToString("N4");
double val_adjusted = double.Parse(decimal_adjusted);
return val_adjusted;
}
}
However, this won't compile. The error is:
"An object reference is required for the nonstatic field, method, or
property 'Program.RoundToTwoDecimalPlaces(double)'"
I must create a new class such as "MyNumeric", and add the method
"RoundToTwoDecimalPlaces" to the class, "MyNumeric". Then I have to
create in "Main" an object such as "mn" and use "d =
mn.RoundToTwoDecimalPlaces(d);"
It seems that I'll have to access the method "RoundToTwoDecimalPlaces"
from an object instead of accessing it directly from the same class
"Program". This doesn't make sense to me.
Anyone comments on this?
Application", it automatically creates a file, "Program.cs".
After I fill in some code in the Main method, the content of
"Program.cs" is below:
class Program
{
static void Main(string[] args)
{
double d = 45.66778;
d = RoundToTwoDecimalPlaces(d);
}
double RoundToTwoDecimalPlaces(double val)
{
string decimal_adjusted = val.ToString("N4");
double val_adjusted = double.Parse(decimal_adjusted);
return val_adjusted;
}
}
However, this won't compile. The error is:
"An object reference is required for the nonstatic field, method, or
property 'Program.RoundToTwoDecimalPlaces(double)'"
I must create a new class such as "MyNumeric", and add the method
"RoundToTwoDecimalPlaces" to the class, "MyNumeric". Then I have to
create in "Main" an object such as "mn" and use "d =
mn.RoundToTwoDecimalPlaces(d);"
It seems that I'll have to access the method "RoundToTwoDecimalPlaces"
from an object instead of accessing it directly from the same class
"Program". This doesn't make sense to me.
Anyone comments on this?