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