Tech Off Thread

6 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Need help with Method call

Back to Forum: Tech Off
  • User profile image
    DearCSharp

    HI All,

    C# novice here.

    I was under the impression that when a method is called, the value on the parameters go one way ONLY i.e from the method call to the method itself.

    example

    callResturant(name) //function call with string variable "name" passed to the method.

    in this case can callResturant send a value back to the method call from the parameter? I know that the method can RETURN a value to the call but is it also possible for it to send a value back from the parameter also? I ask because on the following example, it seems that the method is returning values from the parameter to the call.

    Notice the method display movie is void but it is able to send values back to the call from its parameter. You will have to create a file name movie.txt and place it where the debug folder of wherever you are running c# from. Also enter a string value and on the next line enter an integer, and repeat the process until about 10 lines are completed. I entered Pizza and on the next line 5, Sub and the next line 3 so on ..

     

    Any advice would be appreciated.

    using System;
    using System.IO;


    namespace Assingment11
    {
        class Program
        {
            static void Main(string[] args)
            {
                string[] MovieName = new string[20];
                int[] StarRating = new int[20];

                DisplayMovie(MovieName, StarRating);

                for (int i = 0; i < MovieName.Length; i++)
                {
                    Console.WriteLine(MovieName[i]);
                    Console.WriteLine(StarRating[i]);
                }

                Console.ReadLine();

            }


            public static void DisplayMovie(string[] name, int[] star)
            {
                StreamReader inData = File.OpenText("movie.txt");

                for (int i = 0; i < name.Length; i++)
                {
                    name[i] = inData.ReadLine();
                    star[i] = Convert.ToInt32(inData.ReadLine());
                }


            }
        }
    }

     

  • User profile image
    MasterPi

     I know that the method can RETURN a value to the call but is it also possible for it to send a value back from the parameter also?

    The method isn't returning a value. Something else is happening (hint: conceptually, what happens when you call methods with arguments?).

  • User profile image
    cheong

    This part is more verbose with C++ because they can undecorate the dumped header name to see the effect themselves.

    So I guess I'll go with the answer to what he ask directly - search for out and ref keyword.

     

    Recent Achievement unlocked: Code Avenger Tier 4/6: You see dead program. A lot!
    Last modified
  • User profile image
    blowdart

    , cheong wrote

    This part is more verbose with C++ because they can undecorate the dumped header name to see the effect themselves.

    So I guess I'll go with the answer to what he ask directly - search for out and ref keyword.

     

    Stylistically though out parameters are a red flag for code reviews. If you need more than 2 then you really should be returning an object instead

  • User profile image
    cheong

    , blowdart wrote

    Stylistically though out parameters are a red flag for code reviews. If you need more than 2 then you really should be returning an object instead

    Is it?

    We have standard here that when writing data repository methods, we need to include an out parameter to store errormessages instead of throwing it out directly. And if necessary throw them out in one go to allow users correcting data errors in single pass.

    Recent Achievement unlocked: Code Avenger Tier 4/6: You see dead program. A lot!
    Last modified
  • User profile image
    blowdart

    , cheong wrote

    *snip*

    Is it?

    We have standard here that when writing data repository methods, we need to include an out parameter to store errormessages instead of throwing it out directly. And if necessary throw them out in one go to allow users correcting data errors in single pass.

    Well, it depends. I did say it's a flag. Error Message I can kind understand, if you're doing a Try type action, although even then I'd probably prefer a results struct with an error code and message in.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.