Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - Console Application in .NET

Reply
 
Old 09-08-2008   #1 (permalink)
Curious


 
 

Console Application in .NET

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?

My System SpecsSystem Spec
Old 09-08-2008   #2 (permalink)
Family Tree Mike


 
 

Re: Console Application in .NET

You could also declare your routine as static:

static double RoundToTwoDecimalPlaces(double val)

"Curious" <fir5tsight@xxxxxx> wrote in message
news:0e2080c2-58af-4a3c-b552-26e63ac3c558@xxxxxx
Quote:

> 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?
My System SpecsSystem Spec
Old 09-09-2008   #3 (permalink)
Mr. Arnold


 
 

Re: Console Application in .NET

Curious wrote:
Quote:

> 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().

My System SpecsSystem Spec
Old 09-09-2008   #4 (permalink)
Curious


 
 

Re: Console Application in .NET

Thanks Family Tree Mike! It works if I declare the method static in
class Program.
My System SpecsSystem Spec
Old 09-09-2008   #5 (permalink)
Curious


 
 

Re: Console Application in .NET

Arnold,

Thanks for your input! FYI, (double d) doesn't have to be defined as
static inside static Main.
My System SpecsSystem Spec
Old 09-09-2008   #6 (permalink)
Jon Skeet [C# MVP]


 
 

Re: Console Application in .NET

Curious <fir5tsight@xxxxxx> wrote:
Quote:

> Thanks for your input! FYI, (double d) doesn't have to be defined as
> static inside static Main.
And indeed can't. Local variables can never be declared as static (in
C#).

--
Jon Skeet - <skeet@xxxxxx>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
My System SpecsSystem Spec
Old 09-12-2008   #7 (permalink)
Curious


 
 

Re: Console Application in .NET

On Sep 9, 3:03*pm, Jon Skeet [C# MVP] <sk...@xxxxxx> wrote:
Quote:

> And indeed can't. Local variables can never be declared as static (in
> C#).
Good point, Jon!
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Console application Vista General
Console Application in .NET .NET General
Unhandled Exception in Console Application .NET General
ImageList for Console Application .NET General
Help with stderr from native console application PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46