my first class - getting an error

V

vinnie

I did my first project with the classes, but i get an error that i
don't understand, and so don't know how to correct it.

The error is :
"Error 1 'mixed_class.Program' does not contain a definition for
'Calculus' and no extension method 'Calculus' accepting a first
argument of type 'mixed_class.Program' could be found (are you missing
a using directive or an assembly reference?) C:\Documents and Settings
\Robert\Desktop\prova VS8\mixed_class\Program.cs 20 20 mixed_class"



This is the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace mixed_class
{
public class Program
{
public static void Main(string[] args)
{
double c = 0;
Console.WriteLine(" Program for calculating the sum");
Console.WriteLine();
Console.WriteLine(" Insert first value:...");
double a = Convert.ToDouble(Console.ReadLine());
Console.WriteLine(" Insert the second value:... ");
double b = Convert.ToDouble(Console.ReadLine());
Program So = new Program();
c = So.Calculus(3, 4);
Console.WriteLine(" The sum is: {0}", c);
Console.ReadLine();
}
}
public class summing
{

public double Calculus(double a, double b, out double c)
{
c = a + b;
return c;
}
}
}
 
P

Peter Duniho

vinnie said:
I did my first project with the classes, but i get an error that i
don't understand, and so don't know how to correct it.

I'm not sure what the problem you're having interpreting the error is,
but I _suspect_ you are being led astray by the "...and no extension
method blah blah blah" part. A similar example is the error in .NET 2.0
that says the "best overload" doesn't take certain parameters.

This is a pet peeve of mine, with respect to the compiler errors. In
some cases, in a well-intentioned effort to provide you with additional
guidance, the compiler introduces some concept that you may not even be
aware of, completely confusing the issue.

My guess is that if you just look at the very first clause in the error
-- "'mixed_class.Program' does not contain a definition for
'Calculus'" -- you'd figure it out on your own. But with all that
other stuff, a "newbie" is left scratching their head trying to figure
out what it _all_ means.

In your specific situation, the issue here is that you are using an
instance of a "Program" class, "So", to try to call the "Calculus"
method. But the "Calculus" method exists in the "summing" class, not
the "Program" class. So what you actually need is an instance of the
"summing" class.

In fact, since at least right now your "Program" class only contains a
single static method, I don't see any reason to ever instantiate it.

Change these two lines:

Program So = new Program();

so that it instead reads:

summing So = new summing();

and the error should go away.

As a more general rule, you should really try hard to break compiler
messages (errors or otherwise) into small enough pieces so that you can
really understand what they are saying. The error you got is pretty
clear about what the problem is, as long as you can avoid getting
overwhelmed by the stuff you don't understand.

In this case, the compiler clearly explained that the "Program" class
you're trying to use doesn't contain the "Calculus" method that you're
trying to call. That should have been able to lead you directly to
realizing that you had the wrong class when you were trying to call
"Calculus". Since you should have expected the compiler to be looking
for "Calculus" in the "summing" class, from that error it should have
been simple to just figure out why the compiler is looking at the wrong
class. And of course, that turns out to be because the variable is the
wrong type. :)

Pete
 
R

Registered User

I did my first project with the classes, but i get an error that i
don't understand, and so don't know how to correct it.

The error is :
"Error 1 'mixed_class.Program' does not contain a definition for
'Calculus' and no extension method 'Calculus' accepting a first
argument of type 'mixed_class.Program' could be found (are you missing
a using directive or an assembly reference?) C:\Documents and Settings
\Robert\Desktop\prova VS8\mixed_class\Program.cs 20 20 mixed_class"



This is the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace mixed_class
{
public class Program
{
public static void Main(string[] args)
{
double c = 0;
Console.WriteLine(" Program for calculating the sum");
Console.WriteLine();
Console.WriteLine(" Insert first value:...");
double a = Convert.ToDouble(Console.ReadLine());
Console.WriteLine(" Insert the second value:... ");
double b = Convert.ToDouble(Console.ReadLine());
Here an instance of type Program is created.
Program So = new Program();
This won't work because the Program class does not have a Calculus
method. The previous line should apparently instantiate an instance of
the summing class.
c = So.Calculus(3, 4);
Console.WriteLine(" The sum is: {0}", c);
Console.ReadLine();
}
}
public class summing
{

public double Calculus(double a, double b, out double c)
{
c = a + b;
return c;
}
The return value and the out parameter of method are unneeded
duplicates. This function's signature matches the call to Calculate in
the Program cless' main function.
public double Calculus(double a, double b)
{
double c = a + b;
return c;
}
regards
A.G.
 
P

Paul

This is a pet peeve of mine, with respect to the compiler errors. In
some cases, in a well-intentioned effort to provide you with additional
guidance, the compiler introduces some concept that you may not even be
aware of, completely confusing the issue.

This is so true. I recall fondly that older systems have had much more useful compiler and runtime error information. Like the C compiler on a DG Eclipse system. The stack trace was always right on and you could fix a bug in minutes, without usenet! And the manuals were complete and accessible in a nearby book rack. One thing that annoys me about microsoft progamming is they never tell you the variable that caused a null value exception. What would be so hard about that? And the time saving would be enormous. They never seem to build on the knowledge of past technology and how good some parts of it were.

You can just call the addition like this.

double c = Summing.CalculateSum(a, b);

public class Summing
{
public static double CalculateSum(double a, double b)
{
return a + b;
}
}

It wouldn't work way you have it since the arguments are incorrect.
 
P

Peter Duniho

Paul said:
This is so true. I recall fondly that older systems have had much more
useful compiler and runtime error information. Like the C compiler on a
DG Eclipse system. The stack trace was always right on and you could fix
a bug in minutes, without usenet!

Well, I was talking about the compiler, rather than the debugger. But
yes, I think there are some things the debugger makes more confusing
than it needs to be too.

That said, I have never had trouble getting a correct stack trace.
And the manuals were complete and
accessible in a nearby book rack.

Oddly enough, I not only use electronic docs for practically everything
now, I usually don't even use the local copies, preferring instead to
use the web site versions that are almost always available. I keep the
local copies around just in case, but the online docs seem more
up-to-date, include community comments in some cases (like MSDN doc
pages), and tend to be better linked to external resources.

When MSDN came out, my first thought was "I'll never give up my books".
But apparently I was wrong. :)
One thing that annoys me about
microsoft progamming is they never tell you the variable that caused a
null value exception. What would be so hard about that?

Actually, that one seems pretty hard to me. Compilers don't generate
symbols matching specific machine instructions to specific variables.
So the debugger would have to reverse-engineer whatever machine
instructions led up to the null reference in order to identify the exact
variable, if any, that caused the exception.

In non-optimized builds it might be a little easier. I'd hate to think
what the debugger would have to go through to figure things out in an
optimized build.

My usual solution is to just look at the variables involved, and if one
is null when it shouldn't be, I've found the culprit. Not too hard. :)
And the time
saving would be enormous. They never seem to build on the knowledge of
past technology and how good some parts of it were.

I've never seen a debugger that told me exactly what variable had the
invalid data that led to an exception. That doesn't mean they don't
exist, but I wouldn't call that a standard feature, even in "past
technology".
You can just call the addition like this.

double c = Summing.CalculateSum(a, b);

public class Summing
{
public static double CalculateSum(double a, double b)
{
return a + b;
}
}

It wouldn't work way you have it since the arguments are incorrect.

Heh...funny thing is I thought about suggesting the method be static,
and then thought "I just railed about confusing newbies with more
information than they really need at the moment", and figured I'd better
leave that out.

But if I had, I might have looked at the code closely enough to notice
that extraneous "out" parameter that you picked up on. :)

Pete
 

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