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".

The content is below:

class Program
{
static void Main(string[] args)
{
StreamReader sr = null;

try
{

double d = 45.66778;
d = RoundToTwoDecimalPlaces(d);
console.WriteLine(d.ToString());

}
catch (Exception e)
{
Console.WriteLine(e.Message);

}


}


double RoundToTwoDecimalPlaces(double val)
{
// val contains your given value

// we declare a string that contains the truncated value
string decimal_adjusted = val.ToString("N4");

double val_adjusted = double.Parse(decimal_adjusted);

return val_adjusted;
}

}

It won't even compile. The error is:

"An object reference is required for the nonstatic field, method, or
property 'IO.Program.RoundToTwoDecimalPlaces(double)'"

I must create a new class such as "MyNumeric", and add the method
"RoundToTwoDecimalPlaces" to this class. Then I must create in "Main"
an instance of "mn" and use "d=mn.RoundToTwoDecimalPlaces(d);"

It seems that in order to make the code compile, I have to create a
new class and access the method "RoundToTwoDecimalPlaces" from that
class. This doesn't make sense to me- It's a generic math function and
shouldn't be attached to any class.

Anyone agree with me on this?

My System SpecsSystem Spec
Old 09-10-2008   #2 (permalink)
Cor Ligthert[MVP]


 
 

Re: Console Application in .NET

Curious,

I like it more like this.

\\\
class Program
{
static void Main(string[] args)
{

try
{

double d = 45.66778;
d = new RunPart().RoundToTwoDecimalPlaces(d);
Console.WriteLine(d.ToString());


}
catch (Exception ex)
{
Console.WriteLine(ex.Message);

}


}

class RunPart
{
internal double RoundToTwoDecimalPlaces(double val)
{
// val contains your given value

// we declare a string that contains the truncated value
string decimal_adjusted = val.ToString("N4");

double val_adjusted = double.Parse(decimal_adjusted);

return val_adjusted;
}

}
}
///

Cor

"Curious" <fir5tsight@xxxxxx> schreef in bericht
news:e97d6b98-4233-4687-a8c5-57001e70a332@xxxxxx
Quote:

> When I select File->New->Project, and then if I pick "Console
> Application", it automatically creates a file, "Program.cs".
>
> The content is below:
>
> class Program
> {
> static void Main(string[] args)
> {
> StreamReader sr = null;
>
> try
> {
>
> double d = 45.66778;
> d = RoundToTwoDecimalPlaces(d);
> console.WriteLine(d.ToString());
>
> }
> catch (Exception e)
> {
> Console.WriteLine(e.Message);
>
> }
>
>
> }
>
>
> double RoundToTwoDecimalPlaces(double val)
> {
> // val contains your given value
>
> // we declare a string that contains the truncated value
> string decimal_adjusted = val.ToString("N4");
>
> double val_adjusted = double.Parse(decimal_adjusted);
>
> return val_adjusted;
> }
>
> }
>
> It won't even compile. The error is:
>
> "An object reference is required for the nonstatic field, method, or
> property 'IO.Program.RoundToTwoDecimalPlaces(double)'"
>
> I must create a new class such as "MyNumeric", and add the method
> "RoundToTwoDecimalPlaces" to this class. Then I must create in "Main"
> an instance of "mn" and use "d=mn.RoundToTwoDecimalPlaces(d);"
>
> It seems that in order to make the code compile, I have to create a
> new class and access the method "RoundToTwoDecimalPlaces" from that
> class. This doesn't make sense to me- It's a generic math function and
> shouldn't be attached to any class.
>
> Anyone agree with me on this?
My System SpecsSystem Spec
Old 09-12-2008   #3 (permalink)
Curious


 
 

Re: Console Application in .NET

On Sep 10, 1:15*pm, "Cor Ligthert[MVP]" <notmyfirstn...@xxxxxx>
wrote:
Quote:

> Curious,
>
> I like it more like this.
>
> \\\
> class Program
> * * {
> * * * * static void Main(string[] args)
> * * * * {
>
> * * * * * * try
> * * * * * * {
>
> * * * * * * * * double d = 45.66778;
> * * * * * * * * d = new RunPart().RoundToTwoDecimalPlaces(d);
> * * * * * * * * Console.WriteLine(d.ToString());
>
> * * * * * * }
> * * * * * * catch (Exception ex)
> * * * * * * {
> * * * * * * * * Console.WriteLine(ex.Message);
>
> * * * * * * }
>
> * * * * }
>
> * * * * class RunPart
> * * * * {
> * * * * * *internal double RoundToTwoDecimalPlaces(double val)
> * * * * * * {
> * * * * * * * * // val contains your given value
>
> * * * * * * * * // we declare a string that contains the truncated value
> * * * * * * * * string decimal_adjusted = val.ToString("N4");
>
> * * * * * * * * double val_adjusted = double.Parse(decimal_adjusted);
>
> * * * * * * * * return val_adjusted;
> * * * * * * }
>
> * * * * }
> * * }
> ///
>
Thanks Cor! I agree that "internal" is the right protection method -
It means public within the assembly. It can be used anywhere in the
assembly.

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