What's wrong with Private keyword

G

Guest

Hey,

Can some one tell me about what's wrong with the following code and what is
the solution with some meaningful correction

-----------------------------
class Program
{
static void Main(string[] args)
{
int x, y;
Console.WriteLine("Enter 2 numbers");
x= Convert.ToInt32(Console.ReadLine());
y=Convert.ToInt32(Console.ReadLine());

x = MaxNum(x, y);

Console.WriteLine("The big number is {0}",x);

}
private int MaxNum(int l, int m)
{
if (l > m)
return l;
else
return m;
}
}
-----------------------------
 
G

Guest

Nothing's wrong with the private keyword. It just needs to be "private
static" -- to match the fact that it's being called from Main, which is
static. Study static.
Peter
 
J

Jon Skeet [C# MVP]

Private said:
Can some one tell me about what's wrong with the following code and what is
the solution with some meaningful correction

<snip>

Others have stated what's wrong. However, I think it's important to
note that if you look at the compiler error carefully, it's reasonably
clear:

Test.cs(11,17): error CS0120: An object reference is required for the
nonstatic field, method, or property 'Program.MaxNum(int, int)'

Yes, it uses "jargon" - but jargon you should make yourself familiar
with if you're not already.

If ever you get a compilation error and you don't know why, the first
thing to do is try to understand the error message itself. If there's
any terminology in there that you don't understand, *that's* the next
thing to fix.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Jon said:
If ever you get a compilation error and you don't know why, the first
thing to do is try to understand the error message itself. If there's
any terminology in there that you don't understand, *that's* the next
thing to fix.

I agree. Error messages are so very informative these days, it's almost
cheating. ;)

Do you remember things like:

ERROR - 141

Or three bombs appearing on the screen, signifying an address error
exception?

Now the error messages say exactly what's wrong, and sometimes even what
you should do to fix the error. I wonder how long it'll be until the
error messages offers to fix the errors themselves? :)
 
J

Jon Skeet [C# MVP]

Göran Andersson said:
I agree. Error messages are so very informative these days, it's almost
cheating. ;)

Do you remember things like:

ERROR - 141

Or three bombs appearing on the screen, signifying an address error
exception?

I'm not sure which is worse - those, or the C++ error messages you tend
to get when using templates, which are both long *and* incomprehensible
to mere mortals.
Now the error messages say exactly what's wrong, and sometimes even what
you should do to fix the error. I wonder how long it'll be until the
error messages offers to fix the errors themselves? :)

Eclipse has had that for ages - and very handy it is too :) ReSharper
makes VS 2005 better at doing it, too.
 
B

Bob Milton

Jon,
Actually, the C++ template errors are very clear compared to the old IBM
errors in the System 360 days. My favorite was IEH240I. When you looked it
up, the explanation was, in effect, something is wrong, we have no idea
what, but it is obviously the programmer's fault!
Bob

Göran Andersson said:
I agree. Error messages are so very informative these days, it's almost
cheating. ;)

Do you remember things like:

ERROR - 141

Or three bombs appearing on the screen, signifying an address error
exception?

I'm not sure which is worse - those, or the C++ error messages you tend
to get when using templates, which are both long *and* incomprehensible
to mere mortals.
Now the error messages say exactly what's wrong, and sometimes even what
you should do to fix the error. I wonder how long it'll be until the
error messages offers to fix the errors themselves? :)

Eclipse has had that for ages - and very handy it is too :) ReSharper
makes VS 2005 better at doing it, too.
 
R

rossum

I wonder how long it'll be until the
error messages offers to fix the errors themselves?
C# Express offers to create a stub for any method you reference but
have not declared.

JBuilder can fix some Java errors, when asked it will add a reference
to an exception at the start of a method if you have not done so.

rossum
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top