why this msg?

V

vinnie

this is my little code, the msg i get is:
a) Error 2 'dico.Program.Calcolo(decimal)': not all code paths return
a value C:\Documents and Settings\Robert\Desktop\prova VS8\dico\dico
\Program.cs 42 43 dico;

b) Error 1 A local variable named 'valueted' cannot be declared in
this scope because it would give a different meaning to 'valueted',
which is already used in a 'parent or current' scope to denote
something else C:\Documents and Settings\Robert\Desktop\prova VS8\dico
\dico\Program.cs 24 36 dico;

And this is the code:

namespace dico
{
public class Program
{
public static void Main(string[] args)
{
decimal value = 0;
decimal total = 0;
Insert(value);
Mostra(value);
Calcolo(total);

}

public static decimal Insert(decimal valueted)
{
Console.Write(" Please insert the umber to
evaluate ");
string val = Console.ReadLine();
decimal valueted = Convert.ToDecimal(val);
Console.WriteLine();
while (true)
{
if (valueted >=2)
{
return valueted;
}
Console.WriteLine(" Attenzione valore
minore di due, riprova...");
Console.WriteLine();
}
}

public static void Mostra(decimal valueted)
{
Console.WriteLine(" Il valore da te inserito
come numero di elemnti e': " +valueted);
}

public static decimal Calcolo(decimal valueted)
{

for (int i=10; i<=valueted; i--)
{
Console.WriteLine(" Insert the first
number to use: ");
decimal total;
total = valueted - 1;
Console.WriteLine(total);
}
}
}
}


all i was trying to do was to
a) ask for a number;
b) subtract 1 each time (100-1=99; 99-1=98;...)
c) show the result.

To say that i'm desperate doens't give my right mood now...
 
D

Doug Semler

<snip>

I am debating whether to help you with your homework problems (I thought you
said you were familiar with programming? This is basic first year
functional programming class.)

The error messages are quite clear.
a) Your function Insert isn't returning a value, but you declared it as
returning a decimal.
b) Your function Insert has declared a local variable and parameter of the
same name.
You need to decide whether you want the valuted parameter to be local to the
function and returned, or if you want it to be passed as a ref parameter to
the function.

Your loop in calcolo is definitely not going to work as expected.
Your function calcolo is going to run into the same problem with not
returning a value.

Based on your requirements, you are calling Insert, Calcolo and Mostra in
the wrong order.

--
Doug Semler, MCPD
a.a. #705, BAAWA. EAC Guardian of the Horn of the IPU (pbuhh).
The answer is 42; DNRC o-
Gur Hfrarg unf orpbzr fb shyy bs penc gurfr qnlf, abbar rira
erpbtavmrf fvzcyr guvatf yvxr ebg13 nalzber. Fnq, vfa'g vg?
 
J

Jon Skeet [C# MVP]

vinnie said:
this is my little code, the msg i get is:
a) Error 2 'dico.Program.Calcolo(decimal)': not all code paths return
a value C:\Documents and Settings\Robert\Desktop\prova VS8\dico\dico
\Program.cs 42 43 dico;

And it's absolutely right. You've declared that your method will return
a result, but you're not returning anything.
b) Error 1 A local variable named 'valueted' cannot be declared in
this scope because it would give a different meaning to 'valueted',
which is already used in a 'parent or current' scope to denote
something else C:\Documents and Settings\Robert\Desktop\prova VS8\dico
\dico\Program.cs 24 36 dico;

You've got a parameter called valueted, but you're trying to declare a
local variable with the same name. You can't do that.

While we're at it, look at this loop:

for (int i=10; i<=valueted; i--)

Assuming valueted starts off as 12, that's going to last a very long
time:

i=10 - is i <= 12? Yes. i--...
i=9 - is i <= 12? Yes. i--...
i=8 - is i <= 12? Yes. i--...

etc
 
B

Ben Voigt [C++ MVP]

Doug Semler said:
<snip>

I am debating whether to help you with your homework problems (I thought
you said you were familiar with programming? This is basic first year
functional programming class.)

The error messages are quite clear.
a) Your function Insert isn't returning a value, but you declared it as
returning a decimal.

To be fair, this one isn't true. Insert has only one exit point and it does
return a value.
 
J

Jon Skeet [C# MVP]

Ben Voigt said:
To be fair, this one isn't true. Insert has only one exit point and it does
return a value.

Indeed - Doug just got the wrong method. As per the compiler error
message, it's the Calcolo method which doesn't return a value.
 
D

Doug Semler

Ben Voigt said:
To be fair, this one isn't true. Insert has only one exit point and it
does return a value.

Ach, Gott. I wasn't paying attention and didn't read the error message.
The functionality was so convoluted....




--
Doug Semler, MCPD
a.a. #705, BAAWA. EAC Guardian of the Horn of the IPU (pbuhh).
The answer is 42; DNRC o-
Gur Hfrarg unf orpbzr fb shyy bs penc gurfr qnlf, abbar rira
erpbtavmrf fvzcyr guvatf yvxr ebg13 nalzber. Fnq, vfa'g vg?
 

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