![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | 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)'" > 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 Specs![]() |
| | #4 (permalink) |
| | Re: Console Application in .NET Thanks Family Tree Mike! It works if I declare the method static in class Program. |
My System Specs![]() |
| | #5 (permalink) |
| | 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 Specs![]() |
| | #6 (permalink) |
| | 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. 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 Specs![]() |
| | #7 (permalink) |
| | 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#). |
My System Specs![]() |
![]() |
| 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 | |||