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? 6 3511
You could also declare your routine as static:
static double RoundToTwoDecimalPlaces(double val)
"Curious" <fi********@yahoo.comwrote in message
news:0e**********************************@8g2000hs e.googlegroups.com...
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?
Curious wrote:
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)'"
That's because Main() is *Static* and every procedure called from Main()
must be declared as Static too.
Don't hold me to it, because it's been awhile, but even declaration of
(double d) may need to be (static double d), because you declared it in
(static Main()) instead of declaring it between class Program and the
static Main() statements, outside of static Main().
Thanks Family Tree Mike! It works if I declare the method static in
class Program.
Arnold,
Thanks for your input! FYI, (double d) doesn't have to be defined as
static inside static Main.
On Sep 9, 3:03*pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
And indeed can't. Local variables can never be declared as static (in
C#).
Good point, Jon! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Oz |
last post by:
This is long. Bear with me, as I will really go through all the convoluted
stuff that shows there is a problem with streams (at least when used to
redirect stdout).
The basic idea is that my...
|
by: Mullin Yu |
last post by:
i want to build an application of both gui and batch interface by using
windows application project.
i check either passing any args or not. if no, then open the gui
application. if yes, use the...
|
by: Mark Allison |
last post by:
Hi,
I have an application that I want to be to run in Console mode and GUI
mode. If no params are entered, I want the GUI fired up, if params are
entered, then go into console mode.
I believe...
|
by: Barry Mossman |
last post by:
Hi,
can I detect whether my class is running within the context of a Console
application, vs say a WinForm's application ?
also does anyone know whether the compiler or runtime is smart enough...
|
by: MumboJumbo |
last post by:
Hi I have a really basic question hopefully some can help me with:
Can you write a (i.e. one) C# project that works from the cmd line and gui?
I seems if i write a GUI app it can't write to...
|
by: inpreet |
last post by:
I am trying to build a console application in C#.Net. This application
is suppose to run in background without user interaction. How can I
hide console to appear?
|
by: Mythran |
last post by:
Is it possible to attach Windows WndProc hooks into a Console application
window?
Thanks,
Mythran
|
by: tony |
last post by:
Hello!
When you have windows forms you have the same possibility as when you have a
Console application
to use Console.Writeln to write whatever on the screen.
Now to my question:
Is it...
|
by: Stephany Young |
last post by:
When one uses the System.Diagnostics.Process.Start method to launch a common
or garden Console application, one can set the WindowStyle property of the
StartInfo object to ProcessWindowStyle.Hidden...
|
by: Dilip |
last post by:
Hi All
I have a server based C# console application. This application must
hide its console window when its launched out on the field. So I
dutifully P/Invoke'd FindWindow/ShowWindow...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |