Hi Guys
NP55 Incomplete program
Following incomplete program is given in the following website. The article says that “it makes sense that the output from our code is "5"”,.
Anyone knows how to complete the program that makes the output 5. Please help.
Thank you
class Class1
{
public void Go()
{
int x = 5;
AddFive(x);
Console.WriteLine(x.ToString());
}
public int AddFive(int pValue)
{
pValue += 5;
return pValue;
}
}
Posted Nov 7, 2007, 1:25 PM
Thank you very much for the help, Alan
AlanPosted Nov 7, 2007, 8:39 AM
The point of that line is to give pValue a different value in the AddFive() method to what it had when it was passed in as a parameter (using the variable 'x').
If you didn't do that, then it wouldn't be clear whether the change to pValue within the method was being persisted to the calling code (via 'x') or not as the values would be the same.
Posted Nov 7, 2007, 7:51 AM
In the 1st program what is the need for pValue += 5; because without this program is executing well. Anyone knows please explain the reason.
Posted Nov 1, 2007, 3:08 PM
Thank you, Alan
AlanPosted Nov 1, 2007, 12:20 PM
This should do it:
using System;
class Program
{
static void Main()
{
Class1 class1 = new Class1();
class1.Go();
}
}
class Class1
{
public void Go()
{
MyStruct x = new MyStruct();
x.a = 5;
DoSomething(ref x);
Console.WriteLine(x.a.ToString());
}
public void DoSomething(ref MyStruct pValue)
{
pValue.a = 12345;
}
}
public struct MyStruct
{
public long a, b, c, d, e, f, g, h, i, j, k, l, m; // 'public' was missing in article
}
So, here, a MyStruct object is created and assigned to the variable x. It's 'a' field is set to 5.
The object is then passed by reference to the DoSomething() method which changes its 'a' field to 12345. As the variable was passed by reference, the change is persisted when the method returns and so this is the value which is printed to the console.
Posted Nov 1, 2007, 11:07 AM
Following incomplete program is in the above website. Anyone knows please complete it. It must give the output 12345 according to the article.
public void Go()
{
MyStruct x = new MyStruct();
x.a = 5;
DoSomething(ref x);
Console.WriteLine(x.a.ToString());
}
public void DoSomething(ref MyStruct pValue)
{
pValue.a = 12345;
}
Posted Oct 26, 2007, 10:31 AM
Thank you very much Alan, introducing me a new concept.
AlanPosted Oct 25, 2007, 5:29 PM
The Console.WriteLine() method has 19 overloads including overloads which take a single argument of each of the primitive types (int, long, double , bool, string etc).
Console.WriteLine(x), where x is an int, calls the overload which takes an int argument.
Console.WriteLine(x.ToString()) calls the overload which takes a string argument.
However, all the first overload does is to convert x to a string using x.ToString() and then call the string overload.
So, in other words, both of these method calls produce the same result and are just two different ways of doing the same thing. Which you prefer is a matter of taste but, arguably, the second one is slightly better because you only have the overhead of two method calls instead of three (i.e. 2 Console.WriteLine's and an int.ToString()).
Posted Oct 25, 2007, 4:38 PM
What is the purpose of having ToString() in the Console.WriteLine(); method. Because without ToString() method program is producing same output. Anyone knows please explain the reason.
Posted Oct 25, 2007, 1:54 PM
Thank you very much for the help, Alan
AlanPosted Oct 25, 2007, 12:31 PM
If you place this code before the code you've just posted, the output will be '5':
using System;
class Program
{
static void Main()
{
Class1 class1 = new Class1();
class1.Go();
}
}
See if you can figure out why.