so long? no other way?

V

vinnie

I'm a self learner, and i'm trying to figure out how this code should
work. What is not clear to me, is why the function InsertNumber is
called 3 times: wasn;t easier to call it just once and put all
together?

There is a call to that function at line 8, 13, 17

1 namespace riprova
2 {
3 public class Program
4 {
5 public static void Main(string[] args)
6 {
7 decimal number = 0;
8 InsertNumber(ref number);
9 Console.Write(" Number to
be considerd: " + number);
10 Console.Read();
11 Show(number);
12 }
13 public static void
InsertNumber(ref decimal number)
14 {
15 number = InsertNumber("
Value ");
16 }
17 public static decimal
InsertNumber(string answer)
18 {
19 Console.WriteLine(" Insert
the value of " + answer);
20 string accept =
Console.ReadLine();
21 decimal value =
Convert.ToDecimal(accept);
22 return value;
23 }
24 public static void
Show(decimal number)
25 {
26 for (int amount = 0;
amount >= number; amount++)
27 {
28 number = (number + 1);
29
Console.WriteLine(number);
30 Console.ReadLine();
31 Console.Read();
32 }
33 }
34 }
35 }
 
N

Nicholas Paldino [.NET/C# MVP]

Vinnie,

InsertNumber appears to be called two times, and it's not the same
version of InsertNumber that is called. Basically, the overload of
InsertNumber which takes a string seems to be querying the user for the
number, and then doing the conversion. The overload that returns a decimal
doesn't really do much.

I think the program would make more sense if the overload of
InsertNumber which took a string was called something else. This way, you
wouldn't be confused about the naming.

Overall, the code could be broken out better, IMO.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


vinnie said:
I'm a self learner, and i'm trying to figure out how this code should
work. What is not clear to me, is why the function InsertNumber is
called 3 times: wasn;t easier to call it just once and put all
together?

There is a call to that function at line 8, 13, 17

1 namespace riprova
2 {
3 public class Program
4 {
5 public static void Main(string[] args)
6 {
7 decimal number = 0;
8 InsertNumber(ref number);
9 Console.Write(" Number to
be considerd: " + number);
10 Console.Read();
11 Show(number);
12 }
13 public static void
InsertNumber(ref decimal number)
14 {
15 number = InsertNumber("
Value ");
16 }
17 public static decimal
InsertNumber(string answer)
18 {
19 Console.WriteLine(" Insert
the value of " + answer);
20 string accept =
Console.ReadLine();
21 decimal value =
Convert.ToDecimal(accept);
22 return value;
23 }
24 public static void
Show(decimal number)
25 {
26 for (int amount = 0;
amount >= number; amount++)
27 {
28 number = (number + 1);
29
Console.WriteLine(number);
30 Console.ReadLine();
31 Console.Read();
32 }
33 }
34 }
35 }
 
V

vinnie

Vinnie,

InsertNumber appears to be called two times, and it's not the same
version of InsertNumber that is called. Basically, the overload of
InsertNumber which takes a string seems to be querying the user for the
number, and then doing the conversion. The overload that returns a decimal
doesn't really do much.

I think the program would make more sense if the overload of
InsertNumber which took a string was called something else. This way, you
wouldn't be confused about the naming.

Overall, the code could be broken out better, IMO.


Hi Nicholais, thanks for that.

Can you help me to make it more "understandable" ? I'm really so
confused

Thanks
 
V

vinnie

vinnie,

Which part of the code are you confused about?

Hi NIcholais,
i read you answer and i agree that is really hard to follow,
especially for me that i'm starting now.

What i don't really understand, is why i have to use 3 times a
function that has the same name: i just copied from the book, so i
don't really know how to change it.

I'm making my step toward the C# and it's harder than i though.

However, why i can't just make it this way:
a) ask the number;
b) verify the number;
c) do the operation (subtraction);
d) show the result.

Why i need to build a so difficult code?
 
G

Guest

You still seem to be confused with the Console.Read and Console.ReadLine
methods. I suggest you take these out except for the one in
InsertNumber(string), as that one is necessary. Run in the debugger and
break in Show() at the console.writeline. You appear to be increasing your
loop limit each iteration through the loop.
 
G

Guest

Vinnie,

I don't see any subtraction anywhere in your code...


vinnie said:
vinnie,

Which part of the code are you confused about?

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


On Sep 4, 11:02 am, "Nicholas Paldino [.NET/C# MVP]"
Vinnie,
InsertNumber appears to be called two times, and it's not the same
version of InsertNumber that is called. Basically, the overload of
InsertNumber which takes a string seems to be querying the user for the
number, and then doing the conversion. The overload that returns a
decimal
doesn't really do much.
I think the program would make more sense if the overload of
InsertNumber which took a string was called something else. This way,
you
wouldn't be confused about the naming.
Overall, the code could be broken out better, IMO.

Hi NIcholais,
i read you answer and i agree that is really hard to follow,
especially for me that i'm starting now.

What i don't really understand, is why i have to use 3 times a
function that has the same name: i just copied from the book, so i
don't really know how to change it.

I'm making my step toward the C# and it's harder than i though.

However, why i can't just make it this way:
a) ask the number;
b) verify the number;
c) do the operation (subtraction);
d) show the result.

Why i need to build a so difficult code?
 
V

vinnie

vinnie,

Which part of the code are you confused about?

Hey i saw your web site: really nice!

Btw, are you ready to challenge our paintball team? :)

I was reading you Bio, like mine... but i'm just in the southern part
of US

I agree with you: you can;t be successful if you don;t love what you
do...
 
M

Mythran

It appears to me that the code sample provided is not trying to be the *most
efficient* code possible, but instead it's making an attempt to show you
(the reader) some of the features of the language/framework. Such as
passing a value "by reference", method overloading, method return values,
converting/parsing, looping, etc.

The part that I think you are having a problem understanding is "method
overloading" and the way the author uses it.

HTH,
Mythran
 
B

Ben Voigt [C++ MVP]

Mythran said:
It appears to me that the code sample provided is not trying to be the
*most efficient* code possible, but instead it's making an attempt to show
you (the reader) some of the features of the language/framework. Such as
passing a value "by reference", method overloading, method return values,
converting/parsing, looping, etc.

The part that I think you are having a problem understanding is "method
overloading" and the way the author uses it.

Probably because it is a wholly inappropriate use of overloading.

There are several well-known good uses for overloading:
(1) provide default arguments
(2) treat certain types specially for increased efficiency while still
performing the same conceptual task
(3) ??? I'm sure someone else knows another good use but I can't think of
one right now.
 
D

Doug Semler

Probably because it is a wholly inappropriate use of overloading.

There are several well-known good uses for overloading:
(1) provide default arguments
(2) treat certain types specially for increased efficiency while still
performing the same conceptual task
(3) ??? I'm sure someone else knows another good use but I can't think of
one right now.

Adding to or changing the behavior of a public interface while not
touching the behavior of previously released interfaces.

True story:
We have a product released with an interface function that accepts a
idle timeout value as an integer. One problem: the units of the
timeout are specified in hours. That means that the minimum idle
timeout is 1 HOUR. This function is COM visible. We had a request to
be able to pass in a more fine grained time span for the idle timeout.

Solution: Add a function overload. All current applications behave in
the same manner. Reqesting developer only has to modify one line of
code to pass a timespan instead of an integer. Everybody's happy <g>
 
B

Ben Voigt [C++ MVP]

Doug Semler said:
Adding to or changing the behavior of a public interface while not
touching the behavior of previously released interfaces.

True story:
We have a product released with an interface function that accepts a
idle timeout value as an integer. One problem: the units of the
timeout are specified in hours. That means that the minimum idle
timeout is 1 HOUR. This function is COM visible. We had a request to
be able to pass in a more fine grained time span for the idle timeout.

Solution: Add a function overload. All current applications behave in
the same manner. Reqesting developer only has to modify one line of
code to pass a timespan instead of an integer. Everybody's happy <g>

I think that's a case of #2 or very close. If it wasn't, then it would be
better to use a new name and not overload the existing one.

One problem with your change is that although existing programs keep working
fine, you may change the meaning of other people's source code. Probably
not in your specific case, but it's a potential problem in general.
 

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

Similar Threads

magic code.... 6
section three... correct? 3
why this msg? 5
interface 1
usage of this() 6
Thread 1
waveInOpen 64-bit 0
WCF Service implementing duplex contract keeps throwing an exception 0

Top