C
C#2y ago
azurlified

❔ Is it possible to put a method into a variable and call the variable?

like this:
34 Replies
azurlified
azurlified2y ago
Or atleast something similar to that
Angius
Angius2y ago
A static method, yes A non-static method, no But why do you want to do that here? You can just get the value from this method and print that
azurlified
azurlified2y ago
I'm just messing around and trying to learn things not for any particular reason
mtreit
mtreit2y ago
That's not exactly true, as long as you have an instance.
MODiX
MODiX2y ago
mtreit#6470
REPL Result: Success
var something = new MyClass().SomeMethod;

something();

class MyClass
{
public void SomeMethod()
{
Console.WriteLine("SomeMethod called!");
}
}
var something = new MyClass().SomeMethod;

something();

class MyClass
{
public void SomeMethod()
{
Console.WriteLine("SomeMethod called!");
}
}
Console Output
SomeMethod called!
SomeMethod called!
Compile: 564.016ms | Execution: 73.617ms | React with ❌ to remove this embed.
Angius
Angius2y ago
Huh TIL Thought Funcs and Actions need to be static
mtreit
mtreit2y ago
Nope 🙂
azurlified
azurlified2y ago
isn't that basically what I'm doing? not sure why it might not be working for me
mtreit
mtreit2y ago
Paste your full code
333fred
333fred2y ago
You're invoking aMethod With the () after the name
azurlified
azurlified2y ago
333fred
333fred2y ago
So var method is the thing that aMethod() returns, not aMethod itself You want var method = Object.aMethod; (note the lack of parentheses)
azurlified
azurlified2y ago
Oh I see i'm basically doing () twice
mtreit
mtreit2y ago
Presumably that code doesn't even compile since aMethod takes an int and you aren't passing one
azurlified
azurlified2y ago
although it's telling me this now
azurlified
azurlified2y ago
333fred
333fred2y ago
Are you using .NET Framework?
Angius
Angius2y ago
I started assuming everybody with the green mark next to their name is using Framework lol Microsoft should really hid the (.NET Framework) templates from VS
mtreit
mtreit2y ago
Legacy codebases are a huge part of the ecosystem
Angius
Angius2y ago
Legacy stuff is already on Framework, no new projects to be made Besides, not saying remove it completely, just slap it into some "show more" dropdown
mtreit
mtreit2y ago
I agree if you are creating brand new .NET Framework projects that will not be referenced by other .NET Framework projects you should reconsider your life choices.
azurlified
azurlified2y ago
it's not vs it's w3schools code executer lol
Angius
Angius2y ago
Why
azurlified
azurlified2y ago
if i'm going to use vs, what template should I use if I just want to mess around and run random things?
Angius
Angius2y ago
$newproject
MODiX
MODiX2y ago
When creating a new project, prefer using .NET over .NET Framework, unless you have a very specific reason to be using .NET Framework. .NET Framework is now legacy code and only get security fix updates, it no longer gets new features and is not recommended. https://cdn.discordapp.com/attachments/569261465463160900/899381236617855016/unknown.png
azurlified
azurlified2y ago
sick
Angius
Angius2y ago
The console will be good for starters
mtreit
mtreit2y ago
If you use $code (not screenshots) to paste your exact source code that is failing it will be a lot more clear what the issue you are seeing is.
MODiX
MODiX2y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
azurlified
azurlified2y ago
what's the difference between WPF application and console application?
Angius
Angius2y ago
WPF has all the windows and buttons and text inputs and stuff Console is... console Just the text
mtreit
mtreit2y ago
Well, unless you go for something like https://github.com/gui-cs/ 🙂
GitHub
gui.cs
Modern, cross-platform, terminal UIs since 2007. gui.cs has 6 repositories available. Follow their code on GitHub.
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
More Posts