❔ 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,
static int Example()
static int Example()
and have tried to call it in a new file called Test.cs, by calling
Program.Example()
Program.Example()
but get the error: Cannot use local variable or local function declared in a top-level statement in this context
15 Replies
TheRanger
TheRanger2y ago
can u show code? show ur Program.cs
dankmememachine
dankmememachineOP2y ago
Console.WriteLine(CountWords(args[0], args[1]));
static int CountWords(string file, string word) => System.Text.RegularExpressions.Regex.Matches(File.ReadAllText(file), $@"\b{word}\b", System.Text.RegularExpressions.RegexOptions.IgnoreCase).Count;
Console.WriteLine(CountWords(args[0], args[1]));
static int CountWords(string file, string word) => System.Text.RegularExpressions.Regex.Matches(File.ReadAllText(file), $@"\b{word}\b", System.Text.RegularExpressions.RegexOptions.IgnoreCase).Count;
TheRanger
TheRanger2y ago
thats the whole Program.cs ?
dankmememachine
dankmememachineOP2y ago
it would be this script:
class Test
{
public static void TestMethod(string file, string word) => Console.WriteLine(Program.CountWords(file, word));
}
class Test
{
public static void TestMethod(string file, string word) => Console.WriteLine(Program.CountWords(file, word));
}
and yes
TheRanger
TheRanger2y ago
so calling Test.TestMethod gives u the error u mentioned above?
dankmememachine
dankmememachineOP2y ago
yes
TheRanger
TheRanger2y ago
i dont see u calling Test.TestMethod here
dankmememachine
dankmememachineOP2y ago
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:
Test.TestMethod(args[0], args[1]);

public static partial class Program
{
public static int CountWords(string file, string word) => System.Text.RegularExpressions.Regex.Matches(File.ReadAllText(file), $@"\b{word}\b", System.Text.RegularExpressions.RegexOptions.IgnoreCase).Count;
}
Test.TestMethod(args[0], args[1]);

public static partial class Program
{
public static int CountWords(string file, string word) => System.Text.RegularExpressions.Regex.Matches(File.ReadAllText(file), $@"\b{word}\b", System.Text.RegularExpressions.RegexOptions.IgnoreCase).Count;
}
TheRanger
TheRanger2y ago
u can just not use Top Level statement and use the old way
Pobiega
Pobiega2y ago
All methods made in a TLS file are local
TheRanger
TheRanger2y ago
public class Program
{
public static void Main(string[] args)
{
Test.TestMethod(args[0], args[1]);
}

public static int CountWords(string file, string word) => System.Text.RegularExpressions.Regex.Matches(File.ReadAllText(file), $@"\b{word}\b", System.Text.RegularExpressions.RegexOptions.IgnoreCase).Count;
}
public class Program
{
public static void Main(string[] args)
{
Test.TestMethod(args[0], args[1]);
}

public static int CountWords(string file, string word) => System.Text.RegularExpressions.Regex.Matches(File.ReadAllText(file), $@"\b{word}\b", System.Text.RegularExpressions.RegexOptions.IgnoreCase).Count;
}
dankmememachine
dankmememachineOP2y ago
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
TheRanger
TheRanger2y ago
u cant as they are local methods
Anton
Anton2y ago
you have to wrap it in a class
Accord
Accord2y ago
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.
Want results from more Discord servers?
Add your server