How can I do this??

Hey guys,

I have this LoopMethod which has a parameter, and I have a for loop in it. I want to extract the for loops value and contain it into a variable for use else where. How can I do this? Here’s the code I have so far and its giving me errors like not all paths return a value …

  class LoopClass
    {
     

        public static int LoopMethod(int k)
        {


            int f = 0;
         
        

            for (int i = 0; i < 10; i++)
            {


                return f=i;
             
            }
         
         
        }

    }

Well, I’ll admit to a little confusion on what you are doing, but the error is because you have no return outside of your loop. Also note that this will only return the first value and not each value in the loop. (for example, it will return 0 and not 0-9)

What is it you are trying to do exactly?

The moment you use “return” under any context in a method, you are asking a method to end itself. You are basically starting a forloop but asking it to end on it’s first run. There also seems to be no purpose for your parameter “k”.

So yes, I agree with @Brathnann , knowing what are you going to use this for in the grand scheme of things is far more important than how you think you are going to implement it.

  1. you can’t return a set variable.
    return f = i;

What are you saying here? Are you trying to set f to i, or are you trying to return a value? Do you mean to say:
f = i;
return f;
???

  1. not all code paths return anything. If the loop doesn’t loop for whatever reason, it’ll continue on. So you need to put an extra return statement at the end of the method.

  2. What in the heck are you attempting to do? This will just return 0 as is, because it doesn’t conditionally do anything. The first loop it returns… is there an ‘if’ statement you’re missing?

for(int i = 0; i < 10; i++)
{
    if(i == someCondition) return i;
}

??? Is this what you mean to do?

OR

Are you intending to return a collection of ALL of the i’s? So that you can iterate the elsewhere?

Like an iterator function?

Not enough information here to understand what exactly you’re trying to do…

When coming to the forums don’t just show random code and ask “why doesn’t this do what I want”… tell us what you expect it to do. What you want to accomplish. THEN include your code as a demonstration of how you’re attempting to accomplish it.

Another thing… this isn’t the first thread of yours I’ve seen.

I was in your ‘compare 2 numbers’ thread:

As well as I’ve seen other threads where others already helped you before I came along. But I remember them.

Thing is… most of your threads are attempting to accomplish things completely unrelated to Unity. Are you currently in the process of learning to program for your very first time? Did you pick Unity for a specific reason to do so? Have you considered picking up resources related to C#, that aren’t Unity specific, to learn the fundamentals of the language.

For example… the MSDN documentation for C#. There’s a VAST array of tutorials, books, and documentation out there specifically about C# unrelated to Unity that covers these very basic parts of learning a language.

Technically speaking you can. The assignment operator does return a value. Which is why you can do stupid things in c# like this:

var a = b + (c = d);

Which while being legal code, is also very very stupid code.

I’ve never seen a reasonable case to use the return value of an assignment. But it does have one.

1 Like

You’re not saving the compiler or interperter any time by compressing multiple expressions into one line.
The only language where you should care about brevity of menial tasks is assembly.