|
-
June 5th, 2012, 12:00 PM
#1
Method Parameters....
Hy guys its me again with another question from my book..... Something seems wrong with my solution the the question but i just cant put my finger on it...
Question from my C# book....
Write a method to multiply a number by 10. The method must not return anything, but must manipulate a parameter passed by value. Write another method similer to the first one but pass the parameter by reference. Compare the results of the methods by writing to the console.
Code:
using System;
class MethodParameters
{
public int MulitplyValue(int numVal1, int numVal2)
{
numVal1 = 7;
Console.WriteLine("This is numVal1 before MulitplicationValue method :" + numVal1);
return numVal1 * numVal2;
}
public int MultiplyRef(ref int numRef1, int numRef2)
{
numRef1 = 12;
Console.WriteLine("This is numRef1 before MultiplicationRef method :" + numRef1);
return numRef1 * numRef2;
}
}
class TestNum
{
public static void Main()
{
int numVal1 = 15;
const int numVal2 = 10;
int numVal3;
int numRef1 = 30;
const int numRef2 = 10;
int numRef3;
MethodParameters valuesReferences = new MethodParameters();
numVal3 = valuesReferences.MulitplyValue(numVal1, numVal2);
Console.WriteLine("NumVal1 within the multiplicationValue method : " + numVal3);
Console.WriteLine("numVal1 after multiplicationValue method : " + numVal1);
numRef3 = valuesReferences.MultiplyRef(ref numRef1, numRef2);
Console.WriteLine("NumRef1 within the muliplicationRef method : " + numRef3);
Console.WriteLine("NumRef1 after the multiplicationRef method : " + numRef1);
}
}
-
June 5th, 2012, 02:00 PM
#2
Re: Method Parameters....
 Originally Posted by Opticknerve
[B]Hy guys its me again with another question from my book..... Something seems wrong with my solution the the question but i just cant put my finger on it...
Umm... ok, so what "seems wrong" exactly?
If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.
Yes; I have a blog too - http://the-angry-gorilla.com/
-
June 5th, 2012, 02:13 PM
#3
Re: Method Parameters....
Thanks for the reply bigEd781. Hornestly I dont know what the problem is hence im seeking your help. Do you think ive answered the question correctly
-
June 5th, 2012, 04:23 PM
#4
Re: Method Parameters....
 Originally Posted by Opticknerve
Thanks for the reply bigEd781. Hornestly I dont know what the problem is hence im seeking your help. Do you think ive answered the question correctly
No.
Code:
The method must not return anything
Both your methods return something.
Btw, the goal of this exercise is to print out the original values after each method runs. It is trying to show you that when passed by value, the original variable doesn't change (but when passed by ref, the original value will change).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|