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;
}
}

AlanRank #669 · 3.6K reputationPosted Oct 25, 2007, 8:31 AM EDT
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.
Posted Oct 25, 2007, 9:54 AM EDT
Thank you very much for the help, Alan
Posted Oct 25, 2007, 12:38 PM EDT
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.
AlanRank #669 · 3.6K reputationPosted Oct 25, 2007, 1:29 PM EDT
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 26, 2007, 6:31 AM EDT
Thank you very much Alan, introducing me a new concept.
Posted Nov 1, 2007, 7:07 AM EDT
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;
}
AlanRank #669 · 3.6K reputationPosted Nov 1, 2007, 8:20 AM EDT
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:08 AM EDT
Thank you, Alan
Posted Nov 7, 2007, 2:51 AM EST
In the 1st program what is the need for pValue += 5; because without this program is executing well. Anyone knows please explain the reason.
AlanRank #669 · 3.6K reputationPosted Nov 7, 2007, 3:39 AM EST
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, 8:25 AM EST
Thank you very much for the help, Alan