CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2012
    Posts
    10

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

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Method Parameters....

    Quote Originally Posted by Opticknerve View Post
    [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/

  3. #3
    Join Date
    May 2012
    Posts
    10

    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

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Method Parameters....

    Quote Originally Posted by Opticknerve View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured