❔ How would I call a method created as a top level statement outside of the initial file?
I have Program.cs with a method that is a static int, and have tried to call it in a new file called Test.cs, by calling but get the error: Cannot use local variable or local function declared in a top-level statement in this context
15 Replies
can u show code?
show ur Program.cs
thats the whole
Program.cs
?it would be this script:
and yes
so calling Test.TestMethod gives u the error u mentioned above?
yes
i dont see u calling Test.TestMethod here
I found a solution by refactoring the code I believe, it was changed since the original question. I was able to call by using a partial program class like so:
u can just not use Top Level statement and use the old way
All methods made in a TLS file are local
right that works, i was trying to create a code golf solution and wanted to keep the file at a minimum lines, i could not find a way to make that method accessible by another script in the shortest form that I first provided
there are other ways to do this, I was trying to see if there was a way to access a method defined as a top level statement elsewhere in the code
u cant as they are local methods
you have to wrap it in a class
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.