C
C#β€’2y ago
Dineo

βœ… how can i call a static void in an if statement

.
46 Replies
πŸ¦ƒ Async-Void γ€‹πŸ¦ƒ
ok so show the code you have tried $paste
MODiX
MODiXβ€’2y ago
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
TheBoxyBear
TheBoxyBearβ€’2y ago
ClassName.StaticMethod() or just StaticMethod() if in the same class
Dineo
DineoOPβ€’2y ago
TheBoxyBear
TheBoxyBearβ€’2y ago
But you can't really call if in an if since it's void Not as part of the condition But it can be in the if body
πŸ¦ƒ Async-Void γ€‹πŸ¦ƒ
the if statement needs to be in a method
TheBoxyBear
TheBoxyBearβ€’2y ago
That too
πŸ¦ƒ Async-Void γ€‹πŸ¦ƒ
is this a console app with top level statements?
TheBoxyBear
TheBoxyBearβ€’2y ago
All instructions apart from initial assignment of members need to be in a method
Dineo
DineoOPβ€’2y ago
ok
Connor
Connorβ€’2y ago
I’m flabbergasted that you know about async/await, http clients, and serialization, but you don’t know how to create a method
Dineo
DineoOPβ€’2y ago
mweh its a proof of concept app for school so i didnt really fix my error
πŸ¦ƒ Async-Void γ€‹πŸ¦ƒ
right, but is it a console app?
Dineo
DineoOPβ€’2y ago
no its a 🀒winforms project🀒
Omnissiah
Omnissiahβ€’2y ago
what should the if do
Dineo
DineoOPβ€’2y ago
say the joke
Dineo
DineoOPβ€’2y ago
Dineo
DineoOPβ€’2y ago
here is the full codebase i know its messy and has alot of memory leaks but i just need that joke thingy to work when sorry if im annoying you in some way
πŸ¦ƒ Async-Void γ€‹πŸ¦ƒ
give me a minute
Omnissiah
Omnissiahβ€’2y ago
then just make it a lambda that calls the method and returns something you can use in the if
Dineo
DineoOPβ€’2y ago
like how ??
Omnissiah
Omnissiahβ€’2y ago
static void tell_the_joke() { /* stuff */ }

void caller()
{
if (new Func<bool>(() => { tell_the_joke(); return true; })())
throw new Exception("it works!");

bool or_like_this() { tell_the_joke(); return true; }
if (or_like_this())
throw new Exception("it works!");
}
static void tell_the_joke() { /* stuff */ }

void caller()
{
if (new Func<bool>(() => { tell_the_joke(); return true; })())
throw new Exception("it works!");

bool or_like_this() { tell_the_joke(); return true; }
if (or_like_this())
throw new Exception("it works!");
}
seems simple to me maybe i didn't get what you want
var uhm = () => { tell_the_joke(); return true; };
if (uhm())
throw new Exception("it works!");
var uhm = () => { tell_the_joke(); return true; };
if (uhm())
throw new Exception("it works!");
maybe you could even do it with Task.Run
Dineo
DineoOPβ€’2y ago
Dineo
DineoOPβ€’2y ago
something like that but i got 2 errors from this actually 3
Dineo
DineoOPβ€’2y ago
Dineo
DineoOPβ€’2y ago
ok
πŸ¦ƒ Async-Void γ€‹πŸ¦ƒ
@Dineo remove the third argument
Dineo
DineoOPβ€’2y ago
wdym
πŸ¦ƒ Async-Void γ€‹πŸ¦ƒ
the first error what does it say?
Dineo
DineoOPβ€’2y ago
ok i did that
Dineo
DineoOPβ€’2y ago
i fixed the other one so just need to fix the third error
πŸ¦ƒ Async-Void γ€‹πŸ¦ƒ
where do you initialize it?
Dineo
DineoOPβ€’2y ago
in my static method but its below that method that shows the error if that helps
πŸ¦ƒ Async-Void γ€‹πŸ¦ƒ
it isnt in scope
Dineo
DineoOPβ€’2y ago
soo :)))
πŸ¦ƒ Async-Void γ€‹πŸ¦ƒ
I dont see it anywhere in the code you posted
Dineo
DineoOPβ€’2y ago
its here close to last lines below materialMultiLineTextBox1.AppendText(r + '\n'); line 260
πŸ¦ƒ Async-Void γ€‹πŸ¦ƒ
right so the scope ends at the last } of the method also you arent deserializing to an object I have to go for awhile
Dineo
DineoOPβ€’2y ago
i fixed it :))) closed
Accord
Accordβ€’2y 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.
Dineo
DineoOPβ€’2y ago
/closed
Hugh
Hughβ€’2y ago
You need to use "/close" rather than "/closed"
FusedQyou
FusedQyouβ€’2y ago
Holy shit how did you even manage to come this far This is some next level mess πŸ™ƒ
ero
eroβ€’2y ago
chatgpt, what a question
Dineo
DineoOPβ€’2y ago
mweh
Want results from more Discord servers?
Add your server