Passing variable to a method | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Passing variable to a method

How to receive a variable from a method and send a variable to another method. https://code.sololearn.com/cT5QY5ZmX6RT/?ref=app

27th Mar 2022, 10:56 PM
Akang Toshi
Akang Toshi - avatar
9 Answers
+ 1
Akang Toshi, If you want to pass <c>, <d> and <e> from receive( ) to getCdE( )? then change getCdE( ) to accept three parameters, and then call getCdE( ) from receive( ), passing <c>, <d>, and <e> as arguments. What you need `ref` or `out` for again?
28th Mar 2022, 6:43 AM
Ipang
+ 2
// Hope this code helps you static int c=0, d=0; static float e=0; static void Main(string[] args) { int a=3; int b=4; getCdE(a, b); } public static void getCdE(int valueA, int valueB) { c = valueA + valueB; d = valueB - valueA; e = valueB / 2; float output = c + d + e; Console.WriteLine(output); }
27th Mar 2022, 11:39 PM
SoloProg
SoloProg - avatar
+ 2
SoloProg Thanks for the answer but, i dont want that. The thing is I want something like one method that can accept paramater as well as that method can send out a variable like i have explained in my code. My code seems un-logical but, i need to do something like that. Perhaps I can acheive this by using call by reference or out something like that?
28th Mar 2022, 6:15 AM
Akang Toshi
Akang Toshi - avatar
+ 1
Okay yes I see now. Thank you so much Ipang so simple yet, my My dumb brain won't figure it out. 😄😄
28th Mar 2022, 6:54 AM
Akang Toshi
Akang Toshi - avatar
+ 1
Okay this may seem funny but I am still a newbie a self taught learner and I came across 'ref' and 'out' lastnight and lol i thought 'out' was for something like passing output paramater from the same method. 😭😄😄😄😄😁
28th Mar 2022, 7:00 AM
Akang Toshi
Akang Toshi - avatar
+ 1
Okay, well the similarities of `ref` and `out` is, they both allows the changes of argument data in the called function to also reflect in the calling function. The main difference is, arguments passed by `ref` specifier needs to be initialized in the calling function before they are passed through, but arguments passed by `out` doesn't need to be initialized in the calling function. Hope I put it right though, but you may find it better explained here ... https://www.dotnettricks.com/learn/csharp/difference-between-ref-and-out-parameters
28th Mar 2022, 7:10 AM
Ipang
0
No problem. Just wondering what you plan to use `ref` or `out` for still ...
28th Mar 2022, 6:56 AM
Ipang
0
I have a very vague idea about it.
28th Mar 2022, 7:01 AM
Akang Toshi
Akang Toshi - avatar
0
Or is it really something like that?
28th Mar 2022, 7:02 AM
Akang Toshi
Akang Toshi - avatar